really ensure that the pointer returned from MyConnection::OnRequest() remains valid

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@56816 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2008-11-17 11:00:32 +00:00
parent 48c90c6e1e
commit e536b75069
2 changed files with 12 additions and 8 deletions

View File

@@ -324,25 +324,26 @@ MyConnection::OnRequest(const wxString& topic,
{ {
*size = 0; *size = 0;
wxString afterDate; wxString s,
afterDate;
if ( item.StartsWith("Date", &afterDate) ) if ( item.StartsWith("Date", &afterDate) )
{ {
const wxDateTime now = wxDateTime::Now(); const wxDateTime now = wxDateTime::Now();
if ( afterDate.empty() ) if ( afterDate.empty() )
{ {
m_requestData = now.Format(); s = now.Format();
*size = wxNO_LEN; *size = wxNO_LEN;
} }
else if ( afterDate == "+len" ) else if ( afterDate == "+len" )
{ {
m_requestData = now.FormatTime() + " " + now.FormatDate(); s = now.FormatTime() + " " + now.FormatDate();
*size = strlen(m_requestData.mb_str()) + 1; *size = strlen(s.mb_str()) + 1;
} }
} }
else if ( item == "bytes[3]" ) else if ( item == "bytes[3]" )
{ {
m_requestData = "123"; s = "123";
*size = 3; *size = 3;
} }
@@ -352,7 +353,10 @@ MyConnection::OnRequest(const wxString& topic,
return NULL; return NULL;
} }
const void * const data = m_requestData.mb_str(); // store the data pointer to which we return in a member variable to ensure
// that the pointer remains valid even after we return
m_requestData = s.mb_str();
const void * const data = m_requestData;
Log("OnRequest", topic, item, data, *size, format); Log("OnRequest", topic, item, data, *size, format);
return data; return data;
} }

View File

@@ -78,9 +78,9 @@ public:
wxString m_advise; wxString m_advise;
protected: protected:
// the data returned by last OnRequest(): we keep it in this wxString to // the data returned by last OnRequest(): we keep it in this buffer to
// ensure that the pointer we return from OnRequest() stays valid // ensure that the pointer we return from OnRequest() stays valid
wxString m_requestData; wxCharBuffer m_requestData;
}; };
class MyServer : public wxServer class MyServer : public wxServer