Jump to content
BKor

migrating projects to RAD Studio 12

Recommended Posts

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

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
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

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 by weirdo12

Share this post


Link to post
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.

  • Like 1

Share this post


Link to post
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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×