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. How do you keep a class from deleting an argument passed in by reference?
Qt 6.11 is out! See what's new in the release blog

How do you keep a class from deleting an argument passed in by reference?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 1.6k Views 2 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.
  • F Offline
    F Offline
    Flesh
    wrote on last edited by
    #1

    I ran into a problem while working with the Simple Browser example, I wanted to move the popup dialog for downloads, into a tab, so I passed the reference to the DownloadWidget defined at the parent level, by reference to the Tab Widget as context to the tab, but when closing the app, the TabWidget tries to delete the reference, but the owner of it already deleted it, so its an invalid pointer, and why it crashes, but I have no idea how to prevent this, I have spent a week trying to figure this out, I thought about writing a smaller demo, but found its the interaction between all the classes and the order they delete things in that causes this issue, and this is in fact a Qt Example that some people might be familiar with.

    I published this project on GitHub
    https://github.com/Light-Wizzard/QtWebBrowser
    The read me gives all the details and the code is there.

    Thanks, Flesh.

    Jeffrey Scott Flesher PhD
    http//LightWizzard.com/

    Pl45m4P 1 Reply Last reply
    0
    • F Flesh

      I ran into a problem while working with the Simple Browser example, I wanted to move the popup dialog for downloads, into a tab, so I passed the reference to the DownloadWidget defined at the parent level, by reference to the Tab Widget as context to the tab, but when closing the app, the TabWidget tries to delete the reference, but the owner of it already deleted it, so its an invalid pointer, and why it crashes, but I have no idea how to prevent this, I have spent a week trying to figure this out, I thought about writing a smaller demo, but found its the interaction between all the classes and the order they delete things in that causes this issue, and this is in fact a Qt Example that some people might be familiar with.

      I published this project on GitHub
      https://github.com/Light-Wizzard/QtWebBrowser
      The read me gives all the details and the code is there.

      Thanks, Flesh.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @Flesh

      Short answer: You can't (prevent the TabWidget from deleting its pages / childs)

      You add your widget to your tab with addTab(...). The ownership of your DownloadManagerWidget went to your QTabWidget and it will be cleaned when the tabWidget is also destroyed
      (-> QObject - Parent-Child-ObjectTree)

      • https://doc.qt.io/qt-5/objecttrees.html#construction-destruction-order-of-qobjects

      If you delete your DownloadManagerWidget elsewhere, you will face some crashes and unwanted behavior (like you probably do, at the moment).

      Edit:

      Haven't seen all of your code... do you delete your widget manually in your BrowserWindow?
      It's risky to just pass pointers to widgets around and having them laying around.

      You might want to have a look at QSharedPointer
      (dunno whether it helps in your case. Maybe you have to re-organize your widgets)

      • https://doc.qt.io/qt-5/qsharedpointer.html#QSharedPointer-2

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      Christian EhrlicherC 1 Reply Last reply
      0
      • Pl45m4P Pl45m4

        @Flesh

        Short answer: You can't (prevent the TabWidget from deleting its pages / childs)

        You add your widget to your tab with addTab(...). The ownership of your DownloadManagerWidget went to your QTabWidget and it will be cleaned when the tabWidget is also destroyed
        (-> QObject - Parent-Child-ObjectTree)

        • https://doc.qt.io/qt-5/objecttrees.html#construction-destruction-order-of-qobjects

        If you delete your DownloadManagerWidget elsewhere, you will face some crashes and unwanted behavior (like you probably do, at the moment).

        Edit:

        Haven't seen all of your code... do you delete your widget manually in your BrowserWindow?
        It's risky to just pass pointers to widgets around and having them laying around.

        You might want to have a look at QSharedPointer
        (dunno whether it helps in your case. Maybe you have to re-organize your widgets)

        • https://doc.qt.io/qt-5/qsharedpointer.html#QSharedPointer-2
        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Pl45m4 said in How do you keep a class from deleting an argument passed in by reference?:

        You might want to have a look at QSharedPointer

        Don't use QSharedPointer with QObjects!

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        Pl45m4P 1 Reply Last reply
        2
        • Christian EhrlicherC Christian Ehrlicher

          @Pl45m4 said in How do you keep a class from deleting an argument passed in by reference?:

          You might want to have a look at QSharedPointer

          Don't use QSharedPointer with QObjects!

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4

          @Christian-Ehrlicher

          Hm I thought it's possible.

          What about this example from the documentation?
          Isn't MyObject a QObject there?!

              QSharedPointer<MyObject> obj =
                  QSharedPointer<MyObject>(new MyObject, &QObject::deleteLater);
          
          

          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          1
          • F Offline
            F Offline
            Flesh
            wrote on last edited by
            #5

            That worked, thanks.

            Jeffrey Scott Flesher PhD
            http//LightWizzard.com/

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SimonSchroeder
              wrote on last edited by
              #6

              @Christian-Ehrlicher is right that you should avoid (in general at least) to use a shared pointer with QObject. Try to always use the parent to manage the lifetime of QObjects. Once you start mixing the two you might easily provoke a double free. Be aware that Qt also automatically sets parents in some cases! Whenever you place a widget into a layout it will be reparented. It is much easier to avoid shared pointers with QObject than to try to figure out all the corner cases.

              1 Reply Last reply
              1

              • Login

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