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. Segmentation Fault after assigning QSqlQueryModel from function return in switch statement

Segmentation Fault after assigning QSqlQueryModel from function return in switch statement

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++sqlqsqlqueryqsqlquerymodelsigsegv
14 Posts 6 Posters 1.6k Views
  • 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.
  • S Offline
    S Offline
    SGaist
    Lifetime Qt Champion
    wrote on 3 Mar 2022, 21:04 last edited by
    #5

    Wait... why are you allocating your QSqlQuery object on the heap ? There's no reason for that.

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

    R 1 Reply Last reply 3 Mar 2022, 21:09
    1
    • S SGaist
      3 Mar 2022, 21:04

      Wait... why are you allocating your QSqlQuery object on the heap ? There's no reason for that.

      R Offline
      R Offline
      RyanSolanki
      wrote on 3 Mar 2022, 21:09 last edited by
      #6

      @SGaist What would you suggest we do to fix that? Sorry we are relatively new to Qt and are still learning

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 3 Mar 2022, 21:13 last edited by
        #7

        Just allocate it on the stack like all the examples show.

        When you read a method signature that uses const references, it's usually not a good idea to create an object on the heap, only to de reference it to pass it to that method.

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

        R 1 Reply Last reply 3 Mar 2022, 21:20
        0
        • S SGaist
          3 Mar 2022, 21:13

          Just allocate it on the stack like all the examples show.

          When you read a method signature that uses const references, it's usually not a good idea to create an object on the heap, only to de reference it to pass it to that method.

          R Offline
          R Offline
          RyanSolanki
          wrote on 3 Mar 2022, 21:20 last edited by
          #8

          @SGaist So create the QSqlQuery as a private member of the class rather than in the appliedSort() method?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 3 Mar 2022, 21:21 last edited by
            #9

            No, just create it on the stack in the method, there's no need to use new, nor std::move, nor delete.

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

            R 1 Reply Last reply 3 Mar 2022, 21:35
            1
            • S SGaist
              3 Mar 2022, 21:21

              No, just create it on the stack in the method, there's no need to use new, nor std::move, nor delete.

              R Offline
              R Offline
              RyanSolanki
              wrote on 3 Mar 2022, 21:35 last edited by
              #10

              @SGaist Could that be what is causing the segmentation fault?

              1 Reply Last reply
              0
              • D Offline
                D Offline
                DeShark
                wrote on 3 Mar 2022, 23:02 last edited by
                #11

                It looks like the model has already been deleted when the MainWindow's destructor tries to delete it again and that causes a segfault for what should be obvious reasons.

                From the stack trace the SortLeadActor's destructor is called, and then the MainWindow destructor is called. Are you calling delete on the model that you're returning to MainWindow from appliedSort during ~SortLeadActor?

                P R 2 Replies Last reply 3 Mar 2022, 23:19
                2
                • D DeShark
                  3 Mar 2022, 23:02

                  It looks like the model has already been deleted when the MainWindow's destructor tries to delete it again and that causes a segfault for what should be obvious reasons.

                  From the stack trace the SortLeadActor's destructor is called, and then the MainWindow destructor is called. Are you calling delete on the model that you're returning to MainWindow from appliedSort during ~SortLeadActor?

                  P Offline
                  P Offline
                  Pl45m4
                  wrote on 3 Mar 2022, 23:19 last edited by
                  #12

                  @DeShark

                  Yeah, that would also explain why they are facing the segfault not frequently... If you don't press that button which is connected to the slot where they delete the model, the crash probably won't occur.

                  Anyway, follow @SGaist 's instructions according to the query and in addition, don't try to double delete the pointer.


                  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
                  0
                  • D DeShark
                    3 Mar 2022, 23:02

                    It looks like the model has already been deleted when the MainWindow's destructor tries to delete it again and that causes a segfault for what should be obvious reasons.

                    From the stack trace the SortLeadActor's destructor is called, and then the MainWindow destructor is called. Are you calling delete on the model that you're returning to MainWindow from appliedSort during ~SortLeadActor?

                    R Offline
                    R Offline
                    RyanSolanki
                    wrote on 4 Mar 2022, 00:03 last edited by
                    #13

                    @DeShark Thank you! The double delete was the issue and I have now fixed it

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      DeShark
                      wrote on 4 Mar 2022, 00:50 last edited by
                      #14

                      @RyanSolanki Please mark as solved if you are happy the issue is resolved. Also consider reading up further about Qt's memory management model (maybe it would make sense to pass a parent during the QSqlQueryModel's construction to avoid all that manual memory management or else perhaps smart pointers if there is no clear parent to assign ownership to).

                      Furthermore, SGaist's point about passing a pointer to a function whose signature expects a const reference cannot be understated. If the function expects a pointer, give it a pointer. If it expects a const reference, give it an object created on the stack! https://doc.qt.io/qt-5/qsqlquerymodel.html#details
                      The documentation is good; use it! It is unclear to me why you are using functions such as "clear" for the model. The setQuery() function already clears the model. Why mess around with new and delete several times when you can just pass in a new query to modify the existing model? Futhermore, why bother clear()-ing the model immediately before you delete it? The destructor is called during the delete, which itself frees up any resources, per the documentation.

                      There are deeper problems of comprehension going on here!

                      1 Reply Last reply
                      0

                      14/14

                      4 Mar 2022, 00:50

                      • Login

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