0

I have noticed in QT 5 examples there is no delete call for new widgets allocated.

Does this mean in this example

using namespace std;

#include <QApplication>
#include <QMainWindow>
#include <QTabWidget>


//This is to be used in many files later until the code exits
short * AA = new short[1000000];


int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    MyTabDialog w;
    w.show();


    delete[] AA;  
    return app.exec();

}

the delete call is not needed or its place is wrong?

Thanks

Hatems
  • 39
  • 1
  • 9
  • 1
    possible duplicate of [Memory management in Qt?](http://stackoverflow.com/questions/2491707/memory-management-in-qt) – MrEricSir Sep 30 '14 at 05:30
  • @MrEricSir That question's answers do not cover QObjects allocated in stack, which is the case with `w`. – hyde Oct 06 '14 at 17:26

2 Answers2

2

If you really want to do this in your code snippet you should place delete after application event loop is finished:

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    MyTabDialog w;
    w.show();


    int result = app.exec();
    delete[] AA;
    return result;
}

But why do you allocate AA from the heap? You can just define it as following:

short AA[1000000];

And in Qt examples the widgets commonly created dynamically, for example:

QWidget* myWidget = new QWidget(this);

In example above this is pointer to another QObject instance which is now the parent for myWidget. Now myWidget instance will be automatically destroyed on deleting of it's parent widget and that's why we don't need to call delete explicitly for this instance.

Object trees and ownership reference here.

Max Go
  • 2,092
  • 1
  • 16
  • 26
  • I wanted on the heap because there will too many arrays needed to be global, their total size would make the stack too big – Hatems Sep 30 '14 at 05:32
  • @Hatems, it seems when you allocate your variables statically (in my answer AA is defined as global static array) they for sure not stored on stack. Check this answer http://stackoverflow.com/a/11698458/2266412 – Max Go Sep 30 '14 at 05:53
  • Thanks for the update. I agree global static variables are not in the stack. Good reminder – Hatems Sep 30 '14 at 06:33
0

In Java, all object variables are actually the object references, somewhat similar to C++ pointers. In C++, object can also be allocated on a stack (like MyTabDialog w in your code). It is not an unitialized pointer (MyTabDialog *w would be). It is initialized object that is created using its parameterless constructor and disposed using its destructor.

Because of that, there is no delete call for the variable w in your sample.

Same way it is possible to allocate and not delete arrays on the stack:

int main(int argc, char *argv[])
{
    short AA[1000000];
    // no delete is required to free AA, beware never to return pointer to it!
}

However the stack space may be more limited than the heap space, so huge structures are normally allocated on the heap.

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93