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 to handle emit signal(dynamically allocated item that needs to be freed)
Forum Updated to NodeBB v4.3 + New Features

how to handle emit signal(dynamically allocated item that needs to be freed)

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 696 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.
  • C Offline
    C Offline
    Cyrille de Brebisson
    wrote on last edited by
    #1

    Hello,

    I have a dynamically allocated object which is a parameter passed to a signal with an emit signal(object); type code.
    How/when can I safely delete my object.

    pseudo code:
    Type *a= new Type(paramters);
    some_Processing_On_a();
    emit aHasChanged(a);

    Thanks,
    Cyrille

    jsulmJ 1 Reply Last reply
    0
    • C Cyrille de Brebisson

      Hello,

      I have a dynamically allocated object which is a parameter passed to a signal with an emit signal(object); type code.
      How/when can I safely delete my object.

      pseudo code:
      Type *a= new Type(paramters);
      some_Processing_On_a();
      emit aHasChanged(a);

      Thanks,
      Cyrille

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Cyrille-de-Brebisson Are you passing by reference, or as pointer?
      You can let the receiving slot delete it, but if you connect more than one slot then you will be in trouble.
      You should consider using smart pointers for that instead of raw pointers - smart pointer will delete the object as soon as nobody references it anymore.

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

      1 Reply Last reply
      5
      • C Offline
        C Offline
        Cyrille de Brebisson
        wrote on last edited by
        #3

        Hello,

        At the moment, I am passing my data (variable size) by address (the data is raw allocated (malloc) ram)... but I could change things if needed.

        They seem to be a VERY large number of smart pointer types in QT, any indication on which one I should use?

        Since the slot/signal system seems to do a lot of (void*) cast, I am afraid that smart pointers might not work well as they might miss a copy somewhere...

        Cyrille

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          if you are 100% sure no slot will ever be executed in a secondary thread (or executed by forcing Qt::QueuedConnection) then you can safely call delete a; just after emit aHasChanged(a);

          hey seem to be a VERY large number of smart pointer types in QT, any indication on which one I should use?

          https://doc.qt.io/qt-5/qsharedpointer.html

          I am afraid that smart pointers might not work well as they might miss a copy somewhere

          As long as they are declared with the meta-object system you don't have to worry about anything. QSharedPointer is automatically registered so no worries

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          6
          • C Offline
            C Offline
            Cyrille de Brebisson
            wrote on last edited by
            #5

            hello,

            ok, I will try it, thanks.

            cyrille

            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