vieraci
9th August 2009, 16:27
I'm having some trouble with a singleton class where I create a QSqlQuery object, execute it and access the results from anywhere in the app from an instance.
It works except in one place it crashes the app.
Header file:
class GlobalDataInstance
{
public:
static GlobalDataInstance* Instance();
QStringList getCatItems(int cat_code, int start, int end, bool concatentReturnCode);
bool refreshCatCodes();
protected:
GlobalDataInstance();
GlobalDataInstance(const GlobalDataInstance&);
GlobalDataInstance& operator= (const GlobalDataInstance&);
private:
static GlobalDataInstance* pinstance;
// Can't make categoryCodes static, it doesn't compile, or it crashes immediately.
QSqlQuery categoryCodes;
};
This method called in Main() after I create the first instance:
bool GlobalDataInstance::refreshCatCodes()
{
categoryCodes.exec("SELECT cat_code, cat_text, return_value FROM global_cat_codes order by cat_code, return_value");
return true;
}
Then when I open a window I get an instance pointer and call this method to fill some comboboxes from various places in the query:
QStringList GlobalDataInstance::getCatItems(int cat_code, int start, int end, bool concatentReturnCode)
{
QStringList list;
list.clear();
categoryCodes.first();
while (categoryCodes.next())
{
int catCode = categoryCodes.value(0).toInt();
if (catCode > cat_code)
break;
if (catCode == cat_code)
{
QString oper = categoryCodes.value(2).toString();
if (oper.toInt() >= start)
{
if (oper.toInt() > end)
break;
if (concatentReturnCode)
list << oper + " " + categoryCodes.value(1).toString();
else
list << categoryCodes.value(1).toString();
}
}
}
return list;
}
From that window, I open another and again do the same thing, but from there it crashes.
In debug it appears that categoryCodes dissapears from scope and I don't see how.
I tried to change QSqlQuery to a pointer and created it on the heap in the ctr, but it crashes immediately. What am I dong wrong ?
It works except in one place it crashes the app.
Header file:
class GlobalDataInstance
{
public:
static GlobalDataInstance* Instance();
QStringList getCatItems(int cat_code, int start, int end, bool concatentReturnCode);
bool refreshCatCodes();
protected:
GlobalDataInstance();
GlobalDataInstance(const GlobalDataInstance&);
GlobalDataInstance& operator= (const GlobalDataInstance&);
private:
static GlobalDataInstance* pinstance;
// Can't make categoryCodes static, it doesn't compile, or it crashes immediately.
QSqlQuery categoryCodes;
};
This method called in Main() after I create the first instance:
bool GlobalDataInstance::refreshCatCodes()
{
categoryCodes.exec("SELECT cat_code, cat_text, return_value FROM global_cat_codes order by cat_code, return_value");
return true;
}
Then when I open a window I get an instance pointer and call this method to fill some comboboxes from various places in the query:
QStringList GlobalDataInstance::getCatItems(int cat_code, int start, int end, bool concatentReturnCode)
{
QStringList list;
list.clear();
categoryCodes.first();
while (categoryCodes.next())
{
int catCode = categoryCodes.value(0).toInt();
if (catCode > cat_code)
break;
if (catCode == cat_code)
{
QString oper = categoryCodes.value(2).toString();
if (oper.toInt() >= start)
{
if (oper.toInt() > end)
break;
if (concatentReturnCode)
list << oper + " " + categoryCodes.value(1).toString();
else
list << categoryCodes.value(1).toString();
}
}
}
return list;
}
From that window, I open another and again do the same thing, but from there it crashes.
In debug it appears that categoryCodes dissapears from scope and I don't see how.
I tried to change QSqlQuery to a pointer and created it on the heap in the ctr, but it crashes immediately. What am I dong wrong ?