c++ - would `ui.listView->setModel(new QStringListModel(list));` result in a memory leak -


hi in process of learning qt. using following code

qstringlist list; list << "item1" << "item2" << "item3" << "item4" << "item5"; ui.listview->setmodel(new qstringlistmodel(list)); 

now understanding ui.listview->setmodel being passed reference object on heap. wouldn't object need deleted ? suggestions should using boost safe pointer here ? since every time update content of list have call last statement update display.

it hard tell little code available, yes creating object on heap address might lose , risk potential memory leak. should define either global pointer object , delete when need new one.
better create small class containing reference current qstringlistmodel , define methods create new one, while deleting old one.

class listmodelholder {   private:     qstringlistmodel* model;   public:     listmodelholder() { model = 0; }     ~listmodelholder() { if(model != 0) delete model; }      set_model(qstringlistmodel* model) { this->model = model; }     delete_model() { if(this->model) delete this->model; this->model = 0; }     }; 

just example. can keep object @ global scope or global need.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -