Answered by:
Deep copy pointer to a vector

Question
-
This question might sound silly, but its been a long time since I worked in VC++. Please let me know what I am doing is correct or not.
I have a thread method which has a pointer to a vector of CString (std::vector<CString> * ). I need this pointer to the vector to update an OCX control and based on the values in the vector the control UI updates. Since it updates the UI I need to do it in the UI thread and not inside the thread method. So I would be posting a message within the ThreadMethod and update the control. It looks something like this,
UINT ThreadMethod(LPVOID params)
{
***
std::vector<CString>* data;
GetData(data);
if(data != NULL)
{
PostMessage(WM_CUSTOM_MESSAGE, (WPARAM)data, NULL);
}
***
}
//WM_CUSTOM_MESSAGE handler
LRESULT UpdateControl(WPARAM wpm, LPARAM lpm)
{
std::vector<CString>* data = (std::vector<CString>* )(wpm); //This leaks
controlIns.UpdateControl((VARIANT*) data);
}
The data will have over 100 CString and there could be 1000s of such vector. While running the code I found that the consumer method leaks. So Changed it into an auto pointer. In the consumer method I changed it into an auto pointer. I don't know if it will still solve the leak, can you help me identify if I am doing the right thing?
LRESULT UpdateControl(WPARAM wpm, LPARAM lpm)
{
auto_ptr<std::vector<CString>> data ((std::vector<CString>* )(wpm)); //I Want To Know If this solves the leak
controlIns.UpdateControl((VARIANT*) data.get());
}
Please enlighten me.
-------------kings--------------
Friday, August 23, 2013 2:23 PM
Answers
-
LRESULT UpdateControl(WPARAM wpm, LPARAM lpm) { // get the WPARAM ionto a pointer std::vector<CString> *dataptr = reinterpret_cast<std::vector<CString> *>(wpm); // make a copy of the data (don't see any real reason to make a copy in your example though) std::vector<CString> data = *dataptr; delete dataptr; // I cannot imagine this possibly works right. There // is no way a vector<CString> can possibly be correctly // used as a VARIANT controlIns.UpdateControl((VARIANT*) &data); }
Some other notes:
Neither my solution or yours will work if PostMessage fails. There needs to be a check on PostMessage and cleanup there if the post fails.
- Proposed as answer by May Wang - MSFT Monday, August 26, 2013 8:31 AM
- Marked as answer by May Wang - MSFT Friday, August 30, 2013 2:13 AM
Friday, August 23, 2013 8:18 PM
All replies
-
std::vector<CString>* data;
This looks suspicious. What is GetData() expected to do? Is it supposed to populate the vector pointed to by data? Note that the value of data is garbage.GetData(data);
Friday, August 23, 2013 8:14 PM -
LRESULT UpdateControl(WPARAM wpm, LPARAM lpm) { // get the WPARAM ionto a pointer std::vector<CString> *dataptr = reinterpret_cast<std::vector<CString> *>(wpm); // make a copy of the data (don't see any real reason to make a copy in your example though) std::vector<CString> data = *dataptr; delete dataptr; // I cannot imagine this possibly works right. There // is no way a vector<CString> can possibly be correctly // used as a VARIANT controlIns.UpdateControl((VARIANT*) &data); }
Some other notes:
Neither my solution or yours will work if PostMessage fails. There needs to be a check on PostMessage and cleanup there if the post fails.
- Proposed as answer by May Wang - MSFT Monday, August 26, 2013 8:31 AM
- Marked as answer by May Wang - MSFT Friday, August 30, 2013 2:13 AM
Friday, August 23, 2013 8:18 PM