octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory Leak in Array<T> class


From: David Bateman
Subject: Re: Memory Leak in Array<T> class
Date: Tue, 2 Nov 2004 10:52:16 +0100
User-agent: Mutt/1.4.1i

According to John W. Eaton <address@hidden> (on 11/01/04):
> If this code caused a memory leak, then running the above in a loop
> like this:
> 
>   page_screen_output = 0;
>   while (1)
>     foo;
>   endwhile
> 
> or, to speed things up a bit, modifying it to be
> 
>   #include <octave/config.h>
>   #include <octave/oct.h>
> 
>   DEFUN_DLD (xxx, args, , " ")
>   {
>     NDArray a;
>     return octave_value ();
>   }
> 
> and running the same loop would cause Octave to grow without bound
> (even if the growth was slow).  But that is not what I see.
> 
> The key is the definition of the nil_rep function:
> 
>   typename Array<T>::ArrayRep *nil_rep (void) const
>     {
>       static typename Array<T>::ArrayRep *nr
>       = new typename Array<T>::ArrayRep ();
> 
>       return nr;
>     }
> 
> Note that the returned value is static within the function, so it is
> constructed just once, when NR is initialized.  Each time nil_rep is
> called, we return the same empty ArrayRep object.  It is correct to
> increment the reference count for this object in the constructors,
> because we never want the static NR object to be destroyed.  Defining
> nil_rep this way is an attempt to speed things up by avoiding
> unnecessary calls to new and delete to repeatedly create an empty
> ArrayRep object.  Since all empty ArrayRep objects have the same
> representation, we should only have to create it once.


Amazing what a difference a single "static" keyword makes in this
case. I'd missed that...

Cheers
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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]