Problem splitting dynamically allocated CString
-
Hi, I have a problem splitting up a string. I have a thread which will send a string to my main thread by posting a message. I therefore create my string
CString *s = new CString(); s->Format("%s#@@#%s", _T("Error"), _T("ErrorMessage")); PostMessage(UWM_SENDSTRING, (WPARAM)i, (LPARAM)s);
The code is successfully received by my function. However, here is where it goes wrong.LRESULT CMyView::OnSendString(WPARAM wparam, LPARAM lparam) { CString *s = (CString*)lparam; TRACE0(*s); // this shows my string int pos = s->Find(_T("#@@#")); // shows the position where it is found, OK TRACE0(s->Left(pos)); // gives me crap TRACE0(s->Right(s->GetLength()-(pos+4))); // crap to, or Access Violations, etc delete s;
So basically when I show the string, compute a position or show the length of *s everything is fine. When I just want a part of the string, the first or last, I get a lot of crap or the program just crashes. When I do all of that splitting in a normal procedure whit normal CString (not dynamically allocated), I get the results thaat I want, namely he part before #@@# and the part after that. Any ideas how I can let this Left/Right splitting thing work on dynamic CStrings? tia Wim -
Hi, I have a problem splitting up a string. I have a thread which will send a string to my main thread by posting a message. I therefore create my string
CString *s = new CString(); s->Format("%s#@@#%s", _T("Error"), _T("ErrorMessage")); PostMessage(UWM_SENDSTRING, (WPARAM)i, (LPARAM)s);
The code is successfully received by my function. However, here is where it goes wrong.LRESULT CMyView::OnSendString(WPARAM wparam, LPARAM lparam) { CString *s = (CString*)lparam; TRACE0(*s); // this shows my string int pos = s->Find(_T("#@@#")); // shows the position where it is found, OK TRACE0(s->Left(pos)); // gives me crap TRACE0(s->Right(s->GetLength()-(pos+4))); // crap to, or Access Violations, etc delete s;
So basically when I show the string, compute a position or show the length of *s everything is fine. When I just want a part of the string, the first or last, I get a lot of crap or the program just crashes. When I do all of that splitting in a normal procedure whit normal CString (not dynamically allocated), I get the results thaat I want, namely he part before #@@# and the part after that. Any ideas how I can let this Left/Right splitting thing work on dynamic CStrings? tia WimTry setting a watchpoint (breakpoint on a data address) on s after you create it, and before the PostMessage call. It sounds like something else is modifying it. Also, have you single stepped through the OnSendString function, and watched the contents of s in the Watch window?
Software Zen:
delete this;
-
Hi, I have a problem splitting up a string. I have a thread which will send a string to my main thread by posting a message. I therefore create my string
CString *s = new CString(); s->Format("%s#@@#%s", _T("Error"), _T("ErrorMessage")); PostMessage(UWM_SENDSTRING, (WPARAM)i, (LPARAM)s);
The code is successfully received by my function. However, here is where it goes wrong.LRESULT CMyView::OnSendString(WPARAM wparam, LPARAM lparam) { CString *s = (CString*)lparam; TRACE0(*s); // this shows my string int pos = s->Find(_T("#@@#")); // shows the position where it is found, OK TRACE0(s->Left(pos)); // gives me crap TRACE0(s->Right(s->GetLength()-(pos+4))); // crap to, or Access Violations, etc delete s;
So basically when I show the string, compute a position or show the length of *s everything is fine. When I just want a part of the string, the first or last, I get a lot of crap or the program just crashes. When I do all of that splitting in a normal procedure whit normal CString (not dynamically allocated), I get the results thaat I want, namely he part before #@@# and the part after that. Any ideas how I can let this Left/Right splitting thing work on dynamic CStrings? tia WimI don't think what you are doing is allowed, in effect posting a class, I am sure someone will confirm/deny that, so why not do:-
CString *s = new CString(); s->Format("%s#@@#%s", _T("Error"), _T("ErrorMessage")); TCHAR *sz = new char[s.GetLength() + 1); strcpy(sz, s->GetBuffer(0)); PostMessage(UWM_SENDSTRING, (WPARAM)i, (LPARAM)sz);
Note the code is not 100% correct, e.g.strcpy
should not be used if you are using UNICODE
If I have seen further it is by standing on the shoulders of Giants. - Isaac Newton 1676
-
Hi, I have a problem splitting up a string. I have a thread which will send a string to my main thread by posting a message. I therefore create my string
CString *s = new CString(); s->Format("%s#@@#%s", _T("Error"), _T("ErrorMessage")); PostMessage(UWM_SENDSTRING, (WPARAM)i, (LPARAM)s);
The code is successfully received by my function. However, here is where it goes wrong.LRESULT CMyView::OnSendString(WPARAM wparam, LPARAM lparam) { CString *s = (CString*)lparam; TRACE0(*s); // this shows my string int pos = s->Find(_T("#@@#")); // shows the position where it is found, OK TRACE0(s->Left(pos)); // gives me crap TRACE0(s->Right(s->GetLength()-(pos+4))); // crap to, or Access Violations, etc delete s;
So basically when I show the string, compute a position or show the length of *s everything is fine. When I just want a part of the string, the first or last, I get a lot of crap or the program just crashes. When I do all of that splitting in a normal procedure whit normal CString (not dynamically allocated), I get the results thaat I want, namely he part before #@@# and the part after that. Any ideas how I can let this Left/Right splitting thing work on dynamic CStrings? tia WimThis is probablya bit late (its a public holiday down here in Australia, but I got suckered into coming into the office anyway) anyway, see this link for an explanation and working code on posting CStrings (look for "Passing Pointers in Messages") - Note well one of Joe's comments "However, if you ever plan to use PostMessage to pass a pointer to an object, then you are constrained to always use a static or heap-based object" http://www.pgh.net/~newcomer/messages.htm[^] Hope this helps ... 'G' <- Grinch !!!
-
This is probablya bit late (its a public holiday down here in Australia, but I got suckered into coming into the office anyway) anyway, see this link for an explanation and working code on posting CStrings (look for "Passing Pointers in Messages") - Note well one of Joe's comments "However, if you ever plan to use PostMessage to pass a pointer to an object, then you are constrained to always use a static or heap-based object" http://www.pgh.net/~newcomer/messages.htm[^] Hope this helps ... 'G' <- Grinch !!!
Hi Garth, Thanks for the advice. Strangely enough, the piece of code I posted comes from another essay of Mr. Newcomer I read about threads[^]. There was a reference to the article you mentioned. So probably I missed something reading the articles, or I'm just a bad cut&paste person. I will read these articles again tonight and see if I missed something, else I just send 2 messages (errorcode and errormessage) as it seems to work (Using watch variables) if I don't tamper around with the strings. Thanks and enjoy your holiday Wim
-
Hi Garth, Thanks for the advice. Strangely enough, the piece of code I posted comes from another essay of Mr. Newcomer I read about threads[^]. There was a reference to the article you mentioned. So probably I missed something reading the articles, or I'm just a bad cut&paste person. I will read these articles again tonight and see if I missed something, else I just send 2 messages (errorcode and errormessage) as it seems to work (Using watch variables) if I don't tamper around with the strings. Thanks and enjoy your holiday Wim
Wim Jans wrote: ) if I don't tamper around with the strings Hi Wim - I think it comes down to that particular phrase ... when I worked in San Diego recently I worked on a project that had to modify a CString .. yuck !!! there's a particular set of hoops you have to go through - something like CString.ReleaseBuffer().. I think this is also discussed by Joe Newcomer, under his MVP tips 'CString Management' ... its still a pain in the rear though good luck !