[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Memory Leak in Array<T> class
From: |
David Bateman |
Subject: |
Memory Leak in Array<T> class |
Date: |
Sat, 30 Oct 2004 02:56:33 +0200 |
User-agent: |
Mutt/1.4.1i |
All of the void constructors derived from Array<T> have a memory leak. The
bit of the class that describes the problem is
template <class T>
class
Array
{
protected:
class ArrayRep
{
public:
ArrayRep (void) : data (0), len (0), count (1) { }
};
private:
typename Array<T>::ArrayRep *nil_rep (void) const
{
static typename Array<T>::ArrayRep *nr
= new typename Array<T>::ArrayRep ();
return nr;
}
Array (void)
: rep (nil_rep ()), dimensions (),
idx (0), idx_count (0) { rep->count++; }
};
So what happens is that the void constructor calls nil_rep which creates
the internal representation and adds one to the count. A new ArrayRep
is created for each void constructor. However the constructor itself
then adds 1 to rep->count and the ArrayRep therefore becomes immortal
as its count can never reach 1, when the Array<T>::~Array destructor
is called. This can easily be demostrated with the code snippet.
#include <octave/config.h>
#include <octave/oct.h>
DEFUN_DLD (foo, args, , " ")
{
NDArray a;
a.print_info (octave_stdout, " ");
return octave_value ();
}
which returns
octave:1> foo
rep address: 0x8a4fee8
rep->len: 0
rep->data: 0
rep->count: 2
Regards
David
--
David Bateman address@hidden
Motorola CRM +33 1 69 35 48 04 (Ph)
Parc Les Algorithmes, Commune de St Aubin +33 1 69 35 77 01 (Fax)
91193 Gif-Sur-Yvette FRANCE
The information contained in this communication has been classified as:
[x] General Business Information
[ ] Motorola Internal Use Only
[ ] Motorola Confidential Proprietary
- Memory Leak in Array<T> class,
David Bateman <=