filehistory.cpp returning reference to temporary

I’m getting a compiler warning like this:

src\widgets\filehistory.cpp(92): warning C4172: returning address of local variable or temporary

which is the second return statement in GetHistoryFile():

const wxString &FileHistory::GetHistoryFile(size_t i) const
   wxASSERT(i < mHistory.GetCount());

   if (i < mHistory.GetCount()) {
      return mHistory[i];

   return wxEmptyString;

where wxEmptyString is:

extern WXDLLIMPEXP_DATA_BASE(const wxChar*) wxEmptyString;

Since wxEmptyString isn’t a wxString, a temporary is constructed and returned by reference. That temporary is destroyed when GetHistoryFile() returns.

This is probably not a good idea.

Thanks - this does occur in VS2013 so I reported it on audacity-devel:


The assertion tells us that we shouldn’t ever take that path in the code, but even so there should be some correct defensive code there.

So it chould be changed to

static const wxString empty;
return empty;

Thanks for the report.

I pushed a fix. Thank you again for pointing this out.