memory leaks

Started by
4 comments, last by rip-off 13 years, 3 months ago
How & why do they happen and how can programmers avoid them. I use c++ because I love it XD, how can I stop any memory leaks, as far as I knew the stack would be added to and as soon as a variable went out of scope it would be destroyed and the stack address for allocating new memory would be -= sizeof(type); hence emptying the stack.

The reason I ask is because I have been thinking alot about performance lately and how my windows 7 is about 3 times more responsive and less resource hungry than my vista box, how my chrome is 10x faster than my firefox and does not experience the same leaks.

Also I have noticed that contrary to popular belief, PR etc, most open-source software performs slower than non-oss, so for example ie8 is faster at loading webpages, flash etc than firefox 3.6x

The main exception seems to be Linux OS which is in some ways faster and other ways slower than Windows OS while the two have I would say after a standard install of a packages linux about 10% either way in functionality
Advertisement
Quote:as far as I knew the stack would be added to and as soon as a variable went out of scope it would be destroyed
That is the reason why memory leaks usually don't happen with stack-based allocations (although it is technically possible to leak stack memory, e.g. with alloca inside a loop).

Most memory leaks will come from heap allocations, either because of exceptions or simply because of programmer sloppiness. Wrapping heap allocations/deallocations into stack-allocated objects (in the simplest case, that can be std::auto_ptr) is a well-known remedy for that. Keyword: RAII.
Using the non-throwing swap idiom is another example remedy.
Dang, this topic is all over the place.

Why memory leaks occur. Here's the deal. When you use malloc() or new, you're asking the kernel for a chunk of memory. Your pointers on the stack are "dumb pointers". They do not know what to do when they go out of scope, so the memory is not freed. This is why you must call free() or delete respectively in order to let the kernel know you are done with that memory, so it can use it somewhere else. Languages with garbage collection, like Java, may use reference counting. This basically keeps references of how many instances of your pointer exist, and deletes the pointer when that number reaches 0. I personally use my own shared pointer class, but i know Boost provides one as well.

with your FOSS vs non-FOSS argument, it really depends on the application domain. Some projects are handled better than others, and thus outperform their proprietary counterparts. Some FOSS projects are just not as lucky. The problem is not whether or not it's FOSS, but whether or not the project is managed appropriately.

I hope this helps you out.
As mentioned above, Boost provides a Smart Pointer library. It is exceedingly useful if you intend on using a lot of dynamically created objects.

If you're really interested in memory management, read up on and learn to use smart pointers (the concept and a library that implements them) thoroughly before starting your project. I found Boost libraries after having already started a game and went back through to insert them. Was something of a nightmare.

I Create Games to Help Tell Stories


How & why do they happen
You allocate memory but never release the allocation.
and how can programmers avoid them. I use c++ because I love it XD, how can I stop any memory leaksEasy answer: Don't use new or malloc.
Harder answer: Make sure every new/malloc is paired up with a delete/free.
C++ answer: Use smart pointers / RAII
as far as I knew the stack would be added to and as soon as a variable went out of scope it would be destroyedLeaks tend to be heap allocations (new/malloc), not stack allocations.

windows 7 is about 3 times more responsive and less resource hungry than vista ... chrome is 10x faster than firefox ... most open-source software performs slower than non-oss ... ie8 is faster than firefox 3.6x ... Linux OS is in some ways faster and other ways slower than Windows OS ...I don't see the connection between your anecdotes and the question of memory leaks...
From a research view of memory leak, there are two types of memory leaks. One is that you assign a pointer to refer a chunk of memory and then you change the pointer's value so that specific chuck of memory is lost forever, the other one is that you require a chunk of memory and never free it until the end of running your program. There are many non-commercial tools (most for research purpose) that can dealt with these two situations.

For java, we have FindBug. As to C/C++, MemCheck is an popular tool. All these tools are free to use and are able to find your memory leaks in programs.


[size=2]The reason I ask is because I have been thinking alot about performance lately and how my windows 7 is about 3 times more responsive and less resource hungry than my vista box, how my chrome is 10x faster than my firefox and does not experience the same leaks.<br /> [size=2]It all depends. Is a cache a leak? Maybe it should be considered that way, if its hit rate is low and size is large, or maybe it can be tuned better. In most of the examples you gave, I suspect that improvements in the caching algorithms were big contributors to the performance differences between them.

This topic is closed to new replies.

Advertisement