From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 8381 invoked by alias); 23 May 2006 10:03:06 -0000 Received: (qmail 8371 invoked by uid 22791); 23 May 2006 10:03:06 -0000 X-Spam-Check-By: sourceware.org Received: from wx-out-0102.google.com (HELO wx-out-0102.google.com) (66.249.82.195) by sourceware.org (qpsmtpd/0.31) with ESMTP; Tue, 23 May 2006 10:03:03 +0000 Received: by wx-out-0102.google.com with SMTP id s18so829030wxc for ; Tue, 23 May 2006 03:03:02 -0700 (PDT) Received: by 10.70.65.9 with SMTP id n9mr6203367wxa; Tue, 23 May 2006 03:03:01 -0700 (PDT) Received: by 10.70.90.3 with HTTP; Tue, 23 May 2006 03:03:01 -0700 (PDT) Message-ID: <9dd10b1d0605230303i55809b1fgc88b838c3227f29e@mail.gmail.com> Date: Tue, 23 May 2006 10:03:00 -0000 From: "Digvijoy Chatterjee" To: gcc-help@gcc.gnu.org Subject: C++ auto_ptr template query MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline X-IsSubscribed: yes Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2006-05/txt/msg00224.txt.bz2 Hello, Can anyone help me find a way prevent memory leaks in the type of code bel= ow i tried auto_ptr template but I am still loosing 40 bytes. ([10] ints * 4 in the Class Car) --------------------------------------------------------- class Motor { public: static int i; Motor () { if (i++ =3D=3D 2) throw 46; } ~Motor (){} }; class Car { public: Car () { a =3D new int[10]; M1 =3D new Motor; } ~Car () { delete []a; delete M1; } private: int *a; Motor * M1; }; int Motor::i =3D 0; int main ( ) { try { auto_ptr < Car > auto_car (new Car[3]); } catch (int) { cout << "Car caught an int thrown from motor" << endl; } } ------------------- The Car[3] object is not fully constructed ,so its destructor is not called ,but auto_car is and so when the try scope ends ,auto_car should ensure that the object it wraps gets deleted which should call delete [] a and remove int*a[10], thats my flawed logic i suppose ,correct me if this is wrong Regards Digz