octave-maintainers
[Top][All Lists]
Advanced

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

Array<T>::nil_rep leaks memory


From: John W. Eaton
Subject: Array<T>::nil_rep leaks memory
Date: Mon, 22 Sep 2008 16:02:19 -0400

On 19-Sep-2008, Jaroslav Hajek wrote:

| in the function Array<T>::nil_rep a static pointer to an ArrayRep is
| declared and allocated via new. Unless I'm mistaken, such a pointer is
| never deleted and causes a memory leak. The attached patch fixes this
| by using a static variable instead and returning its address.

| # HG changeset patch
| # User Jaroslav Hajek <address@hidden>
| # Date 1221808946 -7200
| # Node ID 45b534558b2276b9fc85c5f1871102e199942acf
| # Parent  31e86163b7529285a16092625c3ae8ec0d986f92
| fix memory leak in Array
| 
| diff --git a/liboctave/Array.h b/liboctave/Array.h
| --- a/liboctave/Array.h
| +++ b/liboctave/Array.h
| @@ -163,10 +163,9 @@
|  
|    typename Array<T>::ArrayRep *nil_rep (void) const
|      {
| -      static typename Array<T>::ArrayRep *nr
| -     = new typename Array<T>::ArrayRep ();
| +      static typename Array<T>::ArrayRep nr;
|  
| -      return nr;
| +      return &nr;
|      }
|  
|    template <class U>
| diff --git a/liboctave/ChangeLog b/liboctave/ChangeLog
| --- a/liboctave/ChangeLog
| +++ b/liboctave/ChangeLog
| @@ -0,0 +1,5 @@
| +2008-09-19  Jaroslav Hajek  <address@hidden>
| +
| +     * Array.h (Array<T>::nil_rep): Use static variable instead of
| +     allocating static pointer.
| +

The object pointed to by Array<T>::rep should be allocated by new,
otherwise calling delete on it in the destructor would be an error.
So I think this code only works because delete is never called for the
value returned by nil_rep.  So does it really matter?  It seems more
correct to me that the Array<T>::rep pointer should always point to an
object allocated with new.

jwe


reply via email to

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