[fpc-devel] Android SDK's DX reports that a local is invalid
Jonas Maebe
jonas.maebe at elis.ugent.be
Sat Jun 30 17:31:35 CEST 2012
On 30 Jun 2012, at 16:34, Sven Barth wrote:
> procedure TTest.CopyFile(aSrc, aDest: JIFile);
> var
> outstr: JIFileOutputStream;
> instr: JIFileInputStream;
> outchannel, inchannel: JNCFileChannel;
> begin
> outstr := JIFileOutputStream.Create(aDest);
> instr := JIFileInputStream.Create(aSrc);
>
> try
> inchannel := instr.getChannel;
> outchannel := outstr.getChannel;
> inchannel.transferTo(0, inchannel.size, outchannel as JNCWritableByteChannel);
> finally
> try
> if Assigned(inchannel) then
> inchannel.close;
> finally
> if Assigned(outchannel) then
> outchannel.close;
> end;
> end;
> end;
The problem is that Android (and the JVM) assume that it's possible that an exception occurs inside instr.getChannel. In that case, inchannel will not be initialized inside the finally block, and you're not allowed to use any potentially uninitialized data. So you have to explicitly set inchannel and outchannel to nil before the first try-block.
Jonas
More information about the fpc-devel
mailing list