stack overflow or memory out with new ... delete

Discussion to talk about software related topics only.
Post Reply
yanb19
Posts: 5
Joined: Thu Dec 17, 2009 10:38 am

stack overflow or memory out with new ... delete

Post by yanb19 »

I have a throwing exception with the "Trap Vector =Access Error (2)"
and i found where is in my software...but i found nothing special at the first time.

then i simulate the parts of the code and i call it all the time in my main to do the throwing exception.

// code with the throwing exception problem.
Normally with "new" in local variable when the variable
is out of the scope in this case out of the if(1){ .. } the memory is delete without the instruction code "delete". but it seem not true...because after near the 4min 10 sec.
if(1)
{
CZone z;
CZone *t = new CZone( z ); // create clone
if(t->IsFull())
{
t->Flush();
}
}

after that i just add the "delete" instruction and i never see the throwing exception...is it because
the netburner not delete it self the memory allocate for the local variable...
if(1)
{
CZone z;
CZone *t = new CZone( z ); // create clone
if(t->IsFull())
{
t->Flush();
}
delete t;
}

I think the issue is the stack over flow but i'm not sure a 100% do you know something about that problem with the memory allocation with "new" and "delete"

Thanks Yanick.
rnixon
Posts: 833
Joined: Thu Apr 24, 2008 3:59 pm

Re: stack overflow or memory out with new ... delete

Post by rnixon »

While the local variables

CZone z;
CZone *t

will be released when the function returns.

The memory you dynamically allocated that 't' points to:

CZone *t = new CZone( z ); // create clone

will not be released. You made it, so you need to delete it. This isn't a problem with new/delete.
yanb19
Posts: 5
Joined: Thu Dec 17, 2009 10:38 am

Re: stack overflow or memory out with new ... delete

Post by yanb19 »

i know is not a problem with new..delete.

but i think if you do that

void test()
{
CZone z;
CZone *t = new CZone( z ); // create clone
}

or that

void test()
{
CZone z;
CZone *t = new CZone( z ); // create clone
delete t;
}

the memory is release by the return of the function because is a local variable...
but now i see is not the case.

do you know if they have a function to call to are able to know the memory use by the netburner?
to know if the memory is correctly clean.
User avatar
tod
Posts: 587
Joined: Sat Apr 26, 2008 8:27 am
Location: Southern California
Contact:

Re: stack overflow or memory out with new ... delete

Post by tod »

Just to be a little more explicit. In your example

Code: Select all

void test()
{
CZone z;
CZone *t = new CZone( z ); // create clone 
}
Both the object z and the pointer to an object t are local variables and the will use Automatic Memory which I think of as stack based. All the memory for z and just the memory for the pointer t will be reclaimed when test() exits. HOWEVER, the memory for the object pointed to by t is allocated on the Free Store (also known as the heap). When test() exits, the pointer "t" is gone and you have a memory leak because you no longer have a way to release the memory used by the object hat t pointed to. EVERY use of new typically must have a corresponding use of delete. Every use of new [] must have a corresponding delete[]. See Myer's Effective C++ for a detailed explanation. I said "typically" above because in an embedded project you can new up an object that lasts forever and since memory will get reinitialized on a reboot you don't have to delete those objects the way you would in a PC based program. In fact you often won't have any sort of exit() function whereby you even could delete such objects. I tend to avoid new/delete or new[]/delete[] idioms in embedded projects. I will new up permanent objects that have singleton behavior. Judicious use of static classes, containers, namespaces etc can help you avoid memory management bugs.

In your example, I would create a copy constructor or copy assignment method (it CZone has managed resources) so that I could just write

Code: Select all

CZone t;
t =z;
or 
CZone t(z);
Now you don't have to remember to call delete. HOWEVER (there's always a however), if CZone contains resources managed by constructor/destructor semantics you must make sure your copy/assignment code correctly copies those resources. The default object copy code created by C++ does a memberwise copy which will not be what you want when you have managed resources. See Stroustrup's The C++ Programming Language 10.4.4.1 for a detailed explanation.
yanb19
Posts: 5
Joined: Thu Dec 17, 2009 10:38 am

Re: stack overflow or memory out with new ... delete

Post by yanb19 »

Thanks for the reply it's appreciated.

I found the problem the problem

the reason to the "delete" is not in the function is because the new pointer create is stack in
chain list of pointer of this class and the pointer is use in other thread of the application..and normally
delete after the information is read.

but the delete it is not use properly than the memory is never release... :oops:

and than after a long time... i have a throwing exception. :cry:

I just add a "delete" at the right place and the problem gone... :)

Thanks again. Yanick
Post Reply