I think you should compile this code to understand my problem ,because it's quite complex ,and I can't describe it clear:P
Simba Code:
program new;
Function BmpToTIA(Bitmap_Handle : integer): T2dIntegerArray;
Var
I, J ,w ,h : Integer;
B : TBitmap;
Begin
try
B := TBitmap.Create;
B.Handle := Bitmap_Handle;
w := B.Width;
writeln('inside function '+tostr(w));
h := B.Height;
SetLength(Result,w);
For I:= 0 to w-1 Do
SetLength(Result[I],h);
For I := 0 to w-1 Do
For J := 0 to h-1 Do
Result[I][J] := B.Canvas.Pixels[I, J];
B.Free; // this line is evil
except
writeln('Exeption in BmpToTIA');
end;
End;
var
f,ff : T2dIntegerArray;
bmp,bmp2,w,h ,a:integer;
M : Tmufasabitmap;
B_global : Tbitmap;
begin
GetClientDimensions(w,h);
bmp := bitmapfromclient(1,1,w-1,h-1);
M := GetMufasaBitmap(bmp);
B_global := M.ToTBitmap;
writeln('global :' +tostr(b_global.Width) +' handle: ' +tostr(B_global.Handle));
f := BmpToTIA(b_global.Handle );
writeln('global :' +tostr(b_global.Width) +' handle: ' +tostr(b_global.handle)+ ' - global object didn''t change ,but:');
ff := BmpToTIA(b_global.Handle );
writeln('It can''t be used by function any more!');
M.Free;
end.
Output:
Code:
global :149 handle: 855972512
inside function 149
global :149 handle: 855972512 - global object didn't change ,but:
inside function 0
It can't be used by function any more!
Basically what I have here:
First i made Tbitmap object with image of client inside (B_global). Then I put it's handle to function ,which do stuff with it (in this case converts bitmap to T2dintegerarray ,but it's not important).
Inside function I made new Tbitmap object (B) and assigned it to handle of global TBitmap. After all I add line B.Free; ,which should free local obcject and not affect global one.
But ,what is very weird ,this line (B.Free
do something ,that if I sent global object to this function second time it can't use it (see output).
1. Anyone can explain this?
2. If I create local object inside a function and don't free it ,it will be free automatically when function executed or i will have memory leak?