f



Memory allocation problems

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?


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Kieran
6/30/2003 9:17:41 AM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

5 Replies
530 Views

Similar Articles

[PageSpeed] 19

Hi,

> 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?

First, I wonder why you're using malloc instead of new, and hand-rolled
strings instead of the string class, though that's not related to your problem.

In fact, within your destructor, the object being destructed is still in
good shape and in perfectly valid memory, thus the string pointer should be
fine there as well. Hence, the bug is elsewhere.

Why not posting some code here?

Greetings,
	Thomas


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Thomas
6/30/2003 6:38:34 PM

Kieran Simpson schrieb:

> 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?

Please post a minimalist code example showing your problems.
Just reading a description of the symptoms is hardly enough information to
give a serious/helpful response.

regards,

Thomas


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Thomas
6/30/2003 6:41:21 PM
look interesing (and rather ambitious). what do the gurus say?

--
========================================
Max Khesin, software developer -
max@cNvOiSsPiAoMntech.com
[check out our image compression software at www.cvisiontech.com, JBIG2-PDF
compression @
www.cvisiontech.com/cvistapdf.html]


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Max
6/30/2003 9:57:47 PM
Most likely there is something else going on that corrupts the memory.
Try to produce the smallest example that exhibits the problem, and
post it here.

By the way, it is unsafe to allocate memory to a raw pointer in a
constructor, because if some other part of the constructor throws, the
destructor will not be called and you will get a leak.

This is not to say that you should not be allocating memory in a
constructor. In fact, with very few exceptions, constructors should be
the only means of resource acquisition (the RAII idiom). The correct
code would allocate memory in the constructor, but to a managed smart
pointer instead.

--Emil

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
emild
7/1/2003 8:13:51 AM
On Mon, 30 Jun 2003 05:17:41 -0400, Kieran Simpson wrote:

> 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. 

Are you using std::string, or an array of chars?
for std::string malloc will have to be accompanied with a call to the
placement new function.


 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.
> 
It seems you are using VC. But that doesn't prove anything.

> 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?

I don't think so.





      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Dhruv
7/2/2003 5:00:58 PM
Reply: