语言

Menu
Sites
Language
Is AddControl()/Construct() method copies object?

I found in samples code where to Construct() method of object is passed temporary object:

Label* pLabel3 = new Label();
pLabel3->Construct(Rectangle(0,0,150,80), L"LEFT");

And have the following questions:

  1. Is this safe? As my understanding this temporary object will be deleted when we exit of outer function/method.
  2. Why this is working? Does this mean that Construct() holds copy of this object?
  3. Can I pass object, created by new operator? And who should release memory of this object (allocated by new)?

And the same question about AddControl() function. Because to this one is passed object allocated by new operator.

Does this mean that

  1. I can't pass temporary object to AddControl() function?
  2. Framework will release itself "new"-allocated objects passed to the AddControl() method?

响应

5 回复

All UI objects passed to AddControl() will be deleted automatically when the app terminates. If you try to deallocate them manually, you will crash the app.

If the UI object not passe to AddControl(), like KeyPad object, then you need to deallocate it manually.

 

 

Alex Ashirov

Hi Victor,

According to

https://developer.tizen.org/dev-guide/2.2.1/org.tizen.native.appprogramming/html/guide/ui/containers.htm

"Since all child controls are automatically removed and deleted when the parent container is deleted, you do not have to explicitly remove child controls by yourself. Furthermore, if you delete a child control without removing it from its parent container, it causes unhandled exceptions when the parent tries to remove the deleted control."

 

Alex Ashirov

Hi Victor,

Regarding your 1st question about Construct() method.

  1. This is safe.
  2. Don’t worry about that. The Construct() will copy object if it’s needed.
  3. Yes, you can pass object created by new operator. In this case you should release it later because the Construct() knows nothing about where/how the object was created.
Victor Malov

Thanks! To sum up:

  1. Construct() method copies objects and for this reason we can use temporary objects, as in my example. Also we can use heap-allocated objects, but should delete them manually, for example after passing to Construct() method.
  2. AddControl() uses only heap-allocated objects and should automatically delete these objects. After passing the pointer to AddControl() don't need worry about it.
Alex Ashirov

Hi,

Everything is correct.