Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Contributor
Posts: 26
Registered: ‎09-20-2013
My Device: Z10
My Carrier: Orange

ownership of the pointer passed with signal

The parameters of my signals are pointers of QObject. I am confused who should be the owner of the object. Should the signal sender delete the object or the slots connect to the signal?

 

My emit slignal like below.

void MyManager::processWork() {
    MyClass* m1 = new MyClass();
    emit onFinished(m1);
}

 Who is reponsible to delete m1? And, how do I know if a pointer was deleted by some other class?

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: ownership of the pointer passed with signal

It doesn't really matter where you delete the object as long as you do.

 

I'll assume you are trying to thread the work.

 

Generally you delete the object in the class it was created so as not to bother having to mess around type-casting and importing headers in to the called class/QML (potentially later on it also saves problems if multiple classes/objects listen for the signal).

 

My suggestion would be to create another signal connection that auto-deletes on finshed.

 

Take a look at the example in the Thread Support section of this, to understand how it's done...

 

http://developer.blackberry.com/native/documentation/cascades/dev/fundamentals/

 


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Contributor
Posts: 26
Registered: ‎09-20-2013
My Device: Z10
My Carrier: Orange

Re: ownership of the pointer passed with signal

I download some data and create MyClass with it. Then pass MyClass object to the slot connected to onFinished() signal. Therefore, I cannot delete MyClass in the Manager class as I don't know when the other classes will finish with MyClass objects. But, there may be multiple slots connect to the onFinished signal, although normally only one at a time.

 

My problems here are

  • I don't know if there is any slot connects to onFinished().
  • I can't predicte how may slots connected at the same time.
  • I don't know when those classes finish with the MyClass object.

So, what's is the best place to delete MyClass object? Or, anyway to detect if the oject has been deleted by someone else?

 

BTW, MyClass is subclass of QObject.

Developer
Posts: 17,011
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: ownership of the pointer passed with signal

you can use parent/child model. or a smartpointer.
deleteLater should take connected slots into account as well.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 26
Registered: ‎09-20-2013
My Device: Z10
My Carrier: Orange

Re: ownership of the pointer passed with signal

Do you mean instead of passing the pointer through the singal, pass a shared pointer instead?

 

So, replace

void finished(MyClass* obj)

with

voi finished(SharedPointer<MyClass> ptr)

?