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 execution. But I do not think it needs
so much memory. About 500M memory should be enough. I have following
questions about memory leak.
(1).If in my code I only define constructor for my class, and do not
define destructor, will it cause memory leak?
(2).If in my code I only use "new" to declare new object, and do not
use "delete", will it cause memory leak?
For example, in the following code:
void class1::functio n1()
{
class2 *r1;
class2 *r2 = new class2;
class2 *rr[20];
......
function2(rr);
......
//r1 and r2 are also used in function1().
}
In the above code, I have two classes and I define constructor for the
two classes and do not define destructor. In function1(), I declare
two pointers of class2 and an array of pointer of class2. The array rr
is used to bring back values from function2(). For r2, I do not use
"delete".
Will r1, r2 and the array rr[] cause memory leak?
The two pointers, r1 and r2, and array rr[] are local variables, when
the code exits function1(), these local variables should be released
automatically.
Am I right?
Thanks a lot.
John 32 3848
"John" <jo*********@ya hoo.com> wrote in message
news:c3******** *************** ***@posting.goo gle.com... 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 execution. But I do not think it needs so much memory. About 500M memory should be enough. I have following questions about memory leak. (1).If in my code I only define constructor for my class, and do not define destructor, will it cause memory leak?
Depends on the class.
(2).If in my code I only use "new" to declare new object, and do not use "delete", will it cause memory leak?
Yes.
Its a very simple rule, nothing to do with constructors or destructors. When
your program runs every new allocates some memory, if you don't do a delete
for the same memory then you have a memory leak. For example, in the following code:
void class1::functio n1() { class2 *r1; class2 *r2 = new class2; class2 *rr[20];
......
function2(rr);
...... //r1 and r2 are also used in function1().
}
In the above code, I have two classes and I define constructor for the two classes and do not define destructor.
That's irrelevant.
In function1(), I declare two pointers of class2 and an array of pointer of class2. The array rr is used to bring back values from function2(). For r2, I do not use "delete". Will r1, r2 and the array rr[] cause memory leak?
Where are the deletes? There are no deletes so there are memory leaks all
over the place.
The two pointers, r1 and r2, and array rr[] are local variables, when the code exits function1(), these local variables should be released automatically. Am I right?
Wrong. The variables destructed and the memory they occupy is 'released',
BUT the memory they might be pointing to is not released.
void f()
{
X x;
X* xp = new X();
...
}
When you get to the end of this function, the memory for x and xp are both
released. That has nothing to do with the memory pointed to by xp, which is
a completely different thing. The memory for a pointer and the memory that
it points to are not the same thing.
It's very simple, every new must be matched by a delete.
john
"John" <jo*********@ya hoo.com> wrote in message
news:c3******** *************** ***@posting.goo gle.com... 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 execution. But I do not think it needs so much memory. About 500M memory should be enough. I have following questions about memory leak. (1).If in my code I only define constructor for my class, and do not define destructor, will it cause memory leak? (2).If in my code I only use "new" to declare new object, and do not use "delete", will it cause memory leak?
For example, in the following code:
void class1::functio n1() { class2 *r1; class2 *r2 = new class2; class2 *rr[20];
......
function2(rr);
...... //r1 and r2 are also used in function1().
}
In the above code, I have two classes and I define constructor for the two classes and do not define destructor. In function1(), I declare two pointers of class2 and an array of pointer of class2. The array rr is used to bring back values from function2(). For r2, I do not use "delete". Will r1, r2 and the array rr[] cause memory leak? The two pointers, r1 and r2, and array rr[] are local variables, when the code exits function1(), these local variables should be released automatically. Am I right?
Thanks a lot.
John
Hi,
check out "auto_ptr" from any standard c++ boooks.
kutty
John wrote: 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 execution. But I do not think it needs so much memory. About 500M memory should be enough. I have following questions about memory leak. (1).If in my code I only define constructor for my class, and do not define destructor, will it cause memory leak?
See next answer! (2).If in my code I only use "new" to declare new object, and do not use "delete", will it cause memory leak?
Yes, without a destructor, how is the memory you allocated in the
destructor freed?
An alternative is to use std::auto_ptr rather than a plain pointer.
As a rule, any class with pointers must have a destructor and copy
constructor (if nothing else, this makes you think about ownership of
the data you reference through a pointer).
Ian
For example, in the following code:
void class1::functio n1() { class2 *r1; class2 *r2 = new class2; class2 *rr[20];
......
function2(rr);
...... //r1 and r2 are also used in function1().
}
In the above code, I have two classes and I define constructor for the two classes and do not define destructor. In function1(), I declare two pointers of class2 and an array of pointer of class2. The array rr is used to bring back values from function2(). For r2, I do not use "delete". Will r1, r2 and the array rr[] cause memory leak? The two pointers, r1 and r2, and array rr[] are local variables, when the code exits function1(), these local variables should be released automatically. Am I right?
Thanks a lot.
John
"Ian" <no***@nowhere. com> wrote in message
news:10******** *******@drone5. qsi.net.nz... John wrote: 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 execution. But I do not think it needs so much memory. About 500M memory should be enough. I have following questions about memory leak. (1).If in my code I only define constructor for my class, and do not define destructor, will it cause memory leak?
See next answer! (2).If in my code I only use "new" to declare new object, and do not use "delete", will it cause memory leak? Yes, without a destructor, how is the memory you allocated in the destructor freed?
An alternative is to use std::auto_ptr rather than a plain pointer.
As a rule, any class with pointers must have a destructor and copy constructor (if nothing else, this makes you think about ownership of the data you reference through a pointer).
Ian
I think the OP is not talking about a class with pointers but a pointer to a
class. Certainly that is what his code shows.
In any case the rule is every new must be matched with a delete.
Constructors and destructors are just a useful way of making sure that this
rule is followed.
Since the OP is clearly struggling with pointers I would advise avoid using
new where possible. It's certainly a common newbie trait to use new where it
isn't necessary.
// newbie style coding
void func()
{
X* xp = new X;
x->some_func();
delete x;
}
// simpler, safer and better coding
void func()
{
X x;
x.some_func();
}
There's nothing in the code that John posted that indicated he must use new
at all.
john
John wrote: 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 execution. But I do not think it needs so much memory. About 500M memory should be enough. I have following questions about memory leak. (1).If in my code I only define constructor for my class, and do not define destructor, will it cause memory leak?
That depends on what the class does. If it allocates any resources, it
should have a destructor that deallocates them.
(2).If in my code I only use "new" to declare new object, and do not use "delete", will it cause memory leak?
Yes. Everything you got from new should be deleted as soon as you don't
need it anymore.
For example, in the following code:
void class1::functio n1() { class2 *r1; class2 *r2 = new class2; class2 *rr[20];
......
function2(rr);
...... //r1 and r2 are also used in function1().
}
In the above code, I have two classes and I define constructor for the two classes and do not define destructor. In function1(), I declare two pointers of class2 and an array of pointer of class2. The array rr is used to bring back values from function2(). For r2, I do not use "delete". Will r1, r2 and the array rr[] cause memory leak?
If you don't delete them, yes. However, since the pointers are local
variables within your function and not member variables of your class,
you don't need a destructor for cleaning them up, but rather would
delete them in the function. Do you actually need them to be pointers
anyway? If a direct instance suffices, use it.
The two pointers, r1 and r2, and array rr[] are local variables, when the code exits function1(), these local variables should be released automatically.
The local variables are, but not the objects they point to.
Am I right?
What you wrote is right, but I don't think it's what you meant :-)
John wrote: 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 execution. But I do not think it needs so much memory. About 500M memory should be enough. I have following
Maybe you're creating objects inside a infinit while or for loop.
Anil Mamede
Where you use "new", you must use "delete".
Where you use "new", you must use "delete".
Where you use "new", you must use "delete".
class2* r2 = new class2;
delete r2;
I highly suggest that you keep the asterisk beside THE CLASS NAME rather
than beside the variable name.
Think of "delete" as a function that takes one paramater, a pointer. You
pass it the value of the variable "r2", which is of type "class2*".
delete r2;
---
As for normal run-of-the-mill variables, they come into being at the
beginning of the block of code and are destroyed at the end of the block of
code:
int main(void)
{ //Right here, a comes into being
class2 a;
{ //Right here, b comes into being
class2 b;
} // Right here, b is destroyed.
return 0;
} //Right here, a is destroyed
JKop wrote: Think of "delete" as a function that takes one paramater, a pointer. You pass it the value of the variable "r2", which is of type "class2*".
delete r2;
I'd rather say think of new as a source and delete as a sink. Everything
that came from 'new' has to go to 'delete'.
As for normal run-of-the-mill variables, they come into being at the beginning of the block
No, they start existing where they are defined.
of code and are destroyed at the end of the block of code:
int main(void) { //Right here, a comes into being
Nope. a doesn't exist yet.
class2 a;
Here, a commes into existance. { //Right here, b comes into being
No. Same as a class2 b; } // Right here, b is destroyed.
return 0;
} //Right here, a is destroyed
"John Harrison" <jo************ *@hotmail.com> wrote in message news:<2g******* *****@uni-berlin.de>... "Ian" <no***@nowhere. com> wrote in message news:10******** *******@drone5. qsi.net.nz... John wrote: 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 execution. But I do not think it needs so much memory. About 500M memory should be enough. I have following questions about memory leak. (1).If in my code I only define constructor for my class, and do not define destructor, will it cause memory leak?
See next answer! (2).If in my code I only use "new" to declare new object, and do not use "delete", will it cause memory leak? Yes, without a destructor, how is the memory you allocated in the destructor freed?
An alternative is to use std::auto_ptr rather than a plain pointer.
As a rule, any class with pointers must have a destructor and copy constructor (if nothing else, this makes you think about ownership of the data you reference through a pointer).
Ian
I think the OP is not talking about a class with pointers but a pointer to a class. Certainly that is what his code shows.
In any case the rule is every new must be matched with a delete. Constructors and destructors are just a useful way of making sure that this rule is followed.
Since the OP is clearly struggling with pointers I would advise avoid using new where possible. It's certainly a common newbie trait to use new where it isn't necessary.
// newbie style coding void func() { X* xp = new X; x->some_func(); delete x; }
// simpler, safer and better coding void func() { X x; x.some_func(); }
There's nothing in the code that John posted that indicated he must use new at all.
john
Hi John,
Thanks a lot.
My code is based on an old code. I modify it and add my own functions.
The old code uses linked list of objects everywhere. The linked list
is implemented by using pointer. This is the reason why I must use
pointer to a class.
If I do not use "new", will it cause memory leak?
For example,
void func()
{
int *p = 0; // Initialize p to be 0.
int a = 10;
X *x; //line 1
x->some_func();
p = & a;
}
Will the above code cause memory leak?
When line 1 is executed, the constructor of X will initialize x. By
the end of func(), x is released. Am I right?
Thanks again.
John This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: ranjeet.gupta |
last post by:
Dear All
Is the Root Cause of the Memory corruption is the Memory leak, ??
suppose If in the code there is Memory leak, Do this may lead to the
Memory Corruption while executing the program ?
In nut shell, what is/are the realtion/s between the Memory Leak and
Memory Corruption.
Juts Theoritical Assumtion below:
|
by: José Joye |
last post by:
Hi,
I have implemented a Service that is responsible for getting messages from a
MS MQ located on a remote machine. I'm getting memory leak from time to time
(???). In some situation, it is easier to reproduce (e.g.: remote machine
not available). After about 1 day, I get a usage of 300MB of memory.
I have used .NET Memory Profiler tool to try to see where the leak is
located. For all the leaky instances, I can see the following (I...
|
by: Don Nell |
last post by:
Hello
Why is there a memory leak when this code is executed.
for(;;)
{
ManagementScope scope = new ManagementScope();
scope.Options.Username="username";
scope.Options.Password="password";
scope.Path.Path=@"\\pc\root\cimv2";
|
by: jeevankodali |
last post by:
Hi
I have an .Net application which processes thousands of Xml nodes each
day and for each node I am using around 30-40 Regex matches to see if
they satisfy some conditions are not. These Regex matches are called
within a loop (like if or for). E.g.
for(int i = 0; i < 10; i++)
{
Regex r = new Regex();
|
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
| |
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
each object within my JS app to help locate my problem?
Thanks
|
by: Salvador |
last post by:
Hi,
I am using WMI to gather information about different computers (using win2K
and win 2K3), checking common classes and also WMI load balance. My
application runs every 1 minute and reports the status of the machines.
Upon we follow the .NET object lifetime recommendations the application is
constantly consuming more memory! The problem is on the
ManagementObjectSearch, upon we Dispose the object it seems that is not
releasing the...
|
by: Jim Land |
last post by:
Jack Slocum claims here
http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript-
memory-leaks/
that "almost every site you visit that uses JavaScript is leaking memory".
Anybody know anything about this?
Does *Javascript* leak memeory, or
does the *browser* leak memory?
|
by: Ragnar Agustsson |
last post by:
Hi all
I have been wandering about the best way to sandbox memory leaks in 3rd
party libraries when using them from the .Net framework.
I have a 3rd party library, written in C++, that leaks a lot of memory
but I still had to use it.
1.
After using DLLImport and seeing the memory leak I tried to load and
|
by: Peter |
last post by:
I am using VS2008.
I have a Windows Service application which creates Crystal Reports. This is
a multi theaded application which can run several reports at one time.
My problem - there is a memory leak someplace. I can not detect the memory
leak by running several reports by hand, but when I run tha app as a
servrice and process few hundred reports there is significant memory leak.
The application can consume over 1GB of memory where it...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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,...
|
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...
|
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...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |