Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem splitting dynamically allocated CString

Problem splitting dynamically allocated CString

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wim Jans
    wrote on last edited by
    #1

    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

    G T G 3 Replies Last reply
    0
    • W Wim Jans

      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

      G Offline
      G Offline
      Gary R Wheeler
      wrote on last edited by
      #2

      Try 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;

      1 Reply Last reply
      0
      • W Wim Jans

        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

        T Offline
        T Offline
        Ted Ferenc
        wrote on last edited by
        #3

        I 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

        1 Reply Last reply
        0
        • W Wim Jans

          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

          G Offline
          G Offline
          Garth J Lancaster
          wrote on last edited by
          #4

          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 !!!

          W 1 Reply Last reply
          0
          • G Garth J Lancaster

            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 !!!

            W Offline
            W Offline
            Wim Jans
            wrote on last edited by
            #5

            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

            G 1 Reply Last reply
            0
            • W Wim Jans

              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

              G Offline
              G Offline
              Garth J Lancaster
              wrote on last edited by
              #6

              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 !

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups