Link to home
Start Free TrialLog in
Avatar of calum
calum

asked on

memory scope

if i dynamically create an array within a function body, will the memory be released when the function exits automatically?

ie
void func()
{
   CMy_Class* temp_array;
   int some_size = 10;

   temp_array = new CMy_Class[some_size];

   //do some stuff
}
is the memory released or do i have a memory leak?

if so, how do i stop it? i;ve tried to use

delete temp_array;

but i get access violation errors (using ms visual studio).

thanks

cal

Avatar of imladris
imladris
Flag of Canada image

It's a memory leak.

You should free it with:

delete temp_array[];
ASKER CERTIFIED SOLUTION
Avatar of pagladasu
pagladasu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, should be:

delete [] temp_array;
And, yes, pagladasu is right, you would have to do that in the function. If you want to do it later (after you have exited the function) then the function will have to pass a pointer to temp_array to the calling code so that it will still have access to it.
Avatar of calum
calum

ASKER

thanks - but i get an access violation when i attempt to delete [] temp_array;

what;s wrong?
have you kept the destructor public?
Avatar of calum

ASKER

do you mean the destructor for the C_My_Class? the class def looks something like

class CCritter
{
public:

     CCritter();
     ~CCritter();
        //some methods etc
};

does CMy_Class use dynamically allocated memory and is IT being released correctly?

Programming Tip:

In your constructor, initialize dynamically allocated data members to NULL.

myobj = NULL;

In the destructor, check for NULL before you delete:

if (myobj != NULL)
  delete myobj;

Avatar of calum

ASKER

jeez - i don;t know. i do call delete my_obj in other places, and don;t get the access violation. it;s only when i create an array of the objects and then try to delete the array i get this access violation.

the object itself contains a linked list, which is populated later in the program, but i have destructors for the objects the list contains. there is no other dynamically created member.

how would i set my_obj = NULL in the constructor? is that a this pointer?

should i therefore test the this pointer in the destructor to make sure it;s not NULL before deletion?
I think the problem is in the code that you have written for the destructor of CMy_Class.
Avatar of calum

ASKER

hmm - any suggestions as to what to look out for? what is the error message actually telling me?

try compiling the program in Release mode and execute
Can you possibly post the errors you're getting along with the code?
which compiler u r using ?
Is the class derived from some other class ?
Can you post the code?

-ilikenine
Avatar of calum

ASKER

i think i;ve solved the problem:

if i use:

CMy_Class** temp_array = new CMy_Class*[some_size];

i can happily use delete[] temp_array.

who should get the points??

cal
Check your array bounds. If you go beyond what you allocate, and then delete, it will crash badly in visual c++. I think it overwrites a end of data marker or something the system is using to know how much you have.

Above, you allocated an array of pointers(unsigned int on windows). Is that what you wanted??



Avatar of calum

ASKER

yeah - i populate the array with pointers to objects, so i can sort the objects on the member varibles using qsort, since i can't get a linked list of pointers to objects to sort using the sort method.

who gets the points? you;ve all been really helpful.

Avatar of calum

ASKER

everyone v helpful with this topic, even though i eventually found the answer myself. hope the thread is useful. calum