Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 161
  • Last Modified:

Pointers

Would it be wrong not to free "B" in GetImage? Because if I do, there's an AV, and if I don't it works fine. Below is a simplification of the real code, removed irrelevant information:


procedure TMainForm.SomeProcedure;
begin
  Image1.Picure.Bitmap:=GetImage; //Image1 is a TImage
end;

function TMainForm.GetImage: TBitmap;
var B: TBitmap;
begin
  B:=TBitmap.Create;              
  B.Assign(ADODataSet.FieldByName('Image'));
  Result:=B;
end;

thx
0
sageryd
Asked:
sageryd
  • 3
  • 3
  • 2
1 Solution
 
sfockCommented:
yes it would be wrong to free the Bitmap in GetImage,
0
 
ziolkoCommented:
function TMainForm.GetImage: TBitmap;
begin
 Result:=TBitmap.Create;              
 Result.Assign(ADODataSet.FieldByName('Image'));
end;
no problem with destroying B :-))
ziolko.
0
 
sfockCommented:
oops somehow pressed submit ;-)
so i wanted to say :

yes it would be wrong to free the Bitmap in GetImage, because the caller of GetImage will get only a reference of the newly created object. If you free it, hell get a reference to a destroyed Object.

You will only have to take care, that the one that gets the Object(refernece) will ensure that the object will be destroyed, when it is not going to be needed anymore.

In the contect you use it TImage ensures this. If you call the somePicture Method again TImage will free your bitmap, when you assign a new bitmap to Image1.Picture.

If you use somePicture somewhere else you may take a look at it.

So the answer to your question is : Yes it is incorrect to free "B" in GetImage. Otherwise you get the AV because Image1.picture is pointing to an invalid ObjectReference.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
sagerydAuthor Commented:
Exactly my thought, thanks for the verification. ;)

Ziolko, thanks for the simplification. Though I will reward sfock the points since he made the explanation in human text.


OOH I LOVE E-E :)
0
 
ziolkoCommented:
ok no problem:-)
ziolko.
0
 
sagerydAuthor Commented:
Unless of course you need points I could get reward ye some aswell ;)  haha
0
 
ziolkoCommented:
:-) I'll take my points when I deserve them.
ziolko.
0
 
sagerydAuthor Commented:
Either way!

*Grawrlph*
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now