Anders Melander 1795 Posted November 25 5 hours ago, Remy Lebeau said: __fastcall TForm1::~TForm1() { ... for(int j = 0; j < c; ++j) { delete pLayer[j]; } ... } This isn't necessary in this particular case; The TImage32 owns the layers and will automatically destroy them. Share this post Link to post
BKor 0 Posted November 25 Thank you both for your help, reinstalling the gr32 and using the actual constructor&destructor did help. Just one question about "This isn't necessary in this particular case; The TImage32 owns the layers and will automatically destroy them." May I delete pLayer[j] (TImage32) in Form destructor anyway (although not necessary as you say)? Thanks again. Share this post Link to post
Anders Melander 1795 Posted November 25 Just now, BKor said: May I delete pLayer[j] (TImage32) in Form destructor anyway (although not necessary as you say)? Sure, you can if you want to. The layer will remove itself from the layer collection when it's destroyed so the layer will not be destroyed twice. Share this post Link to post
weirdo12 20 Posted Saturday at 08:33 PM (edited) Another approach is to use std::vector<std::unique_ptr<TBitmapLayer>> created by std::make_unique instead of using an array of raw TBitmapLayer pointers which can lead to memory unsafe code. When the std::vector is destroyed (goes out of scope) everything gets cleaned up. Edited Saturday at 08:35 PM by weirdo12 Share this post Link to post
Anders Melander 1795 Posted Sunday at 08:46 AM 12 hours ago, weirdo12 said: Another approach is to use std::vector<std::unique_ptr<TBitmapLayer>> created by std::make_unique instead of using an array of raw TBitmapLayer pointers which can lead to memory unsafe code. When the std::vector is destroyed (goes out of scope) everything gets cleaned up. Having two list think they "own" the same object would be a very bad idea; The layers are already owned and managed by a layer collection. 1 Share this post Link to post
weirdo12 20 Posted Sunday at 05:18 PM 7 hours ago, Anders Melander said: Having two list think they "own" the same object would be a very bad idea; The layers are already owned and managed by a layer collection. So just to extend the conversation as it pertains to the original code, there is no need to store TBitmapLayer* at all (except for convenience as a temporary so that it can be initialized as required). The best practice is to always access a layer through ImageBackground->Layers->Items. Would that be good advice or overkill? Share this post Link to post
Anders Melander 1795 Posted Sunday at 08:02 PM 2 hours ago, weirdo12 said: there is no need to store TBitmapLayer* at all (except for convenience as a temporary so that it can be initialized as required). The best practice is to always access a layer through ImageBackground->Layers->Items. Yes, that's correct; There no need. But I don't see a problem with saving a direct reference somewhere else, for convenience, performance, or readability. In Delphi, I would do something like this: // Create layer var Layer := Image.Layers.Add<TBitmapLayer>; // Set initial layerproperties Layer.Scaled := True; Layer.Bitmap.LoadFromFile('foo.png'); ... or interface type TMyForm = class(TForm) private FBitmapLayer: TBitmapLayer; ... public constructor Create(AOwner: TComponent); override; end; implementation constructor TMyForm.Create(AOwner: TComponent); begin inherited; FBitmapLayer := Image.Layers.Add<TBitmapLayer>; FBitmapLayer.Scaled := True; end; procedure TMyForm.SomeMethod; begin FBitmapLayer.Bitmap.LoadFromFile('foo.png'); ... end; Share this post Link to post