Board index » delphi » TBitmap and TImage : basic q. about memory management
Hernan Gonzalez
Delphi Developer |
TBitmap and TImage : basic q. about memory management2004-01-20 09:31:12 AM delphi278 A simple doubt about memory management when assigning a TBitmap to a TImage component: Say a have a Image1:TImage component in a form, and the image must be updated at runtime; say, when a button is pressed. I coded the method, which responds to the event by reading the filename, loading it in a TBitmap and assigning it to the TImage object in this way: ImgFilename := (... get from another control...) Bitmap1 := TBitmap.Create; Bitmap1.LoadFromFile(ImgFilename); Image1.Picture.Assign(Bitmap1); Bitmap1.Free; This seems to work. The question is : is this correct , in what respect to memory management ? Could this introduce a memory leak? (if it is executed many times, with different images) How does the Assign method work ? It copies the TBitmap object to the Picture's own memory space ? That is: before invoking Bitmap1.Free (which MUST be invoked to avoid a memory leak, if I am not mistaken) immediately after the Picture.Assign , are there two distinct regions in memory that hold (in different formats, perhaps) the bitmap, one in the TBitmap object, the other in the TImage.Picture component ? Is this so ? Thanks Hernan |