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. Question about some code that should crash but does not (release vs. debug & VS2003 vs. VS2008)

Question about some code that should crash but does not (release vs. debug & VS2003 vs. VS2008)

Scheduled Pinned Locked Moved C / C++ / MFC
questionvisual-studiographicsdebuggingannouncement
5 Posts 3 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.
  • M Offline
    M Offline
    Maximilien
    wrote on last edited by
    #1

    Howdy' I have some very bad code that should crash but I'm having difficulty figuring out why it does not crash (or even give out warnings); it works in VS2003 in release&debug and in VS2008 in debug, not in release.

    class Info
    {
    public:
    Info();
    Info(std::wstring s1, std::wstring s2);// : m_string1(_T("")),m_string2(_T("")){};
    std::wstring m_string2;
    };
    struct myStruct
    {
    std::vector <Info*> m_JobInfoVector;
    };

    m_MyStruct = (myStruct*)malloc(sizeof (myStruct) );
    ZeroMemory(m_MyStruct, sizeof(myStruct));
    Info* p = new Info( std::wstring(_T("allo")) );
    m_MyStruct->m_JobInfoVector.clear();

    Question : What happens to the vector member when the ZeroMemory is called ? and why it does not crash in debug but it does in release ? :confused: Thanks. Max.

    This signature was proudly tested on animals.

    S C 2 Replies Last reply
    0
    • M Maximilien

      Howdy' I have some very bad code that should crash but I'm having difficulty figuring out why it does not crash (or even give out warnings); it works in VS2003 in release&debug and in VS2008 in debug, not in release.

      class Info
      {
      public:
      Info();
      Info(std::wstring s1, std::wstring s2);// : m_string1(_T("")),m_string2(_T("")){};
      std::wstring m_string2;
      };
      struct myStruct
      {
      std::vector <Info*> m_JobInfoVector;
      };

      m_MyStruct = (myStruct*)malloc(sizeof (myStruct) );
      ZeroMemory(m_MyStruct, sizeof(myStruct));
      Info* p = new Info( std::wstring(_T("allo")) );
      m_MyStruct->m_JobInfoVector.clear();

      Question : What happens to the vector member when the ZeroMemory is called ? and why it does not crash in debug but it does in release ? :confused: Thanks. Max.

      This signature was proudly tested on animals.

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      Why worry? The code is erronous, so the result of executing it is undefined. Just replace your malloc with new myStruct...and don't, just don't ever write over a class with something like ZeroMemory - it just doesn't make sense. You have constructors to initialise structs and classes in C++. But anyway - the error is in comparison of iterators (which clear does) - in VS2008, Debug mode iterators are different to Release mode iterators unless you specifically enable iterator debugging, which probably has something to do with it.

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      M 1 Reply Last reply
      0
      • S Stuart Dootson

        Why worry? The code is erronous, so the result of executing it is undefined. Just replace your malloc with new myStruct...and don't, just don't ever write over a class with something like ZeroMemory - it just doesn't make sense. You have constructors to initialise structs and classes in C++. But anyway - the error is in comparison of iterators (which clear does) - in VS2008, Debug mode iterators are different to Release mode iterators unless you specifically enable iterator debugging, which probably has something to do with it.

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        I know that, but I was wondering why it did not crash; anyway, the code has been fixed. Thanks. :)

        This signature was proudly tested on animals.

        1 Reply Last reply
        0
        • M Maximilien

          Howdy' I have some very bad code that should crash but I'm having difficulty figuring out why it does not crash (or even give out warnings); it works in VS2003 in release&debug and in VS2008 in debug, not in release.

          class Info
          {
          public:
          Info();
          Info(std::wstring s1, std::wstring s2);// : m_string1(_T("")),m_string2(_T("")){};
          std::wstring m_string2;
          };
          struct myStruct
          {
          std::vector <Info*> m_JobInfoVector;
          };

          m_MyStruct = (myStruct*)malloc(sizeof (myStruct) );
          ZeroMemory(m_MyStruct, sizeof(myStruct));
          Info* p = new Info( std::wstring(_T("allo")) );
          m_MyStruct->m_JobInfoVector.clear();

          Question : What happens to the vector member when the ZeroMemory is called ? and why it does not crash in debug but it does in release ? :confused: Thanks. Max.

          This signature was proudly tested on animals.

          C Offline
          C Offline
          Christopher W Smith
          wrote on last edited by
          #4

          I think you are assuming that a struct in C++ works the same way as a struct in plain old C, but alas it doesn't... well not really anyway. When you introduce non-primitive data types into structs in C++ they cease to be simple "data structures" and become basically the same as classes only their members default to public instead of private. My guess is when you try to overwrite the object you are writing over the beginning of its virtual table which probably contains extra debug information in debug mode. In release mode you are then probably overwriting more important class data. Remember, the sizeof operator is not designed to get the size of an object or its virtual table. http://en.wikipedia.org/wiki/Virtual_method_table[^]

          Chris Smith

          M 1 Reply Last reply
          0
          • C Christopher W Smith

            I think you are assuming that a struct in C++ works the same way as a struct in plain old C, but alas it doesn't... well not really anyway. When you introduce non-primitive data types into structs in C++ they cease to be simple "data structures" and become basically the same as classes only their members default to public instead of private. My guess is when you try to overwrite the object you are writing over the beginning of its virtual table which probably contains extra debug information in debug mode. In release mode you are then probably overwriting more important class data. Remember, the sizeof operator is not designed to get the size of an object or its virtual table. http://en.wikipedia.org/wiki/Virtual_method_table[^]

            Chris Smith

            M Offline
            M Offline
            Maximilien
            wrote on last edited by
            #5

            Thanks, that's probably it; me think i will look at the memory a little closer.

            This signature was proudly tested on animals.

            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