Segmentation Fault after assigning QSqlQueryModel from function return in switch statement
-
Hi and welcome to devnet,
You delete your model object but do not reset the pointer value hence if filterType is not 0 or 1, model will point to invalid memory.
-
@SGaist Hello, I am also part of the poster's group. We are testing this code in a controlled environment where we are isolating the value of filterType to only be 1. The program executes the code in case 1 of the switch statement but then crashes shortly after and never executes the code after the switch. It seems like the program is crashing inside the switch, but still executes all code in case 1.
-
Wait... why are you allocating your QSqlQuery object on the heap ? There's no reason for that.
-
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.
-
No, just create it on the stack in the method, there's no need to use new, nor std::move, nor delete.
-
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?
-
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.
-
@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!