0

Here is my problem, basically I am working on graphs and path finding in C++. I am facing some crashes because I run out of memory. I used a detection leakmemory tool in way to help to find the problem and it occurs in my copyList function :

template<class T>
PElement<T> * PElement<T>::copieListe(PElement<T> * original)
{
    if (!original)
    {
        return NULL;
    }
    else
    {    
        return new PElement<T>(new T(*original->v),copieListe(original->s));
    }
} 

Here is the PElement class :

template <class T>
class PElement
{
public :
T * v;
PElement<T> * s;
}

The constructor called in copyList :

template <class T>
PElement<T>::PElement( T * v, PElement<T> * s ):v(v),s(s){}

I am using this function a thousand times... so there needs to be no memory leaks at all. My graphs got a list of egdes and vertex, so you know what is this about :

template <class S, class T>
class Graphe
{
protected:
int prochaineClef;
public:

PElement< Sommet<T> > * lSommets; // liste de sommets (vertices)
PElement< Arete<S,T> > * lAretes; // liste d'arêtes (edges)
}

I know it's not the easiest/best way to do that but it was not my choice, I am just asking if you have idea for how I can limit the memory leak..

tux3
  • 7,171
  • 6
  • 39
  • 51
rilent
  • 659
  • 1
  • 6
  • 22
  • Using smart pointers and standard library containers is a good way to prevent leaks. – Jonathan Potter Mar 03 '15 at 22:34
  • For the love of all that is holy, if you're going to share lists of pointers around, use some form of smart pointer, not a raw pointer. – aruisdante Mar 03 '15 at 22:35
  • i got no choice about the PElement stuff. i wish i could have store everything in vectors. What do you mean by smart pointers? edit : i found some infos here about smart pointers : http://stackoverflow.com/questions/417481/pointers-smart-pointers-or-shared-pointers . i can't use them – rilent Mar 03 '15 at 22:37
  • If you're on C++ 11 or newer, [`std::shared_ptr`](http://en.cppreference.com/w/cpp/memory/shared_ptr). If not and you can use boost, [`boost::shared_ptr`](http://www.boost.org/doc/libs/1_57_0/libs/smart_ptr/shared_ptr.htm). Both are implementations of a [smart pointer](http://en.wikipedia.org/wiki/Smart_pointer) – aruisdante Mar 03 '15 at 22:38

1 Answers1

0

You are allocating memory in your copylist function's return statement.

In order to avoid a memory leak, whoever calls copylist needs to delete the item you returned from copylist after they are done using it.

Curtis
  • 5,794
  • 8
  • 50
  • 77
  • I see... but it's kinda tricky here, no way to do better my copylist? – rilent Mar 03 '15 at 22:47
  • Use an array PElement[1000] instead of PElement *. You indicated that you use it 1000 times. Do you know for sure the size of the data you are needing to copy? Perhaps you could just allocate the array, put a constant somewhere like 'const in ARRAY_SIZE=1000' and then whoever calls you, can go through all 1000 elements and delete them in a loop. Also, you can copy all the PElements in a loop. – Curtis Mar 03 '15 at 23:16
  • Another way would be to create your own container class that has its own destructor that destroys all elements in it and can generate a copy of itself which can in turn be destroyed.. Sorry, it's been 20 years since I programmed in C. C++ has been about 10 years ago. C# is my current language of choice... – Curtis Mar 03 '15 at 23:18