In general, where I have had heap based objects in main, is not for the main 
application, but for test applications…

Or for when the object under test/use, is created via a shared library, and its 
interface returns the pointer.

Scott
From: interest-bounces+scott.bloom=onshorecs....@qt-project.org 
[mailto:interest-bounces+scott.bloom=onshorecs....@qt-project.org] On Behalf Of 
Constantin Makshin
Sent: Saturday, January 11, 2014 3:24 AM
To: Qt Interest
Subject: Re: [Interest] Create the main widget on heap in main()


True, but why would one need a parented object in main()? :-)
On Jan 11, 2014 3:13 PM, "Etienne Sandré-Chardonnal" 
<etienne.san...@m4x.org<mailto:etienne.san...@m4x.org>> wrote:
QObjects with a parent should never be allocated on stack (otherwise, you 
delete them twice)

2014/1/11 Constantin Makshin <cmaks...@gmail.com<mailto:cmaks...@gmail.com>>

Saving some stack space may make sense on mobile platforms, but in general yes, 
creating objects on the heap in main() is pretty much the same thing as 
creating them on the stack.
On Jan 11, 2014 2:02 PM, "Alejandro Exojo" 
<s...@badopi.org<mailto:s...@badopi.org>> wrote:
El Saturday 11 January 2014, 程梁 escribió:
> Yes, I tought there is no any problem about this sample demo. But is there
> any posibility when the application is large? According to your words I
> don't see any problem about creating the main widget on heap because the
> main event loop will close all visible widgets. And no widget should be
> created in other thead so all widgets must be in main thread, that means
> they will be closed at last. So it is OK when you create the main widget
> on heap in main, am I right?

What I don't understand yet is the reason to do it, though. Why not create
them as local variables?

If you insist in doing it that way, is OK, just make sure you don't leak any
memory/resources. One pattern that I saw for creating applications on Sailfish
OS is the following (and that I don't say is either good or bad):

    QScopedPointer<QApplication> app(new QApplication(argc, argv));
    QScopedPointer<QQuickView> view(new QQuickView);
    view->setSource("/path/to/main.qml");
(...)
    return app->exec();

So, basically is the same, but the objects are instantiated with new, and you
make sure you don't leak them through a smart pointer class.

For your example with a QLabel is easier, since if the object has a parent,
you can forget about it, and the parent will delete all its children.

--
Alex (a.k.a. suy) | GPG ID 0x0B8B0BC2
http://barnacity.net/ | http://disperso.net
_______________________________________________
Interest mailing list
Interest@qt-project.org<mailto:Interest@qt-project.org>
http://lists.qt-project.org/mailman/listinfo/interest

_______________________________________________
Interest mailing list
Interest@qt-project.org<mailto:Interest@qt-project.org>
http://lists.qt-project.org/mailman/listinfo/interest

_______________________________________________
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

Reply via email to