Heap
-
Within a class I need to have an object in the heap. Within the constructor I make a new object and I can access the data members. When I try to access them in different member functions however I get memory access errors. I call the delete in the destructor, and not before. I'm not sure what is wrong.
-
Within a class I need to have an object in the heap. Within the constructor I make a new object and I can access the data members. When I try to access them in different member functions however I get memory access errors. I call the delete in the destructor, and not before. I'm not sure what is wrong.
Could you post your code?
-
Could you post your code?
-
Here's your problem: In
CParticleEngine::init(int nParticles)
you have the following line:CParticle *m_pParticles = new CParticle[nParticles];
Change it tom_pParticles = new CParticle[nParticles];
and your code will work. By declaring the m_pParticles variable again in theinit()
function, you have made a new variable that only has scope in theinit()
function, and happens to be named the same as your member variable m_pParticles. Thus the member variable never gets assigned, and you (rightfully) get memory errors when you later try and delete it. --Dean -
Within a class I need to have an object in the heap. Within the constructor I make a new object and I can access the data members. When I try to access them in different member functions however I get memory access errors. I call the delete in the destructor, and not before. I'm not sure what is wrong.
-
Here's your problem: In
CParticleEngine::init(int nParticles)
you have the following line:CParticle *m_pParticles = new CParticle[nParticles];
Change it tom_pParticles = new CParticle[nParticles];
and your code will work. By declaring the m_pParticles variable again in theinit()
function, you have made a new variable that only has scope in theinit()
function, and happens to be named the same as your member variable m_pParticles. Thus the member variable never gets assigned, and you (rightfully) get memory errors when you later try and delete it. --DeanExcellent. Thanks a bunch!