Question about some code that should crash but does not (release vs. debug & VS2003 vs. VS2008)
-
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.
-
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.
Why worry? The code is erronous, so the result of executing it is undefined. Just replace your
malloc
withnew 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
-
Why worry? The code is erronous, so the result of executing it is undefined. Just replace your
malloc
withnew 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
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.
-
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.
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
-
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
Thanks, that's probably it; me think i will look at the memory a little closer.
This signature was proudly tested on animals.