Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. new command onto qobject ... doubt!

new command onto qobject ... doubt!

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • gfxxG Offline
    gfxxG Offline
    gfxx
    wrote on last edited by
    #1

    I use some number of New QObject ... macro ... the qobject live if my app live .... I read in these day some post and now I have a doubt .... I must use a Delete macro for every new or not? My app works fine ... so this request only serves to be sure to have a decent code at least.

    regards
    Giorgio

    bkt

    jsulmJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Can you show your code ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • gfxxG gfxx

        I use some number of New QObject ... macro ... the qobject live if my app live .... I read in these day some post and now I have a doubt .... I must use a Delete macro for every new or not? My app works fine ... so this request only serves to be sure to have a decent code at least.

        regards
        Giorgio

        jsulmJ Online
        jsulmJ Online
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #3

        @gfxx You should read this: http://doc.qt.io/qt-5/objecttrees.html
        Usually in C++ you have to free the memory you allocated via new keyword at some point to avoid memory leaks. You can use smart pointers to free the memory automatically.
        Many Qt classes (all classes derived from QObject) have "parent" parameter in their constructors. If you pass a parent to a child then child will be deleted when parent is deleted, so you do not have to delete child explicitly in this case:

        Parent *parent = new Parent();
        Child * child = new Child(parent);
        ...
        delete parent; // Here child will be deleted as well
        

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        gfxxG 1 Reply Last reply
        3
        • jsulmJ jsulm

          @gfxx You should read this: http://doc.qt.io/qt-5/objecttrees.html
          Usually in C++ you have to free the memory you allocated via new keyword at some point to avoid memory leaks. You can use smart pointers to free the memory automatically.
          Many Qt classes (all classes derived from QObject) have "parent" parameter in their constructors. If you pass a parent to a child then child will be deleted when parent is deleted, so you do not have to delete child explicitly in this case:

          Parent *parent = new Parent();
          Child * child = new Child(parent);
          ...
          delete parent; // Here child will be deleted as well
          
          gfxxG Offline
          gfxxG Offline
          gfxx
          wrote on last edited by
          #4

          @jsulm Thanks .... most of these in my code use smart pointer ..... but for shure 1 or 2 none ..... so I can simply convert it into smart pointer .... at these point my code is free of memory leaks (because it can be child of).

          I know the smart pointer usage ... anyhow I'm not expert enought so for shure I've place 1-2 new without delete .... than casually I read a post in these forum about memory leaks ... and I remember my error. But the code works so I asked.

          Thanks a lot.
          giorgio

          bkt

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved