Huh, I don't even know what singletons are...
Ok, defensive coding sounds good 
I tried to find the error, but still no success.
How is it possible to get the nice messages where the error happens?
I forced a NULL pointer to try it...
//model_customer = new QStandardItemModel;
...then
Q_ASSERT(model_customer);
...or
Q_CHECK_PTR(model_customer);
//model_customer = new QStandardItemModel;
...then
Q_ASSERT(model_customer);
...or
Q_CHECK_PTR(model_customer);
To copy to clipboard, switch view to plain text mode
But neither Q_ASSERT nor Q_CHECK_PTR produce an output like
ASSERT: "b == 0" in file div.cpp, line 7
Instead I get
&"warning: GDB: Failed to set controlling terminal: Invalid argument\n"
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Back to the original problem:
addresnew.h
QStandardItemModel* model;
To copy to clipboard, switch view to plain text mode
The following call for model = dmgr->customerModel(); crashes.
Disabling it and just output some text works.
addressnew.cpp
AddressNew
::AddressNew(DataManager
* dmgr,
QWidget *parent
){
Q_CHECK_PTR(dmgr);
Q_ASSERT(dmgr);
// the following function is accessible
//model = dmgr->customerModel(); //should usually assign QStandardModelItem*, crashing!
dmgr->customerModel(); //changed to test, just write QTextStream stdout. working!
}
AddressNew::AddressNew(DataManager* dmgr, QWidget *parent)
: QDialog(parent)
{
Q_CHECK_PTR(dmgr);
Q_ASSERT(dmgr);
// the following function is accessible
//model = dmgr->customerModel(); //should usually assign QStandardModelItem*, crashing!
dmgr->customerModel(); //changed to test, just write QTextStream stdout. working!
}
To copy to clipboard, switch view to plain text mode
datamanager.h
void setupModels();
QStandardItemModel *model_customer;
QStandardItemModel *customerModel();
void setupModels();
To copy to clipboard, switch view to plain text mode
datamanager.cpp
void DataManager::setupModels()
{
out << "setupModels()\n";
Q_ASSERT(model_customer);
//not crashing here
}
{
//QTextStream out(stdout);
//out << "set customerModel()\n";
Q_ASSERT(model_customer);
return model_customer; //crash here if returning model is enabled
}
void DataManager::setupModels()
{
QTextStream out(stdout);
out << "setupModels()\n";
model_customer = new QStandardItemModel;
Q_ASSERT(model_customer);
//not crashing here
}
QStandardItemModel *DataManager::customerModel()
{
//QTextStream out(stdout);
//out << "set customerModel()\n";
Q_ASSERT(model_customer);
return model_customer; //crash here if returning model is enabled
}
To copy to clipboard, switch view to plain text mode
I have no idea what causes the problem, can you help me?
Just tell if you need more code.
Bookmarks