473,387 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

question about auto_ptr


Does the code below solve memory leaks on variable m if there is an
exception thrown or do I have to cleanup the auto_ptr myself ?

void A::f()
{
try
{
auto_ptr<Objectm(new Object());

some exception thrown

}
catch(MyException& ex)
{
// some stuff
}
}
Jun 27 '08 #1
2 991
Bruintje Beer wrote:
Does the code below solve memory leaks on variable m if there is an
exception thrown or do I have to cleanup the auto_ptr myself ?
No, that's the function of std::auto_ptr.
void A::f()
{
try
{
auto_ptr<Objectm(new Object());

some exception thrown
the object will be freed here.
>
}
catch(MyException& ex)
{
// some stuff
}
}
--
Ian Collins.
Jun 27 '08 #2
Bruintje Beer a écrit :
Does the code below solve memory leaks on variable m if there is an
exception thrown or do I have to cleanup the auto_ptr myself ?
As soon as you put your pointer in auto_ptr, you're assured it will be
deleted when the auto_ptr is destroyed.
void A::f()
{
try
{
auto_ptr<Objectm(new Object());

some exception thrown

}
catch(MyException& ex)
{
// some stuff
}
}
In this code, the auto_ptr is destroyed at the end of the try block
(exception thrown or not), so there is no memory leak.

Cheers,
--
Vincent Jacques

"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème"
Devise Shadock
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Lloyd Dupont | last post by:
how do I redefine the new operator? for all the structure I use at once! (some of them comes from C include files). The rationale: I'm writting a managed C++ wrapper around C API (external...
2
by: flopbucket | last post by:
I saw the following post a few messages back and am trying to understand exactly why this is so. Thanks for the information. My comments begin with **. The original post was arguing that STL...
16
by: Yong Hu | last post by:
Does the smart pointer never lead to memory leak? Thanks,
2
by: timlyee | last post by:
int *p = new int; auto_ptr<intap1 = p; //will fail on 3 1 auto_ptr<intap1(p); //ok 2 *ap1 = 12; // 3 the first situation has called :...
2
by: dolphin | last post by:
Hi All Today , I read the source code of auto_ptr. I have a question about the copy constructor of auto_ptr. template<class _Ty> class auto_ptr { .................. } there is a copy...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.