Thread: Safely deleting an array as a data member of a class

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    206

    Cool Safely deleting an array as a data member of a class

    Hi,

    Long story short I had to add an array as a data member to an existing class in my game engine. I would like to know if in the class's destructor or in it's release member function do I need to add special code to delete this array without causing a memory leak?

    The array has been initialised with a fixed size as shown, it is not just a pointer:

    D3DXHANDLE* myArray[10];

    Will the class's destructor take care of this for me? With all other attributes which point to objects such as DirectX objects special code is needed and you have to loop through each entry and call the release function of each object contained in the array.

    D3DXHANDLE is not an object though, it is a LPCTSTR variable. Similar-ish to a char pointer so I hear.

    Any ideas? I'd like to do this properly. I could advance beyond this right now but I'd rather keep the brakes on before I compile this DLL file and leave it be.

    Much thanks,

    Chris

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    How does each element of myArray get allocated?

  3. #3
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    the default destructor will delete myArray, but not the objects being pointed to in its elements.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    206
    Ok thanks, so I can assume from this that the class's destructor will kill off the pointer variable myArray (not it's real name - used in this thread as a demo). But what about the variables still left in memory that this pointer once referred to?

    There is no dymanic allocation here, just plain old stuff, and the things in the array elements are not objects per se. More like standard variables.

    So what do I need to do - if anything - to guarantee the prevention of memory leaks here? What would I need to do if it was an array of chars for example?

    Thanks.

  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    to prevent memory leaks, come up with a model for controlling the lifetime and destruction of anything you instantiate with the new operator. if you never call new then there should be no need to call delete.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    120
    Quote Originally Posted by shrink_tubing View Post
    So what do I need to do - if anything - to guarantee the prevention of memory leaks here?
    well, you could add this in the destructor:
    Code:
    for ( unsigned int i = 0; i < myArraySize, ++i ) {
    
        delete myArray[ i ];
    
    }
    If u didnt knew the arrays size u could use sizeof():
    Code:
    for ( unsigned int i = 0; i < sizeof(myArray) / sizeof(char*), ++i )
    note that this could cause a crash if the poiter is unitialized, so i realy recoment using a myArraySize variable to keep track of the number of objects alocated.

    this should clear the array,
    I hope it helped and i also hope i didnt screw up in the explanation :P

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    206

    Post Thanks

    That's very good information thanks to you both.

    Out of interest here is the real data member. Luckily there are no copies of this class so I don't need to worry about multiple class copies with pointers as data members:

    Code:
    D3DXHANDLE*             m_pTechnique[MAX_SHADER]
    It's initialised with a constant value right from the moment of the class's creation. I think therefore it is reasonable to run a delete for loop as the last poster suggests on every element. I do worry what might happen if I ran delete on an element of the array which was empty, but I would think that is not possible when using a constant to initialise the size of the array.

    Perhaps as always I worry too much. In fact for that matter I am still unsure if I need an array of pointers as shown or just an array of variables. I need to check and see exactly what the function they're being prepared for demands.

    Amazes me constantly how I can get so far in programming only to trip over basic stuff all the time, even when it's stuff I know I've already learned.

    C++ sure is a big place.

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    206
    As it happens I was able to change the function call slightly and change the array data member to the following:

    Code:
    D3DXHANDLE              m_pTechnique[MAX_SHADER];
    This is what I'll probably stick with now, I find it easier not to use pointers where I don't have to. Got a bit confused there sorry, mind you a bit of confusion always helps I think with learning once it's gone through.

    I'm stil la bit worried though about undefined behaviour if I ran a delete for loop. I have found a good page online which should fill me in a bit better on exactly what the class default constructor can handle, and what it can't.

    Thanks, if anyone else wants to add anything feel free

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    120
    Quote Originally Posted by shrink_tubing View Post
    I do worry what might happen if I ran delete on an element of the array which was empty, but I would think that is not possible when using a constant to initialise the size of the array.
    it is indeed possible, for example:
    if i declared a pointer and did not initialize it like so:
    Code:
    int* myPointer;
    that pointer is pointing to a random location in memory (because it holds a value that is in its current mem location that was put there by some other program), the same happens to all the pointers in your array, so if u try to delete one of them the program will crash because the system will detect that you're trying to access memory that doesnt "bellong" to the program.

    This used to bite me in the arss ALOT ( never was really an user of vectors :P )

    Quote Originally Posted by shrink_tubing View Post
    In fact for that matter I am still unsure if I need an array of pointers as shown or just an array of variables. I need to check and see exactly what the function they're being prepared for demands.
    ill try to give you an hint,
    If u need to keep a pointer with a reference to one of the array's objects then DEFINITELY use an array of pointers, else you can use a "normal" dynamic array (only a pointer).

    The good things about arrays of pointers is that
    1. they dont change the location of its objects in memory, so you can safely have references to those objects ( with a "normal" array each time u expand it, it will have to recreate every object because else it wouldnt have space to store a new one ).
    2. they are WAY faster to decrement and increment because u are copying pointers instead of full classes so if u want dynamic arrays that are supose to become big this is IMO the method to go for.

    hope i added something else

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by shrink_tubing View Post
    It's initialised with a constant value right from the moment of the class's creation. I think therefore it is reasonable to run a delete for loop as the last poster suggests on every element.
    If you mean that the contents of the array are initialized to a constant value then don't use delete.
    If you mean that just the size of the array is a constant value then you might need to use delete, depending on what the pointers point to.
    Quote Originally Posted by shrink_tubing View Post
    I do worry what might happen if I ran delete on an element of the array which was empty, but I would think that is not possible when using a constant to initialise the size of the array.
    An array element cannot be empty, it will always contain something. It can be uninitialized though, and calling delete on an uninitialized pointer would be bad.
    If you have an array of pointers I would suggest initializing all the elements to NULL. It's safe to delete NULL pointers so then you can loop over the array and call delete on every element.
    But like m37h0d said; If you (or any function you call) haven't allocated any memory with new then you shouldn't use delete either.

    Quote Originally Posted by shrink_tubing View Post
    Perhaps as always I worry too much. In fact for that matter I am still unsure if I need an array of pointers as shown or just an array of variables. I need to check and see exactly what the function they're being prepared for demands.
    I'm assuming this is for directx programming and that you didn't create your own D3DXHANDLE type..
    As far as I know D3DXHANDLEs are basically just integers, so you should never call delete on one. I think the reason they are typedef'ed to char pointers is because some d3d functions can take either a handle or a c-string as an argument. Though I might be completely wrong about this so don't take it as fact
    Last edited by _Mike; 06-13-2011 at 10:43 AM.

  11. #11
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by shiroaisu View Post
    well, you could add this in the destructor:
    Code:
    for ( unsigned int i = 0; i < myArraySize, ++i ) {
    
        delete myArray[ i ];
    
    }
    while this would clear the array, according to the information given by shrink tubing; this would cause an address of invalid pointer error elsewhere.

  12. #12
    Registered User
    Join Date
    Jan 2010
    Posts
    206
    That is excellent, overwhelmingly good information. Thanks so much to you all that is all I need to know for now. Cheers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class with pointers to other classes as member data.
    By Sclorch in forum C++ Programming
    Replies: 2
    Last Post: 02-09-2009, 05:59 AM
  2. How to declare const data member in class
    By chottachatri in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2008, 04:54 AM
  3. Reference another class's data member...
    By RoD in forum C++ Programming
    Replies: 4
    Last Post: 02-15-2003, 11:20 AM
  4. using a class as a data member before you initialize the class?
    By class question in forum C++ Programming
    Replies: 6
    Last Post: 01-05-2003, 04:25 PM
  5. Deleting Data within a Structure or class
    By TankCDR in forum C++ Programming
    Replies: 1
    Last Post: 02-01-2002, 10:37 PM