C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How is memory deallocated after an exception in a constructo

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Fred Zwarts
Guest





PostPosted: Tue Sep 27, 2005 3:43 pm    Post subject: How is memory deallocated after an exception in a constructo Reply with quote



Hello,

I am trying to debug some complex debug code.
In order to track the use of dynamically allocated memory,
I replaced the standard global new and delete operators.
(Not for changing the memory allocation algorithm, but
for gathering some statistics and to find memory leaks.)
This seems to work.

However, I noticed that my replacing delete operator is not called
in the case that an attempt is made to create an object of a class
using the new operator and an exception is thrown in the constructor.

My question is how the memory which was allocated for this object is
deallocated. Apparently the global delete operator is not used.

Cheers,
F.Z.

Back to top
peter.koch.larsen@gmail.c
Guest





PostPosted: Tue Sep 27, 2005 4:35 pm    Post subject: Re: How is memory deallocated after an exception in a constr Reply with quote




Fred Zwarts skrev:

Quote:
Hello,

I am trying to debug some complex debug code.
In order to track the use of dynamically allocated memory,
I replaced the standard global new and delete operators.
(Not for changing the memory allocation algorithm, but
for gathering some statistics and to find memory leaks.)
This seems to work.

However, I noticed that my replacing delete operator is not called
in the case that an attempt is made to create an object of a class
using the new operator and an exception is thrown in the constructor.

It should be.


Quote:
My question is how the memory which was allocated for this object is
deallocated. Apparently the global delete operator is not used.
Try it again. If you have:


class C {...};

C* c = newC();

and C's constructor throws, then the memory will be freed via delete.

Please notice that:
class B;
class C
{
C() { D* pd = new D; throw 0; }
};

....
C* c = new C();

Here memory for D will be leaked.

/Peter

Quote:

Cheers,
F.Z.


Back to top
Andre Kostur
Guest





PostPosted: Tue Sep 27, 2005 4:35 pm    Post subject: Re: How is memory deallocated after an exception in a constr Reply with quote



"Fred Zwarts" <F.Zwarts (AT) KVI (DOT) nl> wrote in news:dhbnuc$49b$1
@info.service.rug.nl:

Quote:
Hello,

I am trying to debug some complex debug code.
In order to track the use of dynamically allocated memory,
I replaced the standard global new and delete operators.
(Not for changing the memory allocation algorithm, but
for gathering some statistics and to find memory leaks.)
This seems to work.

However, I noticed that my replacing delete operator is not called
in the case that an attempt is made to create an object of a class
using the new operator and an exception is thrown in the constructor.

My question is how the memory which was allocated for this object is
deallocated. Apparently the global delete operator is not used.

<sarcasm>As I gaze into my crystal ball, I can obviously see that your
problem is that your computer isn't facing at exactly 43 degrees.</sarcasm>

Show us the code. We can't help you if we can't see what you're doing.
Remember to reduce the problem to the minimum amount of compilable code
that exhibits the problem.

Back to top
Fred Zwarts
Guest





PostPosted: Thu Sep 29, 2005 7:31 am    Post subject: Re: How is memory deallocated after an exception in a constr Reply with quote

<peter.koch.larsen (AT) gmail (DOT) com> wrote

Quote:

Fred Zwarts skrev:

Hello,

I am trying to debug some complex debug code.
In order to track the use of dynamically allocated memory,
I replaced the standard global new and delete operators.
(Not for changing the memory allocation algorithm, but
for gathering some statistics and to find memory leaks.)
This seems to work.

However, I noticed that my replacing delete operator is not called
in the case that an attempt is made to create an object of a class
using the new operator and an exception is thrown in the constructor.

It should be.

My question is how the memory which was allocated for this object is
deallocated. Apparently the global delete operator is not used.
Try it again. If you have:

class C {...};

C* c = newC();

and C's constructor throws, then the memory will be freed via delete.

Thanks for answering my question. It has set me on the right track,
I believe and I am closer to a solution. May I ask a few related questions
to check that I understand it correctly.

*) Consider now the following:

C* c = new C[3];

After allocating memory for the whole array, the constructor for each
array element is called. Suppose, however, that the constructor for the
second array element throws an exception. Is it true that the constructor
for the third element is not called, the destructor for the first element is
called and then the operator delete[] is called?

*) Suppose the class C overloads the global new and delete operators,
(defining these operators with the same parameter list). Then this class-specific
new operator will be used to allocate memory. If then the constructor
throws an exception, the class specific delete operator will be used to
deallocate memory. Is this true?

*) Suppose someone wants to implement another memory allocation
method and needs another operator new with an extra parameter
(the type of this parameter is not relevant).
Then he uses

C * c = new (X) C();

Now no operator delete is called if the constructor throws an exception.
Is it possible to deallocate the memory allocated by this modified new operator?
The pointer c has not yet received the result of the new operator,
so it cannot be use in a catch construction.

Thanks for your help in understanding these issues.

F.Z.

Back to top
Fred Zwarts
Guest





PostPosted: Thu Sep 29, 2005 7:36 am    Post subject: Re: How is memory deallocated after an exception in a constr Reply with quote


"Andre Kostur" <nntpspam (AT) kostur (DOT) net> wrote

Quote:
"Fred Zwarts" <F.Zwarts (AT) KVI (DOT) nl> wrote in news:dhbnuc$49b$1
@info.service.rug.nl:

Hello,

I am trying to debug some complex debug code.
In order to track the use of dynamically allocated memory,
I replaced the standard global new and delete operators.
(Not for changing the memory allocation algorithm, but
for gathering some statistics and to find memory leaks.)
This seems to work.

However, I noticed that my replacing delete operator is not called
in the case that an attempt is made to create an object of a class
using the new operator and an exception is thrown in the constructor.

My question is how the memory which was allocated for this object is
deallocated. Apparently the global delete operator is not used.

sarcasm>As I gaze into my crystal ball, I can obviously see that your
problem is that your computer isn't facing at exactly 43 degrees.
Show us the code. We can't help you if we can't see what you're doing.
Remember to reduce the problem to the minimum amount of compilable code
that exhibits the problem.

May be you should by yourself a flat screen to replace your crystal ball.
It would make it much easier to read text.

If you don't understand plain English, please, ignore it. Apparently
other people were able to understand the question and reply with
useful answers.

Back to top
peter.koch.larsen@gmail.c
Guest





PostPosted: Thu Sep 29, 2005 7:52 am    Post subject: Re: How is memory deallocated after an exception in a constr Reply with quote


Fred Zwarts wrote:
Quote:
peter.koch.larsen (AT) gmail (DOT) com> wrote


Fred Zwarts skrev:

Hello,

I am trying to debug some complex debug code.
In order to track the use of dynamically allocated memory,
I replaced the standard global new and delete operators.
(Not for changing the memory allocation algorithm, but
for gathering some statistics and to find memory leaks.)
This seems to work.

However, I noticed that my replacing delete operator is not called
in the case that an attempt is made to create an object of a class
using the new operator and an exception is thrown in the constructor.

It should be.

My question is how the memory which was allocated for this object is
deallocated. Apparently the global delete operator is not used.
Try it again. If you have:

class C {...};

C* c = newC();

and C's constructor throws, then the memory will be freed via delete.

Thanks for answering my question. It has set me on the right track,
I believe and I am closer to a solution. May I ask a few related questions
to check that I understand it correctly.

*) Consider now the following:

C* c = new C[3];

After allocating memory for the whole array, the constructor for each
array element is called. Suppose, however, that the constructor for the
second array element throws an exception. Is it true that the constructor
for the third element is not called, the destructor for the first element is
called and then the operator delete[] is called?

Yes.

Quote:

*) Suppose the class C overloads the global new and delete operators,
(defining these operators with the same parameter list). Then this class-specific
new operator will be used to allocate memory. If then the constructor
throws an exception, the class specific delete operator will be used to
deallocate memory. Is this true?

Yes.

Quote:

*) Suppose someone wants to implement another memory allocation
method and needs another operator new with an extra parameter
(the type of this parameter is not relevant).
Then he uses

C * c = new (X) C();

Now no operator delete is called if the constructor throws an exception.
Is it possible to deallocate the memory allocated by this modified new operator?

Yes. Just write the corresponding operator delete.

Quote:
The pointer c has not yet received the result of the new operator,
so it cannot be use in a catch construction.

Nothing prevents c to "receive" the value of the call to the new
operator. c contains garbage, but the compiler writer might know
better.


Quote:

Thanks for your help in understanding these issues.

F.Z.


Back to top
Fred Zwarts
Guest





PostPosted: Fri Sep 30, 2005 7:25 am    Post subject: Re: How is memory deallocated after an exception in a constr Reply with quote


<peter.koch.larsen (AT) gmail (DOT) com> wrote

Quote:

Fred Zwarts wrote:
[email]peter.koch.larsen (AT) gmail (DOT) com[/email]> wrote


Fred Zwarts skrev:

Hello,

I am trying to debug some complex debug code.
In order to track the use of dynamically allocated memory,
I replaced the standard global new and delete operators.
(Not for changing the memory allocation algorithm, but
for gathering some statistics and to find memory leaks.)
This seems to work.

However, I noticed that my replacing delete operator is not called
in the case that an attempt is made to create an object of a class
using the new operator and an exception is thrown in the constructor.

It should be.

My question is how the memory which was allocated for this object is
deallocated. Apparently the global delete operator is not used.
Try it again. If you have:

class C {...};

C* c = newC();

and C's constructor throws, then the memory will be freed via delete.

Thanks for answering my question. It has set me on the right track,
I believe and I am closer to a solution. May I ask a few related questions
to check that I understand it correctly.


Thanks for answering these questions. I think I start to understand how this all works.
However, your answer to my last question was not completely clear to me:

Quote:
*) Suppose someone wants to implement another memory allocation
method and needs another operator new with an extra parameter
(the type of this parameter is not relevant).
Then he uses

C * c = new (X) C();

Now no operator delete is called if the constructor throws an exception.
Is it possible to deallocate the memory allocated by this modified new operator?

Yes. Just write the corresponding operator delete.

But the compiler does not know which delete operator corresponds to this modified
new operator. So, the compiler will not automatically create a call to the
corresponding operator delete.
Am I still right?

Quote:
The pointer c has not yet received the result of the new operator,
so it cannot be use in a catch construction.

Nothing prevents c to "receive" the value of the call to the new
operator. c contains garbage, but the compiler writer might know
better.

This is no clear to me. What has the compiler writer to do with it?
Do you want to say that the return value of the operator new (X)
is first assigned to c and that the call to the constructor is a next step?
Is this the order in which these steps must take place?

Maybe my question is not clear. I understand that in cases where the
operator new has more parameters, the programmer is responsible
for calling the corresponding delete (if needed).
My understanding is that in such a case the compiler
will not automatically create a call to some delete operator
when the constructor throws an exception. The exception cannot be
handled in the new operator, because the constructor is called after
the return of the operator new.
So one could use a try and catch block to handle this situation:

C * c;
try {
c = new (X) C();
} catch (int i) {
???
}

Suppose the constructor of C throws an int.
What can be put in place of the question marks? Is c defined at this point?
A delete operator cannot be used, because the object has not been created,
so the destructor should not be called. (But another memory deallocation
function could be called, instead.) But is the pointer c valid at this point?
How would a programmer prevent a memory leak in this case?

Thanks for your attention.
F.Z.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.