function, result, .Free (a newbie question) lazarus / 13-General ---------------------------------------------------------------------- slimton1 · 2011-01-28 07:02 Hi. First of all, I'm no programmer. Self taught hobby coder. So be nice. Here's my code which caused me a trouble. Code: [Select]function MyPic(const MyLRSPicture: String): TPicture; var   LoadImage: TPicture; begin   try     LoadImage := TPicture.Create;     LoadImage.Jpeg.LoadFromLazarusResource(MyLRSPicture);     Result := LoadImage;   finally     LoadImage.Free; { This is my problem }   end; end; I have 1.jpg,2.jpg.... in my LRS. So I tried to load and toss my picture to 'TImage' component named 'imgMyPicture1' when I click some button. Code: [Select]imgMyPicture1.Picture := MyPic('1'); The problem is without LoadImage.Free; it's almost perfect but memory leaks every time I click the button... Task manager shows me it's getting bigger and bigger. That's what you call leak, right? However, with LoadImage.Free; it crashes. I think the Result is already returned but... with LoadImage.Clear; there's no image at all so I'm not sure... I'm confused. What did I do wrong? Should I go around with my previous long and ugly code? I'm currently using snapshot. FPC 2.5.1, svn 29xxx (mostly up-to- date), win32. btw, sorry for my bad English. ---------------------------------------------------------------------- slimton1 · 2011-01-28 07:33 I think I've found a solution. Have to move LoadImage: TPicture; to Here: Code: [Select]var   frmMain: TfrmMain;   LoadImage: TPicture; or here: Code: [Select]implementation var   LoadImage: TPicture; and have to Free it after calling it. What a newb! See ya! Edit: Argh. I've tried this with my another (small) Laz project and realized that this can't be a total problem solver. ---------------------------------------------------------------------- theo · 2011-01-28 10:15 Why not write the procedure so that it can be called like this: MyPic('1', imgMyPicture1.Picture); Here you pass the TPicture to the MyPic procedure, no create/free necessary. ---------------------------------------------------------------------- Imants · 2011-01-28 11:44 Problem was than you destroyed your object and returned reference to dead object. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function MyPic(const MyLRSPicture: String): TPicture;//var//  LoadImage: TPicture;begin  //try    Result := TPicture.Create;    Result.Jpeg.LoadFromLazarusResource(MyLRSPicture);   // Result := LoadImage;  //finally //   LoadImage.Free; { This is my problem } // end;end;  and btw theo suggestion was much better. P.S.   If you declare LoadImage: TPicture as global variable and do not free him somewhere before using your function it will cause memory leak ---------------------------------------------------------------------- slimton1 · 2011-01-28 13:02 I was hoping that it's a bug but my impression is it isn't. Is it? Quote from: theo on January 28, 2011, 11:15:33 amMyPic('1', imgMyPicture1.Picture); This worked quite well. Thanks! Although some strange reason I have to change the order of vars. (it prints something about identifier. as you can see I can't figure out that much. ) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i { current simple code }{ wow I love coding }procedure TfrmMain.MyPic(var LoadImage: TImage; MyLRSPicture: String);begin  LoadImage.Picture.Jpeg.LoadFromLazarusResource(MyLRSPicture);end;Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i MyPic(imgMyPicture1{.Picture},'1');{ This is right, right? } Quote from: Imants on January 28, 2011, 12:44:42 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function MyPic(const MyLRSPicture: String): TPicture;//var//  LoadImage: TPicture;begin  //try    Result := TPicture.Create;    Result.Jpeg.LoadFromLazarusResource(MyLRSPicture);   // Result := LoadImage;  //finally //   LoadImage.Free; { This is my problem } // end;end; and btw theo suggestion was much better. Nope, both of you are way better than me. btw, I can confirm that it behave the same like mine. Eat lots of memories everytime I click the button. Hmm... weird. I'm always trying to simple it down but I think I have much to learn. Thanks for the help! ---------------------------------------------------------------------- Imants · 2011-01-28 14:39 Quote from: slimton1 on January 28, 2011, 02:02:56 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i { current simple code }{ wow I love coding }procedure TfrmMain.MyPic(var LoadImage: TImage; MyLRSPicture: String);begin  LoadImage.Picture.Jpeg.LoadFromLazarusResource(MyLRSPicture);end; Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i MyPic(imgMyPicture1{.Picture},'1');{ This is right, right? } It is because imgMyPicture1.Picture is property you can't take property by adders. Solution could be: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  FTemp: TPicture;Begin  FTemp := imgMyPicture1.Picture;  MyPic(FTemp,'1');end;  Quote from: slimton1 on January 28, 2011, 02:02:56 pmNope, both of you are way better than me. btw, I can confirm that it behave the same like mine. Eat lots of memories everytime I click the button. Hmm... weird. I'm always trying to simple it down but I think I have much to learn. Thanks for the help! I think it is because you are using it like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   image.picture := MyPic('C:\....');  every time you execute function it creates new TPicture variable and asigne it to image picture but newer destroys old one. solution could be Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  FTmp: TPicture;Begin  FTmp := MyPic('C:\....');  try    image.picture.Assign(FTmp);  finally    FTmp.Free   end;....  I still think that theo solution is better I just wanted to show you where are memory leaks created ---------------------------------------------------------------------- Marc · 2011-01-28 15:16 Quote from: slimton1 on January 28, 2011, 02:02:56 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i { current simple code }{ wow I love coding }procedure TfrmMain.MyPic(var LoadImage: TImage; MyLRSPicture: String);begin  LoadImage.Picture.Jpeg.LoadFromLazarusResource(MyLRSPicture);end; There is no need to pass an object by var in this case. Change it to Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TfrmMain.MyPic(LoadImage: TImage; MyLRSPicture: String);begin  LoadImage.Picture.Jpeg.LoadFromLazarusResource(MyLRSPicture);end; ---------------------------------------------------------------------- slimton1 · 2011-01-29 01:48 Quote from: Imants on January 28, 2011, 03:39:46 pm I still think that theo solution is better I just wanted to show you where are memory leaks created I see your point and thanks for your efforts. And sorry to cause your a trouble and misunderstanding. Theo's code worked fine without leak. I was pointing your code has same symptom as mine. Oh, and what I do in here is generate random number with random(99)+1 and load equivalent small jpeg images 1..100 from lrs file. I thought calling function is cool. Urgh, how stupid I was. Quote from: Marc on January 28, 2011, 04:16:29 pmThere is no need to pass an object by var in this case. Change it to Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TfrmMain.MyPic(LoadImage: TImage; MyLRSPicture: String);begin  LoadImage.Picture.Jpeg.LoadFromLazarusResource(MyLRSPicture);end;I see. Now that's why I couldn't combine the very code with loop and FindComponent. Thank you! I realize that I should observe and practice a little further and more before asking this and that. Thanks guys!