01-09-2013 07:27 AM
I was writing my app without it, but some samples and examples use it.
Since I'm not a C/QML Developer, I have no idea if this is something from one of them, or it's something from Cascade, anyone have a logical explanation for this? What it really does? Is there any use for this code-piece?
Example from: https://developer.blackberry.com/cascades/document
using namespace bb::cascades;
TestApp::TestApp(bb::cascades::Application *app) : QObject(app)
{
QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(th is);
qml->setContextProperty("injection", this);
AbstractPane *root = qml->createRootObject<AbstractPane>();
app->setScene(root);
}
Solved! Go to Solution.
01-09-2013 07:45 AM
01-09-2013 08:19 AM
01-09-2013 10:28 AM
![]()
Thanks, I think I understood, I'll wait my head stop spinning so I can make sure of that.
01-09-2013 12:09 PM
The reason you'll want to use this is because when Qt object trees are destroyed and cleaned-up, the parents destroy their children.This way, cleanup happens in a controlled manner. Earlier sample code tended to instantiate the application on the stack, which would cause it to be destroyed when it goes out of scope when main() exits. I think this can lead to some double-destruction happening if it is also attached to a parent object, so it is probably best these days to do something like shown here:
https://github.com/blackberry/Cascades-Community-S
note that I am allocating my class using new, but then not tracking it anywhere.. this is because the parent object is tracking it for me and will destroy it when the time comes.
If you look back in the commit history, you'll see that I used to do it differently:
the downside with this approach is that "app" and "mainApp" both go out of scope at the end of main(), and I have no control over the order that their destructors will be called. Things could get messy if I had set mainApp's parent object to "app" when destructors get called.
Cheers,
Sean
01-09-2013 12:28 PM
01-09-2013 12:43 PM - edited 01-09-2013 12:49 PM
really?
{
Class1 a;
Class2 b;
}
you're saying that C++ guarantees that b is destroyed before a when this block ends? It hadn't bothered to research this. (EDIT: yup, this is what C++ guarantees) Still dangerous, as simply re-arranging declaration could cause grief, especially if one of those classes is set to destroy the other instance.
01-09-2013 01:19 PM