Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Memory can not release correctly because of using lambda as slot
Qt 6.11 is out! See what's new in the release blog

Memory can not release correctly because of using lambda as slot

Scheduled Pinned Locked Moved Solved Qt for Python
16 Posts 5 Posters 3.1k 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.
  • JoeCFDJ JoeCFD

    @JonB But without lambdas it can be hard e.g. to pass parameters. <== what do you mean? A slot can do the same things as in a lambda func, right?

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #6

    @JoeCFD
    Parameters from the caller (of the connect()), e.g. the common:

    for i in range(10):
        self.buttons[i].clicked.connect(lambda ii=i: print(ii))
    
    1 Reply Last reply
    0
    • JoeCFDJ JoeCFD

      @JonB But without lambdas it can be hard e.g. to pass parameters. <== what do you mean? A slot can do the same things as in a lambda func, right?

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

      @JoeCFD said in Memory can not release correctly because of using lambda as slot:

      what do you mean? A slot can do the same things as in a lambda func, right?

      If types in signal and slot do not match you cant connect or pass directly... with lambda you can modify your received data and process it further.


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

      ~E. W. Dijkstra

      JoeCFDJ 1 Reply Last reply
      1
      • Pl45m4P Pl45m4

        @JoeCFD said in Memory can not release correctly because of using lambda as slot:

        what do you mean? A slot can do the same things as in a lambda func, right?

        If types in signal and slot do not match you cant connect or pass directly... with lambda you can modify your received data and process it further.

        JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by
        #8

        @Pl45m4 Slot is defined by yourself and you can match them without issues. Using lambda as slot may cause crash when the widget is destroyed. This happened in my app.

        JonBJ 1 Reply Last reply
        0
        • JoeCFDJ JoeCFD

          @Pl45m4 Slot is defined by yourself and you can match them without issues. Using lambda as slot may cause crash when the widget is destroyed. This happened in my app.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #9

          @JoeCFD said in Memory can not release correctly because of using lambda as slot:

          Using lambda as slot may cause crash when the widget is destroyed. This happened in my app.

          You might choose to raise your own topic for this, with example. Ooi, Python (PySide? PyQt?) or C++?

          JoeCFDJ 1 Reply Last reply
          0
          • JonBJ JonB

            @JoeCFD said in Memory can not release correctly because of using lambda as slot:

            Using lambda as slot may cause crash when the widget is destroyed. This happened in my app.

            You might choose to raise your own topic for this, with example. Ooi, Python (PySide? PyQt?) or C++?

            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by
            #10

            @JonB C++. For example, if the signal comes from another class outside a widget, the signal is connected to a lambda func in the widget. When the widget is destroyed, the lambda may still be attached to that signal. When the signal comes, the lambda tries to process the data of the widget. The app will crash now since the data of the widget does not exist anymore.

            JonBJ 1 Reply Last reply
            0
            • JoeCFDJ JoeCFD

              @JonB C++. For example, if the signal comes from another class outside a widget, the signal is connected to a lambda func in the widget. When the widget is destroyed, the lambda may still be attached to that signal. When the signal comes, the lambda tries to process the data of the widget. The app will crash now since the data of the widget does not exist anymore.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #11

              @JoeCFD
              I'd be interested to see your connect() statement. If a QObject is the slot object that should remove it as slot from all signals when destroyed, shouldn't it?

              MyWidget::MyWidget()
              {
                  connect(externalObject, &ExternalSignal, this, [] () { });
              }
              

              Doesn't that get disconnected during ~MyWidget()?

              JoeCFDJ jeremy_kJ 2 Replies Last reply
              0
              • JonBJ JonB

                @JoeCFD
                I'd be interested to see your connect() statement. If a QObject is the slot object that should remove it as slot from all signals when destroyed, shouldn't it?

                MyWidget::MyWidget()
                {
                    connect(externalObject, &ExternalSignal, this, [] () { });
                }
                

                Doesn't that get disconnected during ~MyWidget()?

                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by JoeCFD
                #12

                @JonB I did not do anything to disconnect in the destructor when lambda was used. I guess the disconnection was not done quickly enough while there are a lot of things going on in the code. Now I do disconnection manually even with slot in ~MyWidget() to avoid crash.

                1 Reply Last reply
                0
                • JonBJ JonB

                  @JoeCFD
                  I'd be interested to see your connect() statement. If a QObject is the slot object that should remove it as slot from all signals when destroyed, shouldn't it?

                  MyWidget::MyWidget()
                  {
                      connect(externalObject, &ExternalSignal, this, [] () { });
                  }
                  

                  Doesn't that get disconnected during ~MyWidget()?

                  jeremy_kJ Offline
                  jeremy_kJ Offline
                  jeremy_k
                  wrote on last edited by
                  #13

                  @JonB said in Memory can not release correctly because of using lambda as slot:

                  @JoeCFD
                  I'd be interested to see your connect() statement. If a QObject is the slot object that should remove it as slot from all signals when destroyed, shouldn't it?

                  MyWidget::MyWidget()
                  {
                      connect(externalObject, &ExternalSignal, this, [] () { });
                  }
                  

                  Doesn't that get disconnected during ~MyWidget()?

                  The problem is that neither PyQt nor PySide support using a context object. This removes two important properties for using lambda functions:

                  • The ability to specify which thread the slot should run in
                  • The ability to automatically disconnect when an indirect receiver is destroyed

                  Asking a question about code? http://eel.is/iso-c++/testcase/

                  JonBJ 1 Reply Last reply
                  1
                  • jeremy_kJ jeremy_k

                    @JonB said in Memory can not release correctly because of using lambda as slot:

                    @JoeCFD
                    I'd be interested to see your connect() statement. If a QObject is the slot object that should remove it as slot from all signals when destroyed, shouldn't it?

                    MyWidget::MyWidget()
                    {
                        connect(externalObject, &ExternalSignal, this, [] () { });
                    }
                    

                    Doesn't that get disconnected during ~MyWidget()?

                    The problem is that neither PyQt nor PySide support using a context object. This removes two important properties for using lambda functions:

                    • The ability to specify which thread the slot should run in
                    • The ability to automatically disconnect when an indirect receiver is destroyed
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #14

                    @jeremy_k said in Memory can not release correctly because of using lambda as slot:

                    The problem is that neither PyQt nor PySide support using a context object. This removes two important properties for using lambda functions:

                    I asked whether @JoeCFD was talking about Python or C++ and he said C++.

                    jeremy_kJ 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @jeremy_k said in Memory can not release correctly because of using lambda as slot:

                      The problem is that neither PyQt nor PySide support using a context object. This removes two important properties for using lambda functions:

                      I asked whether @JoeCFD was talking about Python or C++ and he said C++.

                      jeremy_kJ Offline
                      jeremy_kJ Offline
                      jeremy_k
                      wrote on last edited by
                      #15

                      @JonB said in Memory can not release correctly because of using lambda as slot:

                      @jeremy_k said in Memory can not release correctly because of using lambda as slot:

                      The problem is that neither PyQt nor PySide support using a context object. This removes two important properties for using lambda functions:

                      I asked whether @JoeCFD was talking about Python or C++ and he said C++.

                      That's clear. I was being expedient in responding to one of your previous comments.

                      @JonB said in Memory can not release correctly because of using lambda as slot:

                      @JoeCFD
                      But without lambdas it can be hard e.g. to pass parameters.

                      Asking a question about code? http://eel.is/iso-c++/testcase/

                      1 Reply Last reply
                      1
                      • JonBJ JonB

                        @JoeCFD
                        But without lambdas it can be hard e.g. to pass parameters.

                        @feiyuhuahuo
                        I said I think this is expected behaviour from Python, if it's a problem for you you need to work around if it's a deal-breaker.
                        Does executing self.action_bilinear.triggered.disconnect() recover the memory? That would disconnect all slots, hopefully including lambdas?

                        feiyuhuahuoF Offline
                        feiyuhuahuoF Offline
                        feiyuhuahuo
                        wrote on last edited by
                        #16

                        @JonB
                        I add this self.action_bilinear.triggered.disconnect() in closeEvent, but just doesn't work.

                        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