?
Solved

Pointers

Posted on 2003-03-04
8
Medium Priority
?
160 Views
Last Modified: 2010-04-04
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
Comment
Question by:sageryd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 3

Expert Comment

by:sfock
ID: 8066197
yes it would be wrong to free the Bitmap in GetImage,
0
 
LVL 21

Expert Comment

by:ziolko
ID: 8066205
function TMainForm.GetImage: TBitmap;
begin
 Result:=TBitmap.Create;              
 Result.Assign(ADODataSet.FieldByName('Image'));
end;
no problem with destroying B :-))
ziolko.
0
 
LVL 3

Accepted Solution

by:
sfock earned 200 total points
ID: 8066240
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:sageryd
ID: 8066387
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
 
LVL 21

Expert Comment

by:ziolko
ID: 8067208
ok no problem:-)
ziolko.
0
 
LVL 1

Author Comment

by:sageryd
ID: 8067432
Unless of course you need points I could get reward ye some aswell ;)  haha
0
 
LVL 21

Expert Comment

by:ziolko
ID: 8067707
:-) I'll take my points when I deserve them.
ziolko.
0
 
LVL 1

Author Comment

by:sageryd
ID: 8067782
Either way!

*Grawrlph*
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question