Results 1 to 3 of 3

Thread: Object instance in a singleton class dissapears

  1. #1
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Object instance in a singleton class dissapears

    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:
    Qt Code:
    1. class GlobalDataInstance
    2. {
    3. public:
    4. static GlobalDataInstance* Instance();
    5. QStringList getCatItems(int cat_code, int start, int end, bool concatentReturnCode);
    6. bool refreshCatCodes();
    7. protected:
    8. GlobalDataInstance();
    9. GlobalDataInstance(const GlobalDataInstance&);
    10. GlobalDataInstance& operator= (const GlobalDataInstance&);
    11. private:
    12. static GlobalDataInstance* pinstance;
    13. // Can't make categoryCodes static, it doesn't compile, or it crashes immediately.
    14. QSqlQuery categoryCodes;
    15. };
    To copy to clipboard, switch view to plain text mode 


    This method called in Main() after I create the first instance:
    Qt Code:
    1. bool GlobalDataInstance::refreshCatCodes()
    2. {
    3. categoryCodes.exec("SELECT cat_code, cat_text, return_value FROM global_cat_codes order by cat_code, return_value");
    4. return true;
    5. }
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. QStringList GlobalDataInstance::getCatItems(int cat_code, int start, int end, bool concatentReturnCode)
    2. {
    3. list.clear();
    4. categoryCodes.first();
    5. while (categoryCodes.next())
    6. {
    7. int catCode = categoryCodes.value(0).toInt();
    8. if (catCode > cat_code)
    9. break;
    10. if (catCode == cat_code)
    11. {
    12. QString oper = categoryCodes.value(2).toString();
    13. if (oper.toInt() >= start)
    14. {
    15. if (oper.toInt() > end)
    16. break;
    17. if (concatentReturnCode)
    18. list << oper + " " + categoryCodes.value(1).toString();
    19. else
    20. list << categoryCodes.value(1).toString();
    21. }
    22. }
    23. }
    24. return list;
    25. }
    To copy to clipboard, switch view to plain text mode 

    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 ?

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Object instance in a singleton class dissapears

    show us your Instance(), please

  3. #3
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Object instance in a singleton class dissapears

    Oh !! I see what I did, I was calling initForm before creating the instance.
    I should stop working when I get tired


    In ctor:

    Qt Code:
    1. initForm();
    2. globalData = GlobalDataInstance::Instance();
    To copy to clipboard, switch view to plain text mode 

    In use:
    Qt Code:
    1. void AddressDialog::initForm()
    2. {
    3. m_ui->cbAddressType->addItems(globalData->getCatItems(1,1,10,false));
    4. m_ui->cbAddressType->installEventFilter(this);
    5. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 3
    Last Post: 7th May 2008, 11:33
  2. Replies: 4
    Last Post: 26th June 2007, 19:19
  3. Replies: 3
    Last Post: 16th May 2007, 11:07
  4. Creating object of other class in Run() method
    By santosh.kumar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 15:05
  5. Replies: 2
    Last Post: 8th October 2006, 16:49

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.