memory allocation problems

K

Kieran Simpson

In my project I have a class which I create an instance of, into a pointer
using the 'new' keyword.

Inside 'class1' I allocate memory (using malloc) for a string in the
constructor and everything works fine. When I try to destroy the instance
using the corresponding 'delete' keyword, the code goes to 'class1'
destructor where I try to free the memory (using free) allocated by the
constructor. However I get a debug error.

Am I getting the error because when I destroy the instance of 'class1' the
memory allocated by the constructor is already freed, and therefore I am
trying to free free memory, or is there something else at work?
 
J

Josephine Schafer

Jason Xie said:
before you free the memory, please check that it is not a null pointer.

Freeing a NULL pointer does not cause any problems.
OP should post some real code to demonstarate the problem.
 
J

John Harrison

Kieran Simpson said:
In my project I have a class which I create an instance of, into a pointer
using the 'new' keyword.

Inside 'class1' I allocate memory (using malloc) for a string in the
constructor and everything works fine. When I try to destroy the instance
using the corresponding 'delete' keyword, the code goes to 'class1'
destructor where I try to free the memory (using free) allocated by the
constructor. However I get a debug error.

Am I getting the error because when I destroy the instance of 'class1' the
memory allocated by the constructor is already freed, and therefore I am
trying to free free memory, or is there something else at work?

Maybe, maybe not. Hard to tell without seeing any code.

POST SOME CODE, jeez.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top