Bitmaps and Memory Leaks

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Bitmaps and Memory Leaks

Postby Marloes » Thu Sep 03, 2015 6:55 pm

Just a quick question to help me understand how things work. I use FireMonkey but I guess it is the same for VCL.

If I copy a bitmap into a TImage using either Image1->Bitmap->Assign() or Image1->Bitmap->LoadFromFile(),

- do I need to call Clear() when closing a form?
- do I need to call Clear() before calling Assign() or LoadFromFile() to replace the existing picture with another one?

These are probably elementary questions but I would like to understand this aspect better to avoid memory leaks
Marloes
Active Poster
Active Poster
 
Posts: 13
Joined: Mon Dec 01, 2014 5:35 am

Re: Bitmaps and Memory Leaks

Postby rlebeau » Fri Sep 04, 2015 5:07 pm

Marloes wrote:If I copy a bitmap into a TImage using either Image1->Bitmap->Assign() or Image1->Bitmap->LoadFromFile(),

- do I need to call Clear() when closing a form?
- do I need to call Clear() before calling Assign() or LoadFromFile() to replace the existing picture with another one?


No to both. TImage owns its TBitmap, and TBitmap will clear itself when assigning/loading a new bitmap into it.

Marloes wrote:These are probably elementary questions but I would like to understand this aspect better to avoid memory leaks


There is no leak in this situation.

But, what COULD leak is the source bitmap that you are passing to Assign(), if you are dynamically allocating it and not freeing it when finished using it.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1509
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: Bitmaps and Memory Leaks

Postby Marloes » Sat Sep 05, 2015 5:17 am

Thank you.

I only load pictures from file so memory leaks should not be an issue here then :)
Marloes
Active Poster
Active Poster
 
Posts: 13
Joined: Mon Dec 01, 2014 5:35 am


Return to Technical

Who is online

Users browsing this forum: No registered users and 8 guests

cron