Search code examples
c++buildertimage

Although setting TImage::Picture = NULL won't cause a memory leak, what alternatives exist that might avoid that confusion?


Despite bummi's fine explanation of why Image1.Picture := nil won't cause a memory leak, I'm concerned that it may raise red-flags in the minds of new people reading my code, since its safety is counter-intuitive. Therefore, I'd like to avoid it. What more-intuitive alternatives exist? Although bummi's answer was for Delphi, I'm actually more interested in C++Builder.


Solution

  • Encapsulating the property assignment provides two advantages:

    1. a single location to document the non-leakiness of it (DRY)
    2. a less worrisome-looking interface to use everywhere else

    e.g.:

    // Not a memory leak: see http://stackoverflow.com/a/23999207/782738
    #define ClearImage(Image) Image->Picture = NULL