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. Strange crush

Strange crush

Scheduled Pinned Locked Moved C / C++ / MFC
comhelpquestion
19 Posts 7 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.
  • H Hans Ruck

    I'll show it but is a bit complicated. I wrote f in order to simplify the stuff. Look:

    template<class V, class T>
    T* StructFromVector(V *pvect,
    string szName)
    {
    if (!pvect) return FALSE;

    for (V::iterator it=pvect->begin();
    it!=pvect->end(); it++)
    if ((*it)->m_szName==szName)
    return *it;

    return NULL;
    }

    where string is the STL one and pvect will be a pointer to an STL vector containing pointers to some structures. The real call looks like this:

    if (!StructFromVector<RIGHTVECTOR2, RightStruct>(&(pUser->
    m_vectRights), (char *)bstRight))
    {
    //...
    }

    Please help! If you can... :(( rechi

    D Offline
    D Offline
    Daniel Turini
    wrote on last edited by
    #8

    Bogdan Rechi wrote: (char *)bstRight) This is not safe, and maybe your problem. A NULL BSTR is often used to represent empty strings and std::string's == operator will generate an access violation on "if ((*it)->m_szName==szName)". Don't know if this is your problem, but soon will be :) Use always (char *)_bstr_t(bstRight) or _com_util::ConvertBSTRToString(bstRight) My latest articles: XOR tricks for RAID data protection Win32 process suspend/resume tool

    H 1 Reply Last reply
    0
    • S Steen Krogsgaard

      from you "Hungarian" I'd say bstRight is a bstr, can you really cast it to (char*)? And shouldn't you really cast it to a string? Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

      H Offline
      H Offline
      Hans Ruck
      wrote on last edited by
      #9

      Steen Krogsgaard wrote: bstRight is a bstr Actually, is _bstr_t. But you're right, i could have cast it directly to string. Thanx for observation. Unfortunatelly, it doesn't solve the crush problem... rechi

      S 1 Reply Last reply
      0
      • H Hans Ruck

        Steen Krogsgaard wrote: bstRight is a bstr Actually, is _bstr_t. But you're right, i could have cast it directly to string. Thanx for observation. Unfortunatelly, it doesn't solve the crush problem... rechi

        S Offline
        S Offline
        Steen Krogsgaard
        wrote on last edited by
        #10

        I'm still not sure you can cast a _bstr_t to a string (at least not using an explicit cast), they have totally different binary representations, but then again, I'm no expert on either. Anyway, how about stepping through your code, especially through the function, and find the offending line? I'm pretty sure it's not in the line calling the function, it must be somewhere within the function. Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

        H 1 Reply Last reply
        0
        • H Hans Ruck

          I'll show it but is a bit complicated. I wrote f in order to simplify the stuff. Look:

          template<class V, class T>
          T* StructFromVector(V *pvect,
          string szName)
          {
          if (!pvect) return FALSE;

          for (V::iterator it=pvect->begin();
          it!=pvect->end(); it++)
          if ((*it)->m_szName==szName)
          return *it;

          return NULL;
          }

          where string is the STL one and pvect will be a pointer to an STL vector containing pointers to some structures. The real call looks like this:

          if (!StructFromVector<RIGHTVECTOR2, RightStruct>(&(pUser->
          m_vectRights), (char *)bstRight))
          {
          //...
          }

          Please help! If you can... :(( rechi

          R Offline
          R Offline
          Ranjan Banerji
          wrote on last edited by
          #11

          If bstRight is no longer needed try doing: if (!StructFromVector(&(pUser-> m_vectRights), (char *)bstRight.Detach() )){//...}

          1 Reply Last reply
          0
          • R Ranjan Banerji

            Just a hunch.... You are passing the function a BSTR. What BSTR class are you using? It coud be that you now have two objects pointing to the the same string. 1. The object in your vector and 2. The bstring container. When one goes out of scope it deletes the string. Then when the other goes out of scope it tries to delete or use the string which has just been deleted.

            H Offline
            H Offline
            Hans Ruck
            wrote on last edited by
            #12

            Ranjan Banerji wrote: You are passing the function a BSTR No, i'm passing a string. If you were right, i think it should have crushed on the first call. But if i call it without if (...) it works ok and doesn't crush. rechi

            1 Reply Last reply
            0
            • S Steen Krogsgaard

              I'm still not sure you can cast a _bstr_t to a string (at least not using an explicit cast), they have totally different binary representations, but then again, I'm no expert on either. Anyway, how about stepping through your code, especially through the function, and find the offending line? I'm pretty sure it's not in the line calling the function, it must be somewhere within the function. Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

              H Offline
              H Offline
              Hans Ruck
              wrote on last edited by
              #13

              Steen Krogsgaard wrote: how about stepping through your code I did it before posting here. I even stepped through the assembly lines; they look like this:

              012DDBCF mov dword ptr [ebp-68h],eax
              012DDBD2 cmp dword ptr [ebp-68h],0
              012DDBD6 jne CRolesUsers2::UserHasRight+1C3h (012ddc53)
              33: {
              34: for (it=pUser->m_vectRoles.begin(); it!=pUser->m_vectRoles.
              012DDBD8 mov ecx,dword ptr [ebp-1Ch]

              It fails on jne when - very intersting! - tries to jump to 012DDBD8 and not to 012ddc53. rechi

              S 1 Reply Last reply
              0
              • D Daniel Turini

                Bogdan Rechi wrote: (char *)bstRight) This is not safe, and maybe your problem. A NULL BSTR is often used to represent empty strings and std::string's == operator will generate an access violation on "if ((*it)->m_szName==szName)". Don't know if this is your problem, but soon will be :) Use always (char *)_bstr_t(bstRight) or _com_util::ConvertBSTRToString(bstRight) My latest articles: XOR tricks for RAID data protection Win32 process suspend/resume tool

                H Offline
                H Offline
                Hans Ruck
                wrote on last edited by
                #14

                Daniel Turini wrote: Use always (char *)_bstr_t(bstRight) I did. bstRight is _bstr_t... rechi

                M 1 Reply Last reply
                0
                • H Hans Ruck

                  Steen Krogsgaard wrote: how about stepping through your code I did it before posting here. I even stepped through the assembly lines; they look like this:

                  012DDBCF mov dword ptr [ebp-68h],eax
                  012DDBD2 cmp dword ptr [ebp-68h],0
                  012DDBD6 jne CRolesUsers2::UserHasRight+1C3h (012ddc53)
                  33: {
                  34: for (it=pUser->m_vectRoles.begin(); it!=pUser->m_vectRoles.
                  012DDBD8 mov ecx,dword ptr [ebp-1Ch]

                  It fails on jne when - very intersting! - tries to jump to 012DDBD8 and not to 012ddc53. rechi

                  S Offline
                  S Offline
                  Steen Krogsgaard
                  wrote on last edited by
                  #15

                  Ooh, my asm is a little rusty! I don't get this - I assume 012DDBCF to 012DDBD6 is the asm for

                  if (!pvect) return FALSE

                  but from the asm it seems at it is testing the opposite: jne (jump not equal), that is if dword ptr[ebp-68h] != 0 - or am I remembering jne wrong? Anyway, assuming the asm is right, the "jumping" to 012DDBD8 is because the test failed - that is, pvect != NULL. And the access violation comes as line 34 is executed. Try splitting the line into seperate statements (my stl is even worse than my asm, so this may not work):

                  V* vstart = pvect->begin();
                  V* vend = pvect->end();
                  for (V::iterator it= vstart; it!=vend; it++)
                  {
                  ...blablabla

                  gotta go home now, see you tomorrow. Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

                  H 1 Reply Last reply
                  0
                  • S Steen Krogsgaard

                    Ooh, my asm is a little rusty! I don't get this - I assume 012DDBCF to 012DDBD6 is the asm for

                    if (!pvect) return FALSE

                    but from the asm it seems at it is testing the opposite: jne (jump not equal), that is if dword ptr[ebp-68h] != 0 - or am I remembering jne wrong? Anyway, assuming the asm is right, the "jumping" to 012DDBD8 is because the test failed - that is, pvect != NULL. And the access violation comes as line 34 is executed. Try splitting the line into seperate statements (my stl is even worse than my asm, so this may not work):

                    V* vstart = pvect->begin();
                    V* vend = pvect->end();
                    for (V::iterator it= vstart; it!=vend; it++)
                    {
                    ...blablabla

                    gotta go home now, see you tomorrow. Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

                    H Offline
                    H Offline
                    Hans Ruck
                    wrote on last edited by
                    #16

                    Steen Krogsgaard wrote: my asm is a little rusty And so is mine... :rolleyes: Steen Krogsgaard wrote: I assume 012DDBCF to 012DDBD6 is the asm for... I'm sorry, i have been confusing. It was about if (!StructFromVector<...>(...). So, that assembling occurs after the function call ends! Observation: it only crushes when i try to debug it. If i call it without debugging, everything seems ok but i have logical errors and got to end in the step by step hell. X| rechi

                    S 1 Reply Last reply
                    0
                    • H Hans Ruck

                      Daniel Turini wrote: Use always (char *)_bstr_t(bstRight) I did. bstRight is _bstr_t... rechi

                      M Offline
                      M Offline
                      Mike Player
                      wrote on last edited by
                      #17

                      BSTR are wide chars. Unless you are compiling in UNICODE this is a very bad cast. Check out WideCharToMultiByte to convert from WCHAR/BSTR to char

                      H 1 Reply Last reply
                      0
                      • M Mike Player

                        BSTR are wide chars. Unless you are compiling in UNICODE this is a very bad cast. Check out WideCharToMultiByte to convert from WCHAR/BSTR to char

                        H Offline
                        H Offline
                        Hans Ruck
                        wrote on last edited by
                        #18

                        I used _bstr_t. It has a (char *) conversion operator. I've checked MSDN and it is not about UNICODE at all... Please, correct me if i'm wrong. rechi

                        1 Reply Last reply
                        0
                        • H Hans Ruck

                          Steen Krogsgaard wrote: my asm is a little rusty And so is mine... :rolleyes: Steen Krogsgaard wrote: I assume 012DDBCF to 012DDBD6 is the asm for... I'm sorry, i have been confusing. It was about if (!StructFromVector<...>(...). So, that assembling occurs after the function call ends! Observation: it only crushes when i try to debug it. If i call it without debugging, everything seems ok but i have logical errors and got to end in the step by step hell. X| rechi

                          S Offline
                          S Offline
                          Steen Krogsgaard
                          wrote on last edited by
                          #19

                          This is about your program accessing random memory, so sometimes it will go right, sometimes wrong, so I wouldn't put too much into it only crashing when you debug it. Anyway, if the program crashes after the template function returns there's two possibilites: your return value is garbage, or your parameters are garbage (or produced garbage during cleanup). I still very much suspect your (char*) cast - try moving it out of the if statement and see if the crash-point moves. I would copy/convert the _bstr_t to a string before the function call. Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

                          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