Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: CBXE8 - How to dynamically "delete" a component created using "new" ?


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 5 - Last Post: Oct 13, 2015 5:36 PM Last Post By: GAI CHEW KAI
GAI CHEW KAI

Posts: 117
Registered: 7/25/14
CBXE8 - How to dynamically "delete" a component created using "new" ?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 8, 2015 12:17 AM
Embarcadero® C++Builder XE8 Version 22.0.19027.8951 and Indy 10.
Window 10 Pro x64
Android 4.1.2

QUESTIONS

1. I have a component TLabel created such as:

TLabel* pLabel = new TLabel( this );

...and added to TForm via:

Form1->AddObject( pLabel );

How do I dynamically delete pLabel so that it disappears from Form1 ?

I have tried the following methods, but all failed to make pLabel disappears from Form1:

delete pLabel;
delete( pLabel );
pLabel->Free();
pLabel->Destroying();
pLabel->DestroyComponents();

Please advise.
GAI CHEW KAI

Posts: 117
Registered: 7/25/14
Re: CBXE8 - How to dynamically "delete" a component created using "new" ?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 8, 2015 12:42 AM   in response to: GAI CHEW KAI in response to: GAI CHEW KAI
...I noticed by using:

Form1->RemoveObject( pLabel ), it works, pLabel disappeared but do I still need to delete pLabel ?

Please advise.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: CBXE8 - How to dynamically "delete" a component created using "new" ?[Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 8, 2015 9:03 AM   in response to: GAI CHEW KAI in response to: GAI CHEW KAI
GAI wrote:

Android 4.1.2

Mobile platforms use ARC for managing object lifetimes via reference counting.
Desktop platforms do not use ARC for objects.

1. I have a component TLabel created such as:

TLabel* pLabel = new TLabel( this );

You are assigning an Owner to the label. The Owner will free the label when
the Owner itself is freed.

...and added to TForm via:

Form1->AddObject( pLabel );

Use the Parent property instead, just like in VCL:

pLabel->Parent = Form1;


How do I dynamically delete pLabel so that it disappears from Form1 ?

On desktop platforms, 'delete' will free the label immediately. But on mobile
platforms, 'delete' simply removes your reference to the object, decrementing
its reference count. There are still other active references to the label
- the Owner and the Parent - which is why you do not see the label being
freed yet. If you want the label to be freed, you need to remove all of
those references as well:

pLabel->Parent = NULL;  // <-- remove the Paren'st references
this->RemoveComponent(pLabel); // <-- remove the Owner's references
delete pLabel; // <-- now this should work


Or, you can call the Dispose() method to force the label to be freed immediately,
even if there are other references to it (the TComponent.Notification() system
will allow the Parent and Owner to detect the free and remove their references):

pLabel->Dispose();


--
Remy Lebeau (TeamB)
GAI CHEW KAI

Posts: 117
Registered: 7/25/14
Re: CBXE8 - How to dynamically "delete" a component created using "new" ?[Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 8, 2015 8:15 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
> {code}
> pLabel->Parent = NULL; // <-- remove the Paren'st references
> this->RemoveComponent(pLabel); // <-- remove the Owner's references
> delete pLabel; // <-- now this should work
- This method crashed my application.

> On desktop platforms, 'delete' will free the label immediately. But on mobile
> platforms, 'delete' simply removes your reference to the object, decrementing
> its reference count. There are still other active references to the label ...
- I am missing ways in finding information, can you give guidelines as to where to gain the information provided above so I can do a better search ?

>pLabel->Dispose();
- It seems in CBXE only with DisposeOf(), and it works eventually.

Thanks for the feedback :)
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: CBXE8 - How to dynamically "delete" a component created using "new"?[Edit] [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 9, 2015 7:01 PM   in response to: GAI CHEW KAI in response to: GAI CHEW KAI
GAI wrote:

This method crashed my application.

It should not have.

It seems in CBXE only with DisposeOf(), and it works eventually.

Yes, that is the one I meant.

--
Remy Lebeau (TeamB)
GAI CHEW KAI

Posts: 117
Registered: 7/25/14
Re: CBXE8 - How to dynamically "delete" a component created using "new"?[Edit] [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 13, 2015 5:36 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thank you.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02