View Full Version : Unable to free bitmap
In this example I can't free bitmap ,because of error:
Error: Exception: Target bitmap was destroyed! at line 14
program new;
procedure test;
var
bmp ,w,h:integer;
TPA : TPointArray;
begin
w := 10;
h := 10;
bmp := createbitmap(w,h);
SetTargetBitmap(bmp);
FindcolorsTolerance(TPA,0,0,0,w-1,h-1,0);
FreeBitmap(bmp);
end;
begin
test;
end.
,so I have choice between getting error ,or to litter memory (The following bitmaps were not freed: [0]). What to do ?
@Edit
program new;
procedure test;
var
bmp ,w,h:integer;
TPA : TPointArray;
begin
w := 10;
h := 10;
bmp := createbitmap(w,h);
SetTargetBitmap(bmp);
FindcolorsTolerance(TPA,0,0,0,w-1,h-1,0);
try
FreeBitmap(bmp);
except
end;
end;
begin
test;
end.
This construction doesn't generate any error. But ,I'm not sure ,is that really free bitmap in memory or just suppress warning messages in debug box?
Sir Ducksworthy
04-15-2012, 07:46 PM
Local Variable, It is stacked but not defined so it stay's in memory.
It is corrupted when another function is called and adds something else to the stack so when trying you get an exception.
Run this and you'll see what I mean:
program new;
procedure test;
var
bmp,w,h:integer;
TPA : TPointArray;
begin
bmp := createbitmap(w,h);
FreeBitmap(bmp);
w := 10;
h := 10;
SetTargetBitmap(bmp);
FindcolorsTolerance(TPA,0,0,0,w-1,h-1,0);
end;
begin
test;
end.
Unfortunately we do not have static variables in Pascal besides Constant's and Global Variables.
Well maybe you are right ,but honestly ,I have no idea what are you talking about :P
You can make bmp as global variable ,problem will still exist.
I thought the issue is ,that Simba is losing access to bitmap's handle after calling SetTargetBitmap...but this bitmap is still accessible and you can operate on it! Only thing you can't do is to free it. Weird:
program new;
{$i srl/srl.simba}
{$I srl/srl/misc/debug.simba}
var
bmp:integer;
procedure test;
var
w,h:integer;
TPA : TPointArray;
begin
w := 30;
h := 30;
bmp := createbitmap(w,h);
SetTargetBitmap(bmp);
FindcolorsTolerance(TPA,0,0,0,w-1,h-1,0);
end;
begin
test;
invertbitmap(bmp); // we can operate on bitmap (if bitmap is corrupted error should be here - but it's fine)
debugbitmap(bmp); // even show it!
try
freebitmap(bmp);
except
writeln('Error: Exception: Target bitmap was destroyed!'); // but not free!
end;
end.
And the cause of problem is function SetTargetBitmap(bmp); ,if you comment it you won't get error.
Sir Ducksworthy
04-15-2012, 09:07 PM
Sorry I forgot to mention it is actually freed,
Run this and you'll see that it No longer Exists :)
program new;
procedure test;
var
w,h:integer;
TPA : TPointArray;
begin
w := 10;
h := 10;
SetTargetBitmap(createbitmap(w,h));
FindcolorsTolerance(TPA,0,0,0,w-1,h-1,0);
try
FreeBitmap(0);
except
Writeln('Destroyed');
end;
FreeBitmap(0);
end;
begin
test;
end.
masterBB
04-15-2012, 09:18 PM
Just set the target back:
program new;
procedure test;
var
bmp ,w,h:integer;
TPA : TPointArray;
begin
w := 10;
h := 10;
bmp := createbitmap(w,h);
SetTargetBitmap(bmp);
FindcolorsTolerance(TPA,0,0,0,w-1,h-1,0);
SetDesktopAsClient;//or what ever your target was
FreeBitmap(bmp);
end;
begin
test;
end.
Run this and you'll see that it No longer Exists
Run this ;)
program new;
{$i srl/srl.simba}
{$I srl/srl/misc/debug.simba}
procedure test;
var
w,h:integer;
TPA : TPointArray;
begin
w := 100;
h := 100;
SetTargetBitmap(createbitmap(w,h));
FindcolorsTolerance(TPA,0,0,0,w-1,h-1,0);
DrawTPABitmap(0,TPA,clgreen);
DebugBitmap(0);
try
FreeBitmap(0);
except
Writeln('Destroyed');
end;
FreeBitmap(0);
end;
begin
test;
end.
Just set the target back:
Thx for advice ,I added SmartSetTarget; ,and it's fixed now :)
Powered by vBulletin® Version 4.2.1 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.