Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

multiple new ui classes need delete?



  • I have multiple classes that I use to display different ui forms from push buttons on_click(). When I close the other class UI, that class destructor deletes the ui. Do I also need to call delete() to free those class instances allocated on the heap? Does the other class destructor take care of that?

     OtherClass *other1 = new OtherClass;
    other1->function(data);
    other1->show();
    //other stuff
    delete other1;
    }
    

    Also, when I create Qt classes on the heap, do they get deleted by their destructors?

    QImage *img = new QImage;
    

  • Qt Champions 2019

    Why should a pointer get deleted automatically somewhere when you're using a plain pointer and don't use Qt parent - child relationship?



  • @Psnarf

    If you allocate memory on heap by using new, you have to delete it yourself.
    BTW: In most cases, you dont have to allocate QImages on heap. If you create it on stack and fill it with data, it gets cleaned up when it goes out of scope. (But depends on your case.)



  • @Christian-Ehrlicher
    Color me a Qt newbie. Don't know about Qt parent - child relationships.

    @Pl45m4
    Thanks for the new explanation. I'm using a lot of large images, don't want to exhaust the stack. If I pass them to another class for processing, more better to pass a pointer? Slowly working my way up to edge detection.
    Appreciate your help!



  • @Christian-Ehrlicher

    I put all the new statements in my constructor, then delete them all in my destructor . That way I can keep track of the heap allocation/deallocation.
    other1 *other = new other1(this);
    I think that establishes the parent-child relation? Then other1's destructor gets called to delete its Ui. If I don't do that, the other1 structure remains on the heap as a memory leak when I delete its pointer?



  • @Psnarf

    Yes, this is a possible solution.

    If your other1 class inherits QObject and you set this-class (I guess QMainWindow or any other std Qt class?!) as parent, other1 and even its children get cleaned up, when deleting this ( the parent)

    https://doc.qt.io/archives/qt-4.8/objecttrees.html#overview



  • Obquote: "All I know is what I read in doc.qt.io."
    I learned that QImage is among the list of Implicitly Shared classes which can be passed to functions by value and returned from functions without concern for copying overhead. Must take care with STL-style iterators, though. I missed that feature when I first started using QImages. I'm going to re-write my trig utility classes as QObjects without Ui.
    https://doc.qt.io/qt-5/objecttrees.html

    Should it be of concern to anyone, QtMath trig functions take qreal arguments in radians, not degrees. Doc mentions the return values as radians, but not the arguments.
    Thanks again for your help! I'm now a little-smarter dummy.


Log in to reply