Hi!
I have been writing several games in c++ lately and each time I encounter the same problem. I have a quite advance class structure with inheritance. I have several classes which include pointer arrays of other complex classes (which in their turn also have pointer arrays of other classes).
Now everything seems to be correctly initialized and memory allocation seems ok, but sometimes my program crashes without any reason at the same spot. I have tried to debug it many times, but everything seems fine. But somehow I suspect that the classes included within another class overlap in memory. Is that possible? And if so, what can I do to avoid it?
Here is another strange thing. I have a following class (much simplified):
Now when I try to do something like C->myFunc(); I get SIGSEGV. When I debug the program, it says that when entering the function MyClass3::myFunc() other contents of class C is unavailable (this==NULL). That is why the function myFunc() can't access any of these. This is though I have clearly allocated memory for this class.Code:class MyClass { MyClass(); ~MyClass(); MyClass1 *A; MyClass2 *B; MyClass3 *C; } MyClass::MyClass() { A=new MyClass1(); B=new MyClass2(); C=new MyClass3(); }
If I now change the constructor of MyClass a bit:
This works perfectly.Code:MyClass::MyClass() { C=new MyClass3(); //this comes now first A=new MyClass1(); B=new MyClass2(); }
Can anyone tell me what's going on here?
Thanx