Does clearing an image by assigning Image1.Picture := nil; cause a memory leak?

旧时模样 提交于 2019-12-22 04:37:25

问题


I have read here http://delphi.about.com/od/adptips2005/qt/cleartimage.htm that a way to clear an image from a TImage is to assign nil to .Picture, like this:

Image1.Picture := nil;

I just want to be sure... I guess the Image1.Picture.loadFromFile(fileName) will allocate some memory and simply setting it to nil, without freeing the memory, will lead to a memory leak.

Am I correct? If this is so, which is the "proper" way to unload and clear an image from a TImage?


回答1:


The TImage.Picture setter is TImage.SetPicture() in the ExtCtrls unit, which calls TPicture.Assign() in the Graphics unit, which calls TPicture.SetGraphic(), which will free an existing Graphic before assigning a new Graphic.

So the usage of

Image1.Picture := nil; 

Will ultimately call

Image1.Picture.SetGraphic(nil); 

And will not cause any memory leak.



来源:https://stackoverflow.com/questions/23991829/does-clearing-an-image-by-assigning-image1-picture-nil-cause-a-memory-leak

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!