public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* C++ auto_ptr template query
@ 2006-05-23 10:03 Digvijoy Chatterjee
       [not found] ` <EDC8DDD212FEB34C884CBB0EE8EC2D9121649D@namailgen.corp.adobe.com>
  0 siblings, 1 reply; 2+ messages in thread
From: Digvijoy Chatterjee @ 2006-05-23 10:03 UTC (permalink / raw)
  To: gcc-help

Hello,
Can anyone help me find a way  prevent memory leaks in the type of code below
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++ == 2)
      throw 46;
  }
   ~Motor (){}
};

class Car
{
public:
    Car ()
  {
    a = new int[10];
    M1 = new Motor;
  }
  ~Car ()
  {
    delete []a;
    delete M1;
  }
private:
  int *a;
  Motor * M1;
};

int  Motor::i = 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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: C++ auto_ptr template query
       [not found] ` <EDC8DDD212FEB34C884CBB0EE8EC2D9121649D@namailgen.corp.adobe.com>
@ 2006-05-24  4:54   ` Digvijoy Chatterjee
  0 siblings, 0 replies; 2+ messages in thread
From: Digvijoy Chatterjee @ 2006-05-24  4:54 UTC (permalink / raw)
  Cc: gcc-help

On 5/24/06, John (Eljay) Love-Jensen <eljay@adobe.com> wrote:
> HI Digvijoy,
>
> std::auto_ptr is for holding a single object, not arrays of objects.
>
> Don't use std::auto_ptr to hold arrays of objects.
>
> Use a std::list<Car> instead.
> try
> {
>   std::list<Car> carList;
>   carList.push_back(Car());
>   carList.push_back(Car());
>   carList.push_back(Car());
> }
>
> Or I bet Boost (www.boost.org) has something like a std::auto_ptr that holds arrays of objects.
>
> HTH,
> --Eljay
>
>
Hello Eljay,
I tried using std::list<Car> as you have suggested above, the code as
above runs but tells me :
*** glibc detected *** double free or corruption (fasttop): 0x0804b038 ***
and  i loose 40 bytes, (the last Car when it allocated 10 ints but the
Motor fails )
if I comment the delete M ,and delete []a in the Car destructor
method, there is no double free corruption detected ,
  ~Car ()
 {
   //delete []a;
   //delete M1;
 }
 but when i run valgrind :
==32159== LEAK SUMMARY:
==32159==    definitely lost: 122 bytes in 5 blocks.
which is i think
[(4 * 10 ints) *3 Car objs ]=120
[ (1*1Motor) * 2 Car objs ] =2
Any  suggestions ???
Digz

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2006-05-24  4:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-05-23 10:03 C++ auto_ptr template query Digvijoy Chatterjee
     [not found] ` <EDC8DDD212FEB34C884CBB0EE8EC2D9121649D@namailgen.corp.adobe.com>
2006-05-24  4:54   ` Digvijoy Chatterjee

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).