473,416 Members | 1,530 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,416 software developers and data experts.

What is a memory leak

I'm reading about exception safety and wondering when I need to bother,
and when a program can crash without hogging some resource. If I am only
talking about memory allocated during a program, then I can see that

(dodgy top-of-head code follows)

int main{
while (true){
MemoryHoggingObject * myObjectPtr = new MemoryHoggingObject;
}

return 0;
}
.........

will fill up memory until something breaks and it crashes, lets say on
the 1000th iteration

but what about:

int main(){

MemoryHoggingObject * myObjectPtr = new MemoryHoggingObject;
exit(1);

}

Does it follow that I can only run this program 1000 times? Or are all
the resources released once it exits? If the exit is replaced by some
exception-generating code, is there any difference?

cheers

shaun
Jul 17 '06 #1
12 1752
shaun roe wrote:
I'm reading about exception safety and wondering when I need to
bother, and when a program can crash without hogging some resource.
Is this a well-masqueraded homework? Looks like it...
If I am only talking about memory allocated during a program, then I
can see that

(dodgy top-of-head code follows)

int main{
while (true){
MemoryHoggingObject * myObjectPtr = new MemoryHoggingObject;
}

return 0;
}
........

will fill up memory until something breaks and it crashes, lets say on
the 1000th iteration

but what about:

int main(){

MemoryHoggingObject * myObjectPtr = new MemoryHoggingObject;
exit(1);

}

Does it follow that I can only run this program 1000 times?
Usually not.
Or are all
the resources released once it exits? If the exit is replaced by some
exception-generating code, is there any difference?
It highly depends on the execution environment. Some free program
memory once program finishes and return it to the available memory,
some don't.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 17 '06 #2

shaun roe wrote:
I'm reading about exception safety and wondering when I need to bother,
and when a program can crash without hogging some resource. If I am only
talking about memory allocated during a program, then I can see that

(dodgy top-of-head code follows)

int main{
while (true){
MemoryHoggingObject * myObjectPtr = new MemoryHoggingObject;
}
Just a little bit of dodgy declaration of main :)
>
return 0;
}
........
The default behaviour of new is to throw a badalloc when memory
allocation fails. Therefore, return 0 will never happen, and your
program will exit with an unhandled exception. When it does that I
think the deconstructors for objects are called as part of the exit
handler (I could well be wrong). However, most likely your OS will
release resources acquired otherwise.
>
will fill up memory until something breaks and it crashes, lets say on
the 1000th iteration

but what about:

int main(){

MemoryHoggingObject * myObjectPtr = new MemoryHoggingObject;
exit(1);

}

Does it follow that I can only run this program 1000 times? Or are all
the resources released once it exits? If the exit is replaced by some
exception-generating code, is there any difference?
Generally, you want to be in a position where your program releases
resources as it no longer requires them. However, on a return from
main, or exit() or unhandled exception, they are generally released by
the OS - if the program doesn't do that 'on it's way out...'

Jon.

Jul 17 '06 #3
shaun roe wrote:
I'm reading about exception safety and wondering when I need to bother,
and when a program can crash without hogging some resource. If I am only
talking about memory allocated during a program, then I can see that

(dodgy top-of-head code follows)

int main{
while (true){
MemoryHoggingObject * myObjectPtr = new MemoryHoggingObject;
}

return 0;
}
........

will fill up memory until something breaks and it crashes, lets say on
the 1000th iteration

but what about:

int main(){

MemoryHoggingObject * myObjectPtr = new MemoryHoggingObject;
exit(1);

}

Does it follow that I can only run this program 1000 times?
No.
Or are all the resources released once it exits?
The C++ standard says nothing about obtaining nor returning memory to the
OS. It refrains (wisely?) from making such requirements about the support
offered by the computation environment. However, I do not know of any OS
that will not reclaim resources upon termination of a program.
If the exit is replaced by some exception-generating code, is there any
difference?
Not in terms of the standard. Even if you call delete() for all the pointers
you new()-ed in your program, there is no guarantee that memory will be
returned to the OS.
Best

Kai-Uwe Bux
Jul 17 '06 #4


Which of these lines gives a _memory leak_ ;)

char* p1, *p2, *p3;
p1 = new char[5];
p2 = (char*) &p2;
p3 = (char*) "Hello";
delete p1[];
delete p2[];
delete p3[];
Jul 18 '06 #5
Gernot Frisch wrote:
Which of these lines gives a _memory leak_ ;)

char* p1, *p2, *p3;
p1 = new char[5];
p2 = (char*) &p2;
p3 = (char*) "Hello";
delete p1[];
delete p2[];
delete p3[];
None. The program is ill-formed.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 18 '06 #6
Gernot Frisch wrote:
Which of these lines gives a _memory leak_ ;)

char* p1, *p2, *p3;
p1 = new char[5];
p2 = (char*) &p2;
p3 = (char*) "Hello";
delete p1[];
ill-formed. but delete[] p1 would be fine.
delete p2[];
ill-formed, delete [] p2; would be illegal because the value
in p2 was not allocated by new.
delete p3[];
ill-formed. delete [] p3 would be illegal because the value
was not allocated by new.

None of these would cause a memory leak.

Jul 18 '06 #7
Ron Natalie wrote:
Gernot Frisch wrote:
>Which of these lines gives a _memory leak_ ;)

char* p1, *p2, *p3;
p1 = new char[5];
p2 = (char*) &p2;
p3 = (char*) "Hello";
delete p1[];
ill-formed. but delete[] p1 would be fine.
> delete p2[];
ill-formed, delete [] p2; would be illegal because the value
in p2 was not allocated by new.
It's also illegal because the address of 'p2' stored in 'p2'
though a C-style cast (equivalent to 'reinterpret_cast' here)
is used in some way other than to convert back.
>
> delete p3[];

ill-formed. delete [] p3 would be illegal because the value
was not allocated by new.

None of these would cause a memory leak.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 18 '06 #8
Victor Bazarov wrote:
Ron Natalie wrote:
>Gernot Frisch wrote:
>>Which of these lines gives a _memory leak_ ;)

char* p1, *p2, *p3;
p1 = new char[5];
p2 = (char*) &p2;
p3 = (char*) "Hello";
delete p1[];
ill-formed. but delete[] p1 would be fine.
>> delete p2[];
ill-formed, delete [] p2; would be illegal because the value
in p2 was not allocated by new.

It's also illegal because the address of 'p2' stored in 'p2'
though a C-style cast (equivalent to 'reinterpret_cast' here)
is used in some way other than to convert back.
Yep, I caught that too, too many things wrong.
Note that the following is a better demonstration of
that concept:

p2 = (char*) new char*[5];
delete [] p2;
Jul 18 '06 #9
Kai-Uwe Bux wrote:
>
The C++ standard says nothing about obtaining nor returning memory to the
OS. It refrains (wisely?) from making such requirements about the support
offered by the computation environment. However, I do not know of any OS
that will not reclaim resources upon termination of a program.
I believe Windows 3.* failed to reclaim memory.
At least that is what I was told by people who
wrote programs for it.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 19 '06 #10
Kevin Handy wrote:
Kai-Uwe Bux wrote:
>>
The C++ standard says nothing about obtaining nor returning memory
to the OS. It refrains (wisely?) from making such requirements about
the support offered by the computation environment. However, I do
not know of any OS that will not reclaim resources upon termination
of a program.

I believe Windows 3.* failed to reclaim memory.
At least that is what I was told by people who
wrote programs for it.
I am not sure Windows 3.* qualify as real OSes... Do they? OT, of course.
Jul 19 '06 #11
Victor Bazarov wrote:
Kevin Handy wrote:
>Kai-Uwe Bux wrote:
>>>
The C++ standard says nothing about obtaining nor returning memory
to the OS. It refrains (wisely?) from making such requirements about
the support offered by the computation environment. However, I do
not know of any OS that will not reclaim resources upon termination
of a program.

I believe Windows 3.* failed to reclaim memory.
At least that is what I was told by people who
wrote programs for it.
I was a happy user of MS-DOS until Microsoft discontinued support. I never
had any trouble with DOS not reclaiming the little memory there was: and
not all my programs were clean (however, maybe by Modula-2 compiler
inserted some clean-up code behind my back).

I cannot say anything about Windows, though: I never used it.

I am not sure Windows 3.* qualify as real OSes... Do they? OT, of
course.
Really? If I recall correctly, the OS was MS-DOS and Windows 3.* was just a
DOS program.
Best

Kai-Uwe Bux
Jul 19 '06 #12

"Kai-Uwe Bux" <jk********@gmx.netskrev i meddelandet
news:e9**********@murdoch.acc.Virginia.EDU...
Victor Bazarov wrote:
>Kevin Handy wrote:
>>Kai-Uwe Bux wrote:
The C++ standard says nothing about obtaining nor returning
memory
to the OS. It refrains (wisely?) from making such requirements
about
the support offered by the computation environment. However, I do
not know of any OS that will not reclaim resources upon
termination
of a program.

I believe Windows 3.* failed to reclaim memory.
At least that is what I was told by people who
wrote programs for it.

I was a happy user of MS-DOS until Microsoft discontinued support. I
never
had any trouble with DOS not reclaiming the little memory there was:
and
not all my programs were clean
That's because the DOS "memory management" worked backwards. :-)

The currently running program had access to all free memory by
default. (All memory actually, as it was unprotected).

To keep some part after the program terminated, it had to explicitly
register an address range as reserved. Otherwise the next program
would get exactly the same free memory. DOS didn't have to do
anything.
>(however, maybe by Modula-2 compiler
inserted some clean-up code behind my back).
Only by calling terminate instead of terminate-and-stay-resident.
Bo Persson
Jul 20 '06 #13

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

Similar topics

10
by: Debian User | last post by:
Hi, I'm trying to discover a memory leak on a program of mine. I've taken several approaches, but the leak still resists to appear. First of all, I've tried to use the garbage collector to...
14
by: J. Campbell | last post by:
what happens to allocated memory when a program terminates before the memory is released. For example: int main(){ int* array; int a_size = 1000; array = new int; for(int i = 0; i < a_size;...
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...
4
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";...
9
by: Anton | last post by:
{Willy Skjveland} Hi, how can I trace a Memory leak in aspnet_wp.exe? {Rheena} One moment please while I search it for you. It may take me a few moments {Willy Skjveland} I need to find out...
13
by: Boni | last post by:
I use 3-d party component. In this component I must pass a reference to my object. The problem is that this component has an ugly bug.When this component is disposed, it incorrectly don't delete...
3
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". ...
0
by: nejucomo | last post by:
Hi folks, Quick Synopsis: A test script demonstrates a memory leak when I use pythonic extensions of my builtin types, but if I use the builtin types themselves there is no memory leak. ...
17
by: Mike | last post by:
Hello, I have following existing code. And there is memory leak. Anyone know how to get ride of it? function foo has been used in thousands places, the signature is not allowed to change. ...
22
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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...
0
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...
0
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...

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.