* 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
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).