C++ Superclass memory curroption

Started by
13 comments, last by Nitage 17 years ago
I am comfertable with Visual Studio 2005 and the compiler and the debugger :) and I do know c++ and c very fluentlly : ). But untill the pointer gets curropted your right it doesn't show any visibile signs thats what I'm getting at. Even when I use pure c++ code(I replaced the char string[ 100 ] with just string).

Is there a problem doign this jmPlayer *player = new jmPlayer, and storing player in a array or vector array that comprises of jmEntity *.
Advertisement
Quote:Original post by crozzbreed23
Is there a problem doign this jmPlayer *player = new jmPlayer, and storing player in a array or vector array that comprises of jmEntity *.


No, there is no problem. Unless the contents of the array or vector are modified in some other invalid way, or the pointed memory is deleted.
Quote:Original post by ToohrVyk
Quote:Original post by crozzbreed23
Is there a problem doign this jmPlayer *player = new jmPlayer, and storing player in a array or vector array that comprises of jmEntity *.


No, there is no problem. Unless the contents of the array or vector are modified in some other invalid way, or the pointed memory is deleted.


So if the pointer to the array/vector container to all jmEntities is valid what would cause windows to start overwriting memory where it points to the inherited function addresses.

Quote:Original post by crozzbreed23
So if the pointer to the array/vector container to all jmEntities is valid what would cause windows to start overwriting memory where it points to the inherited function addresses.


I doubt the function addresses are being overwritten. It sounds more like some class, perhaps the one containing the collection of entity pointers, does not follow the Rule of Three (if you need to implement a destructor, copy constructor, or assignment operator, you need to implement all three).

Try double-checking that.

--smw

Stephen M. Webb
Professional Free Software Developer

Firstly, there is an error in the code you posted so far - jmEntity doesn't have a virtual destructor.

Secondly, there are a number of other potential errors (I can't say if they're responsible for your problem until you post all the code and specify what you mean by "The class gets corrupted") - the potential errors caused by using strcpy/memset and other C functions have been discussed. The other big issue possibly causing your errors is the manner in which your are intialising your classes:

Perhaps the constructor of your derived classes use the member variables of jmEntity (which aren't initialized when the constructor runs).

Perhaps the Init() functions of your derived classes don't call the Init() function of the base class.

Perhaps the members of jmEntity() which are initialised in SpawnEntity() never get intialized.

You'd be much better off elimintating the the Init() function and just using the constructors of your objects properly. Then

This topic is closed to new replies.

Advertisement