Hello everybody. I have a very strange behaviour with QLabel and QGraphicsScene.
I develop a simple game, where 2 players how a set of soldiers and basically try to eliminate each other. This is a turn-based strategy and obviously I use QLabel to display QTimer value on a game map.
The aim of developing this game is learning of Qt, c++11 and some programming techniques.

I'm gonna highlight troubled parts with comments containing "ololo"
. So here is a code:
Qt Code:
  1. std::shared_ptr<GameManager> GameManagerCreator::createGameManager()
  2. {
  3. // ololo if remove static everything is fine.
  4. static std::shared_ptr<GameManager> gameManager(new GameManager());
  5. gameManager->init();
  6. qDebug() << "GameManagerCreator::createGameManager use count " << gameManager.use_count();
  7. return gameManager;
  8. }
To copy to clipboard, switch view to plain text mode 

This is a creation of my game logic class. As u can see, I would like to use here a singleton.

Qt Code:
  1. void GameManager::initialiseGameInterface()
  2. {
  3. mp_menuButton.reset(new QPushButton);
  4. setButtonSettings(mp_menuButton, ":/settingImages/menuButton.png");
  5. mp_scene->addWidget(mp_menuButton.get())->setZValue(1.0);
  6. const std::shared_ptr<Tile> rightTopCorner = mp_scene->tile(std::make_pair(0, mp_scene->mapSizeInTiles().second - 1));
  7. mp_menuButton->move(rightTopCorner->rect().x(), rightTopCorner->rect().y());
  8.  
  9. mp_changeTurnButton.reset(new QPushButton);
  10. setButtonSettings(mp_changeTurnButton, ":/settingImages/changeTurnButton.png");
  11. mp_scene->addWidget(mp_changeTurnButton.get())->setZValue(1.0);
  12. connect(mp_changeTurnButton.get(), SIGNAL(clicked(bool)), this, SLOT(changeTurn()));
  13.  
  14. QString settingsFolder(":/settingImages/");
  15. mp_moveBlantButton.reset(new QPushButton);
  16. setButtonSettings(mp_moveBlantButton, settingsFolder + "moveButton.png");
  17. mp_scene->addWidget(mp_moveBlantButton.get())->setZValue(1.0);
  18. connect(mp_moveBlantButton.get(), SIGNAL(clicked(bool)), this, SLOT(moveButtonClicked()));
  19.  
  20. mp_attackBlantButton.reset(new QPushButton);
  21. setButtonSettings(mp_attackBlantButton);
  22. mp_scene->addWidget(mp_attackBlantButton.get())->setZValue(1.0);
  23. connect(mp_attackBlantButton.get(), SIGNAL(clicked(bool)), this, SLOT(attackButtonClicked()));
  24.  
  25. mp_meleeAttackBlantButton.reset(new QPushButton);
  26. setButtonSettings(mp_meleeAttackBlantButton, settingsFolder + "meleeAttackButton.png");
  27. mp_scene->addWidget(mp_meleeAttackBlantButton.get())->setZValue(1.0);
  28. connect(mp_meleeAttackBlantButton.get(), SIGNAL(clicked(bool)), this, SLOT(meleeAttackButtonClicked()));
  29.  
  30. mp_timerDisplayer.reset(new QLabel);
  31. setTimerSettings();
  32. // ololo this line is leading to a crash in the destructor!!!
  33. mp_scene->addWidget(mp_timerDisplayer.get())->setZValue(1.0);
  34. placeTimerDisplayer();
  35. }
To copy to clipboard, switch view to plain text mode 

Here I initialize interface settings (mostly buttons). And look at the line
Qt Code:
  1. mp_scene->addWidget(mp_timerDisplayer.get())->setZValue(1.0);
To copy to clipboard, switch view to plain text mode 
This one leads to an exception in the destructor. I can't understand why. There is no difference between initializing buttons and this QLabel. However if I comment this line everything works perfectly except there is no QLabel on the map but no surprise here

Qt Code:
  1. void GameManager::cleanScene()
  2. {
  3. QList<QGraphicsItem*> list =mp_scene->items();
  4. for(int i=0;i<list.length();i++)
  5. mp_scene->removeItem(list[i]);
  6. }
  7.  
  8. void GameManager::clean()
  9. {
  10. cleanScene();
  11.  
  12. // cleaning other things
  13.  
  14.  
  15. if(mp_playerInCharge)
  16. mp_playerInCharge.reset();
  17. if(mp_menuButton)
  18. mp_menuButton.reset();
  19. if(mp_moveBlantButton)
  20. mp_moveBlantButton.reset();
  21. if(mp_attackBlantButton)
  22. mp_attackBlantButton.reset();
  23. if(mp_meleeAttackBlantButton)
  24. mp_meleeAttackBlantButton.reset();
  25. if(mp_changeTurnButton)
  26. mp_changeTurnButton.reset();
  27.  
  28. if(mp_turnTimer)
  29. mp_turnTimer.reset();
  30. if(mp_timerDisplayer) {
  31. //ololo crashes here
  32. mp_timerDisplayer.reset();
  33. }
  34.  
  35. if(mp_view) {
  36. mp_view.reset();
  37. }
  38. if(mp_scene) {
  39. mp_scene.reset();
  40. }
  41. }
  42.  
  43. GameManager::~GameManager()
  44. {
  45. clean();
  46. qDebug() << "gamemanager destructor";
  47. }
To copy to clipboard, switch view to plain text mode 


And here is my destructor. In case you wondering why I don't use mp_scene->clear(), that's because if I have mp_timerDisplayer it crashes. So before deleting all my interface buttons and so on, I remove them from the scene.
The most strange part that everything works perfectly if i don't use singleton. (simply remove static from
Qt Code:
  1. static std::shared_ptr<GameManager> gameManager(new GameManager());
To copy to clipboard, switch view to plain text mode 
)

Another strange thing is that if I leave static be and remove this QLabel which displays timer count down everything also works fine.

It's worth mentioning that all other code which is used with mp_timerDisplayer is strictly about styles and setting values, I don't create it anywhere else and I don't delete it anywhere else.
If it's any help I can publish it as well.
The error is access violation and bla bla bla. Looks like I delete the same object twice. First it deleted then I remove it from the scene and the second one is when std::shared_ptr counter goes to zero in mp_timerDisplayer.reset();

But you see there is no difference between button creations and freeing and this QLabel.

Any help would be much appreciated.