filehistory.cpp returning reference to temporary

Building and customizing Audacity from the source code.

If you require help using Audacity, please post on the forum board relevant to your operating system:
Windows
Mac OS X
GNU/Linux and Unix-like

filehistory.cpp returning reference to temporary

Permanent link to this post Posted by henric » Sat May 07, 2016 4:29 am

I'm getting a compiler warning like this:
Code: Select all
src\widgets\filehistory.cpp(92): warning C4172: returning address of local variable or temporary

which is the second return statement in GetHistoryFile():
Code: Select all
const wxString &FileHistory::GetHistoryFile(size_t i) const
{
   wxASSERT(i < mHistory.GetCount());

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

   return wxEmptyString;
}

where wxEmptyString is:
Code: Select all
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.
henric
 
Posts: 28
Joined: Sun Jan 17, 2016 9:12 pm
Operating System: Windows 10

Re: filehistory.cpp returning reference to temporary

Permanent link to this post Posted by Gale Andrews » Mon May 09, 2016 12:44 pm

Thanks - this does occur in VS2013 so I reported it on audacity-devel: http://audacity.238276.n2.nabble.com/Compiler-warning-in-FileHistory-cpp-td7573891.html.

Gale
________________________________________FOR INSTANT HELP: (Click on Link below)
* * * * * Tips * * * * * Tutorials * * * * * Quick Start Guide * * * * * Audacity Manual
Gale Andrews
Quality Assurance
 
Posts: 25825
Joined: Fri Jul 27, 2007 12:02 am
Operating System: Windows 10

Re: filehistory.cpp returning reference to temporary

Permanent link to this post Posted by Paul L » Mon May 09, 2016 1:30 pm

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

Code: Select all
static const wxString empty;
return empty;


Thanks for the report.
Paul L
 
Posts: 855
Joined: Mon Mar 11, 2013 7:37 pm
Operating System: Please select

Re: filehistory.cpp returning reference to temporary

Permanent link to this post Posted by Paul L » Mon May 09, 2016 2:40 pm

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

PRL
Paul L
 
Posts: 855
Joined: Mon Mar 11, 2013 7:37 pm
Operating System: Please select


Return to Compiling Audacity



Who is online

Users browsing this forum: No registered users and 1 guest