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
LVL 1
sagerydAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.