[fpc-devel] Android SDK's DX reports that a local is invalid

Sven Barth pascaldragon at googlemail.com
Sat Jun 30 17:43:43 CEST 2012


On 30.06.2012 17:31, Jonas Maebe wrote:
>
> 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.

Aha! That explains and solves the problem. Thank you.

Regards,
Sven




More information about the fpc-devel mailing list