473,785 Members | 2,432 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

memory leakage in c++

hi all:
I have a code like below, is there any serious memory leakage in my
code. I am confusion now but no idea how to fix it. In the member
function of class A, I create a new object of class B like below:

void A::function()
{
B *newobject = new B;
newobject->....;
newobject->....; //did some action here
...

}
But I did not delete newobject in the A::function(acc tually, I cannot
delete it since I need to use it later. Will the newobject be
automatically destroy when the main function end? Is the memory leakage
problem serious in this case? Thanks a lot!

Jul 23 '05 #1
7 1778
>I cannot delete it since I need to use it later
Where do you use it ? From this example I can see that you can use it
only within A::function() .

You can make "newobject" a member of your class and delete it in
destructor.

Jul 23 '05 #2
>I cannot delete it since I need to use it later
Where do you use it ? From this example I can see what you can use
newobject only within A::function().

Make newobject member of your class and delete it in destructor.

Jul 23 '05 #3
what I did it now is like below:
in the .h file, I define a pointer point to class B;
and in the function, I assign the newobject to it.
and in the destructor, delete this pointer. Is this method safe and
efficient? Thanks
this is my code:
A.h file

class B;
B *pr;

A.cpp file:
A::~A()
{
delete pr;
}

void A::function()
{
B *newobject = new B;
pr=newobjet;
pr->....;
pr->....; //did some action here
...
}

Jul 23 '05 #4
an*****@hotmail .com wrote:
what I did it now is like below:
in the .h file, I define a pointer point to class B;
and in the function, I assign the newobject to it.
and in the destructor, delete this pointer. Is this method safe and
efficient? Thanks
Don't worry about "efficient" until you have code that works properly.
Hoare's Law: "Premature optimization is the root of all evil".
this is my code:
A.h file

class B;
B *pr;

A.cpp file:
A::~A()
{
delete pr;
}

void A::function()
{
B *newobject = new B;
pr=newobjet;
pr->....;
pr->....; //did some action here
...
}


Standard idiom. If an object owns a pointer, it deletes it upon
destruction.
Jul 23 '05 #5


an*****@hotmail .com wrote:
what I did it now is like below:
in the .h file, I define a pointer point to class B;
and in the function, I assign the newobject to it.
and in the destructor, delete this pointer. Is this method safe and
efficient? Thanks
this is my code:
A.h file

class B;
B *pr;

A.cpp file:
A::~A()
{
delete pr;
}

void A::function()
{
B *newobject = new B;
pr=newobjet;
pr->....;
pr->....; //did some action here
...
}


*pr is a global pointer, yet it has the lifetime of an A object,
specifficaly the lifetime of the FIRST destructed A object.
As other pointed, why isn't pr a member of A?

Dan

Jul 23 '05 #6


an*****@hotmail .com schreef:
what I did it now is like below:
in the .h file, I define a pointer point to class B;
You should put it in the class declaration.
and in the function, I assign the newobject to it.
and in the destructor, delete this pointer. Is this method safe and
efficient? Thanks
this is my code:
A.h file

class B;
B *pr;

A.cpp file:
A::~A()
{
delete pr;
}

void A::function()
{
B *newobject = new B;
pr=newobjet;
pr->....;
pr->....; //did some action here
...
}


Safe: not really. If you call A::function twice for one
A object, you have two calls to new but still only one
delete. If you have an A object but don't call A::function,
the delete will try to delete garbage.

There are better solutions. The first thing you need to do is put
pr in class A:

class B;
class A {
...
B* pr;
// then add the basic identity functions
A(); // will set pr to 0
A( A const& src ); // has to copy *(src.pr)
A& operator=( A const& src ); // has to replace this->pr
~A(); // clean up pr
};

then you have to deal with the possibility of pr already being
allocated:
void A::function()
{
if( pr==0 ) // from A::A()
pr = new B;
//...
}

You can also use std::auto_ptr<B > pr, it saves you a bit of work.

HTH,
Michiel Salters

Jul 23 '05 #7
an*****@hotmail .com schrieb:
what I did it now is like below:
in the .h file, I define a pointer point to class B;
and in the function, I assign the newobject to it.
and in the destructor, delete this pointer. Is this method safe and
efficient? Thanks
this is my code: A.h file

class B;
B *pr;
At first, make the pointer a member variable of the class. What about
initialization? Initialize it by NULL;
A.cpp file:
A::~A()
{
delete pr;
}

void A::function()
{
B *newobject = new B;


Test it before creation. What if the object already exists? Use the
constructor of A to create the object once.

Generally think about the new operator. This new creates an object on
the heap. If you just need a private object in some methods of class A
you can use a normal member variable (not a pointer):

class A {
B b; // define member variable of type B
A ()
: b() // construct member variable b
{
}
};

void A::function()
{
b. ...;
b. ...; // do actions here with b
...
}

This member variable does not need a new and requires also no
destructor in class A at all because the destructors of all member
variables are called automatically. This way is also most efficient
because no heap operation has been done.

T.M.
Jul 23 '05 #8

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

Similar topics

2
3736
by: frustrated | last post by:
Before I begin, I must first make the following disclaimer: Although I have considerable programming experience, I do not consider myself by any means to be an expert C++ programmer. The following may be nothing more than a relection of my ignorance. If what I describe is not an actual bug, I would be very appreciative if you could briefly explain to me how I can de-allocate memory allocated by a set class, since everything I have tried is...
2
1223
by: Sambucus | last post by:
Hi group! I am using C++ and java with JNI to get some text in a RICHEDIT to my java program. I do so by accessing a C++ method every second. It all works fine except that it leaks memory every call I make to the C++ method. Can anyone please help me with this problem? I am not sure wether the leakage is in the C++ or java code. But i guess it's on the C++ side since I'm kind of newbee there.
10
2782
by: s.subbarayan | last post by:
Dear all, I happen to come across this exciting inspiring article regarding memory leaks in this website: http://www.embedded.com/story/OEG20020222S0026 In this article the author mentions: "At a certain point in the code you may be unsure if a particular block is no longer needed. If you free() this piece of memory, but continue to access it (probably via a second pointer to the same
18
2274
by: Ramasubbu Ramasubramanian XR (AS/EAB) | last post by:
What is memory leakage, could any one explain with sample code
1
3806
by: Gaël | last post by:
Hi everybody! I have a really big problem with ASP.NET application. I noticed that the w3wp.exe memory size, increase with the time and the use of my website. When it raise a certain value, w3wp crashes and restart just after. My application is on a WebServer2003. So I have to resolve 2 problem : -Why the memory size of the w3wp increase non stop. - Why the w3wp crashes (in the case where there is no link between those
0
1370
by: kiran kumar | last post by:
Hi All, I am working on embedded python on C these days. I feel there is a memory leakage in this code. I have used our own memory pool and all the python code will use the heap from this memory pool. RunScript(pScriptName,pFuncName,...) { PyEval_AcquireLock() threadState = Py_NewInterpreter(); PyThreadState_Swap(threadState);
14
2349
by: madhawi | last post by:
i want to know that on what situation memory leakage happan and what is the solution to solve the problem of memory leakage.
3
2569
by: Godzilla | last post by:
Hello, I have a program that create and pop an object off a queue, but it is experiencing some memory leakage. I have been unable to detect where the memory leakage occur. The strange thing is when i replace the object creation with a plain integer/string, the leak goes away... Here's the code I used as my test: import Queue
11
2308
by: prpradip | last post by:
I have an ImageList (_imageList). In _imageList I have put large numbers of Icons. Now what I need is to get Handle of all Icons that I put in _imageList, so that I can destroy (DestoryIcon) them all and release the memory because it's causing Memory leakage.
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.