From e536b75069b5caa8333c5609bfe05b68ba2b5653 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 17 Nov 2008 11:00:32 +0000 Subject: [PATCH] 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 --- samples/ipc/server.cpp | 16 ++++++++++------ samples/ipc/server.h | 4 ++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/samples/ipc/server.cpp b/samples/ipc/server.cpp index afe890a..4bd2635 100644 --- a/samples/ipc/server.cpp +++ b/samples/ipc/server.cpp @@ -324,25 +324,26 @@ MyConnection::OnRequest(const wxString& topic, { *size = 0; - wxString afterDate; + wxString s, + afterDate; if ( item.StartsWith("Date", &afterDate) ) { const wxDateTime now = wxDateTime::Now(); if ( afterDate.empty() ) { - m_requestData = now.Format(); + s = now.Format(); *size = wxNO_LEN; } else if ( afterDate == "+len" ) { - m_requestData = now.FormatTime() + " " + now.FormatDate(); - *size = strlen(m_requestData.mb_str()) + 1; + s = now.FormatTime() + " " + now.FormatDate(); + *size = strlen(s.mb_str()) + 1; } } else if ( item == "bytes[3]" ) { - m_requestData = "123"; + s = "123"; *size = 3; } @@ -352,7 +353,10 @@ MyConnection::OnRequest(const wxString& topic, 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); return data; } diff --git a/samples/ipc/server.h b/samples/ipc/server.h index 2395938..a81a442 100644 --- a/samples/ipc/server.h +++ b/samples/ipc/server.h @@ -78,9 +78,9 @@ public: wxString m_advise; 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 - wxString m_requestData; + wxCharBuffer m_requestData; }; class MyServer : public wxServer -- 2.7.4