473,404 Members | 2,170 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,404 software developers and data experts.

memory leak in constructor and during object creation

Will the below code cause memory leak in c++

class base{
int a;
int *pint;
someclass objsomeclass;
someclass* psomeclass;
base(){
objsomeclass = someclass();
psomeclass = new someclass();
pint = new int();
throw "constructor failed";
a = 43;
}
}
main(){
base temp();
}

in the above code constructor fails.Which objects will be leaked and
how to avoid memory leak and even if destructor is called for
automatic object is temp and temp pointer will leak the memory?

main(){
base *temp = base();
}

what about in the above code and how to avoid memory leak when
constructor fails
Sep 29 '08 #1
4 2186
raj s wrote:
Will the below code cause memory leak in c++

class base{
int a;
int *pint;
someclass objsomeclass;
someclass* psomeclass;
base(){
objsomeclass = someclass();
psomeclass = new someclass();
pint = new int();
throw "constructor failed";
a = 43;
}
}
main(){
base temp();
}

in the above code constructor fails.Which objects will be leaked and
how to avoid memory leak and even if destructor is called for
automatic object is temp and temp pointer will leak the memory?
Wrap them in something that frees the memory, such as auto_ptr or an
appropriate smart pointer.
main(){
base *temp = base();
}

what about in the above code and how to avoid memory leak when
constructor fails
There isn't one.

--
Ian Collins.
Sep 29 '08 #2
On Sep 29, 7:33*pm, raj s <yesr...@gmail.comwrote:
Will the below code cause memory leak in c++

class base{
* * int a;
* * int *pint;
* * someclass objsomeclass;
* * someclass* psomeclass;
* * base(){
* * * * objsomeclass = someclass();
* * * * psomeclass = new someclass();
* * * * pint = new int();
* * * * throw "constructor failed";
* * * * a = 43;
* * }}
Should be a semicolon after this. Now, as
you suspect, if you create a 'base' then
you get a memory leak because the memory
allocated by "new someclass()" and "new int()"
is never freed.

The best solution is to design your class
to not 'new' things. I guess you are coming
from Java or something like that by your code
stype. In C++ you rarely need to have classes
that 'new' their members. For example, in
the above code, there is no need to 'new' an int.

Failing that, you will have to use
'smart pointers' instead of raw pointers.
For example, if instead of:
someclass *psomeclass;
you could have:
auto_ptr<someclasspsomeclass;

Note that you can initialize things in
the constructor initializer list:
base(): psomeclass( new someclass ) {.....}

Some comments on the rest of your code:
>
main(){
* * base temp();
This doesn't declare any objects, it declares
a function. I think you meant:
base temp;

You seem to have a fascination with useless
parentheses :)

Sep 29 '08 #3
"raj s" <ye*****@gmail.comwrote in message news:a0**********************************@8g2000hs e.googlegroups.com...
Will the below code cause memory leak in c++

class base{
int a;
int *pint;
someclass objsomeclass;
someclass* psomeclass;
base(){
objsomeclass = someclass();
psomeclass = new someclass();
pint = new int();
throw "constructor failed";
a = 43;
}
}
main(){
base temp();
}

in the above code constructor fails.Which objects will be leaked and
how to avoid memory leak and even if destructor is called for
automatic object is temp and temp pointer will leak the memory?

main(){
base *temp = base();
}

what about in the above code and how to avoid memory leak when
constructor fails
If you really must use new and if you cannot use a smart pointer type,
than you should handle the exception yourself. E.g.:

base()
: pint (0), psomeclass (0)
{
try {
objsomeclass = someclass();
psomeclass = new someclass();
pint = new int();
throw "constructor failed";
a = 43;
} catch (...) {
delete pint;
delete psomeclass;
throw;
}
}

Sep 29 '08 #4
raj s schrieb:
Will the below code cause memory leak in c++
Yes. All pointer objects will never be destroyed, since your programm
does not provide a delete statement.
class base{
int a;
int *pint;
someclass objsomeclass;
someclass* psomeclass;
base(){
objsomeclass = someclass();
psomeclass = new someclass();
pint = new int();
throw "constructor failed";
a = 43;
}
}
main(){
base temp();
}
in the above code constructor fails.Which objects will be leaked and
how to avoid memory leak
Use smart pointers.
and even if destructor is called for
automatic object is temp and temp pointer will leak the memory?
The destructor will not be called if the constructor fails. But the
destructor for a successfully constructed base members is called -
normally. In your case, you do not catch your exception. That usually
causes a call to abort(), which will end your programm immediately
without any cleanup. From that point it is up to the operating system to
do the cleanup.
main(){
base *temp = base();
}

what about in the above code and how to avoid memory leak when
constructor fails
The memory of base is normally freed in this case, since no object is
successfully constructed. But again, you must catch your exception to
prevent your programm from an immediate abort.
Marcel
Sep 29 '08 #5

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

Similar topics

5
by: Jason Callas | last post by:
I was doing starting some experimenting with the GC and ran into the following odd result. I created an object and my used memory went up by almost 11k but when I cleared it and forced a collection...
32
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes...
10
by: Jonathan Ames | last post by:
Moving to C++ from Java, I'm still confused by some aspects of memory cleanup operations. For example, let's say I have a class MovingObject which maintains a pointer to another class...
4
by: Morten Aune Lyrstad | last post by:
Ok, now I'm officially confused. I have a large project going, which uses a win32 ui library I am developing myself. And I'm getting weird memory leaks. I don't know if I can explain what is going...
4
by: jason | last post by:
my w3wp.exe process has recently gotten an ever-expanding memory footprint. the two most likely causes are either a memory leak in the object library it uses, or session variables not getting...
23
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
8
by: Adrian | last post by:
Hi I have a JS program that runs localy (under IE6 only) on a PC but it has a memory leak (probably the known MS one!) What applications are there that I could use to look at the memory usage of...
9
by: benoit808 | last post by:
I don't have a lot of experience with C++ so I apologize if this is a stupid question. I use Paul Nettle's memory manager (mmgr.cpp) which reports a memory leak but I don't think there's one. Here...
1
by: cdmsenthil | last post by:
I have an Infragistics UltrawebGrid . Each Row in the grid is attached to a context menu using Infragistics CSOM Upon click on the menu, I am creating an Iframe dynamically which points to...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.