c++ - Pass object (that will go out of scope) by reference -


so have function:

adddatatolist(..., qvariant metadata) { } 

the relevant qvariant constructor in case is: qvariant(const qstring & val).

now have function foo(), call adddatatolist() function. how pass valid value qvariant object?

if have this:

void foo() {     qstring str = "string";     adddatatolist(..., qvariant(str)); } 

it not work because str object go out of scope, leading undefined behavior.

but if create string on heap, this:

void foo() {     qstring *str = new qstring("string");     adddatatolist(..., qvariant(*str)); } 

it work, there memory leaks (because qvariant doesn't take ownership of passed pointer).

how solve situation?

edit: actual implementation of adddatatolist(...):

void tpmsvalidator::adddatatolist(const qstring &data, bool usemetadata, messagetypes type, qvariant metadata) {     qstring text;     qicon okicon(qdir::currentpath() + "\\resources\\icons\\ok3.png");     qicon notokicon(qdir::currentpath() + "\\resources\\icons\\notok.png");     qicon noticeicon(qdir::currentpath() + "\\resources\\icons\\warning2.png");      if (data != qstring()) {         qlistwidgetitem *data_w = new qlistwidgetitem;          if (type == messagetypes::notification) {             data_w->setbackgroundcolor(qcolor(qt::globalcolor::gray));             data_w->settextcolor(qcolor(qt::globalcolor::black));             data_w->seticon(noticeicon);             text.append("notice: ");         }         else if (type == messagetypes::telegramokinfo) {             data_w->setbackgroundcolor(qcolor(qt::globalcolor::gray));             data_w->settextcolor(qcolor(qt::globalcolor::black));             data_w->seticon(okicon);             text.append("info: ");         }         else if (type == messagetypes::telegramnotokinfo) {             data_w->setbackgroundcolor(qcolor(qt::globalcolor::gray));             data_w->settextcolor(qcolor(qt::globalcolor::red));             data_w->seticon(notokicon);             text.append("info: ");         }         else if (type == messagetypes::warning) {             data_w->setbackgroundcolor(qcolor(qt::globalcolor::darkyellow));             data_w->settextcolor(qcolor(qt::globalcolor::black));             text.append("warning: ");         }         else {             data_w->setbackgroundcolor(qcolor(qt::globalcolor::darkred).light(130));             data_w->settextcolor(qcolor(qt::globalcolor::white));             data_w->seticon(notokicon);             text.append("error: ");         }          text.append(data);         data_w->settext(text);         data_w->setfont(qfont("helvetica"));          if (usemetadata) {             data_w->setdata(qt::userrole, metadata);         }          ui.listdata->additem(data_w);          if (ui.autoscrollcheckbox->ischecked()) {             ui.listdata->scrolltobottom();         }     } } 

with

adddatatolist(..., qvariant metadata) { } 

you taking qvariant value. such copy the value passed function. in

adddatatolist(..., qvariant(str)); 

you create temporary qvariant , contents of temporary copied metadata in adddatatolist(). metadata own object , not dependent on temporary created.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -