Unsolved TabWidget tabs, each with a model TableView. Crashing switching between tabs.
-
Yeah
Akismet.com can be a little flower sometimes but i hope it catches some real spam from time to time.
The syntax for accessing members depending on the allocation method.
http://net-informations.com/faq/net/stack-heap.htm
When you do
ClassX a; // stack. its a local variable. meaning it dies with the scope its in. function/ class
To access its member, you use var.xxx ( dot )
ClassX *a = new ClassX(); // heap. variable now points to memory and lives until delete is called on it
To access its member, you use var->xxx ( -> )When you have first type allocation, one should always be vary if passing to another class.
Especially if a & a needed ( take address )
but new (heap) allocation is often need for objects that we need to live longer than where they are allocated.
Hope this clears it up a bit. -
This post is deleted! -
This post is deleted! -
@mrjj
Reply Part 2
animalsearch.cppincludes section //akismet didn't like my includes...ugh AnimalSearch:: AnimalSearch (QWidget *parent) : QDialog(parent), ui(new Ui::AnimalSearch) { ui->setupUi(this); sqlconnect PreviousSearch; QString uname = qgetenv ("USER"); if (uname.isEmpty()) uname = qgetenv("USERNAME"); QString GetComplete = "long query to get previous searches using the currently logged on user"; this->PreviousSearchModel = new QSqlQueryModel(); Previous Search Model = PreviousSearch. QueryDB (GetComplete); ui->Previous Search Table View->set Model(Previous Search Model); } AnimalSearch:~AnimalSearch() { delete ui; } void AnimalSearch::on_OkButton_clicked() { close(); } void AnimalSearch::onSearchButton_clicked() { sqlconnect CurrentSearch; QString SearchQuery = "Hard coded query to search for box turtles"; this->SearchModel = new QSqlQueryModel(); SearchModel = CurrentSearch.QueryDB(SearchQuery); ui->SearchTableView->setModel(SearchModel); }
-
Reply Part 3
mainwindow.cpp
include mainwindow.h include ui_mainwindow.h include animalsearch.h //trust me the syntax is correct in the program. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { AnimalSearch animalsearch; animalsearch.setModal(true); animalsearch.exec(); //This is line 21. According to debugger this is where the crash occurs. }
Now, when this code is ran the previous search tabs QTableView is populated with previous searches.
I can click between the current search tab and the previous search tab without fail as many times as I'd like.
If i click the search button, which is hard coded to search for a box turtle the search tab QTableView populates with box turtle results. Cool good stuff.
But, when i click back on previous search tab i have a crash. I am shown the same error message as before. Exception at 0x7ffa38f1c1ea, code: 0xc0000005: read access violation at: 0x0, flgs=0x0 (first chance).
The program stops at line 21 of the mainwindow in the pushbutton slot that launches the animalsearch ui.I hope I did not complicate things moving away from the originally posted code.
Again thanks for your time.
-
Hi
I think you going to need to use the debugger and put break point in button, and single step into creation and showing animalsearch. until you find the line that makes it crash.I cant see/guess guess from the source, maybe others can guess the reason for the crash.
It really sounds like a dangling pointer :)
-
Ok. I will do just that. After reading your link from one of your previous posts about stack and heap, but still not completely understanding, i watched this guys videos, ReelLearning. I think I have a good grasp of what I should be looking for now. I will spend the day using debugging to try and solve the problem.
This video was very informative.
Pointers and Dynamic Memory in C++ (Memory Management)Good video as well.
Introduction to Pointers in C++, Tutorial on Pointers, C++ PointersInteresting observation. The more I learn about c++/Qt the more I realize I don't know much:)
Thanks for the guidance.
-
Hi
if you stil have doubts about stack vs heap, please ask/post
description of that still needs more info.
As its a critical part of C++ so one has to know when to use
heap in the cases where it will be ultra important.- Interesting observation. The more I learn about c++/Qt the more I realize I don't know much:)
Heh yep. I get that feeling from models and views in Qt :)
- Interesting observation. The more I learn about c++/Qt the more I realize I don't know much:)
-
Hello,
Isn't it just because you re-declare your proxy model in the constructor body of AnimalSearch ?
QSortFilterProxyModel *SearchHistorySortProxyModel = new QSortFilterProxyModel(this);
I think it shadows your member declared in the header as you have used the same name.
This should work better
this->SearchHistorySortProxyModel = new QSortFilterProxyModel(this);
-
@Gojir4
I will try that out when i switch back to the master branch. Currently I'm working on a new branch where i cut all the fat of the program. now it is simple and easy to understand.Also i noticed something while using the dbugger today. The error happens in qsql_odbc.cpp Unfortunately I don't see the file code i see the assembly code when the error pops up. So it looks Greek to me. Any suggestions?
-
@Core2 said in TabWidget tabs, each with a model TableView. Crashing switching between tabs.:
The error happens in qsql_odbc.cpp
Just check which line of your code was executed just before qsql_odbc.cpp
And did you fix the issue @Gojir4 pointed out? -
No I have not implemented the changes suggested by Gojir4 because I moved away from that code. At this time I'm not using a proxy model to simplify things.