| Banananas (17) | |
|
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 should not go over 200 mb. I have few other applications that use Crystal Reports and those applications do not have memory leak, the big difference is those applications are single threaded. My question: are there any tools, books, tutorials that can help me to find the memory leak? | |
|
|
|
| Imadatobanisa (647) | |
|
There aren't any book, or tool which can help you find and solve your memory leak problem rapidly and directly. :) But the cause : Yes. Here are several causes : 1- You allocated dynamic memory (pointers) but you forgot to delete (free) them. 2- Your structures have a lot of members, probably this means not all variables are used completely, so also it may cause memory leak. Edit : Sorry, maybe my first answer is wrong :) | |
|
Last edited on
|
|
| modoran (1245) | |
|
Of course there are dedicated tools for that, like Visual Leak Detector and many others : http://vld.codeplex.com/ However, you cannot easily debug a windows service as it is, just debug your EXE as any normal console applicaton (it could be required to first change your application code to be able to do that, it depends how it is written) | |
|
Last edited on
|
|
| hot pie (9) | |
| I can recommend to read a book "Effective C++" and you can read in wikipedia or softpedia about different debuggers (VLD, Purifu, MemCheck, DrMemory, Deleaker). Also, read about object oriented programming. | |
|
|
|
| Banananas (17) | |
| I get it. Perhaps you can share books or links to a good library? | |
|
|
|
| hot pie (9) | |
| Just search in google)) | |
|
|
|
| Imadatobanisa (647) | |
|
You may use some block code to detect memory leak. (Where you doubt) Good luck. | |
|
|
|
| Banananas (17) | |
| What block? I've never heard about it. Maybe I do not quite understand you. | |
|
|
|
| Imadatobanisa (647) | |||||||||||
|
Ok, Please, check the Task Manager. "Block code" in my opinion is the code which is only used to debug (usually complex programs) that standard debug feature cannot. Some functions which can do this effectively (which I'm currently using in many big projects) : - Sleep(); - exit(); - ExitProcess(); - MessageBox(); - FatalAppExit(); - sprintf, printf
About your memory leak : You also can use this to watch your program progress (with your Task Manager) A very simple example :
Certainly It causes memory leak. Now, you put some block code, for example :
Now you open up the Task Manager and see the program memory-consuming grows up and up. Test it, and you will see. Another example :
Open the Task Manager, the open your program, wait for 5 seconds. Look at your program process and find information "Memory Usage". Watch it closely. Then you'll see memory leak. :) Then you basically detected the problem. Expand the code and continue putting the block code (where you doubt). Many fatal errors or (memory leak) errors can be avoided. :) | |||||||||||
|
Last edited on
|
|||||||||||
| NwN (770) | |
|
Just on a sidenote, you may want to have a quick look at this link: http://vld.codeplex.com/ All the best, NwN | |
|
|
|
| Imadatobanisa (647) | |||
That's good stuff. I'll try it out. :) | |||
|
|
|||
| Banananas (17) | |
| Thanks, I already read this article. I learned how to manage memory. Can I prevent leaks? How to protect code from the leak? Or is it impossible? | |
|
|
|
| Framework (3237) | |
|
It's possible to prevent memory leaks by simply being careful. The example code provided by Jackson Marie intentionally (I hope) shows lack of care and logic. In general, allocating different blocks of memory is a good way to leak memory unless you utilise the use of smart pointers. However, common solutions are memory pools or garbage collection. A memory pool is a large pre-allocated block of memory that is capable of storing any type of information. This pool of memory is normally managed by an interface which the program uses to store its data. Memory pools themselves do not leak if the pool is released. However, if an object's constructor is called, the corresponding destructor must be called. A garbage collection system is a system that automatically frees allocated memory which saves the programmer from developing time-consuming memory management systems. A leaking GBS almost never happens. My opinion on GBSs: They do save the programmer time, but they don't teach proper discipline of memory like C/C++ does. Personally, I avoid GB at all costs; it's bad for my mental well-being. Wazzak | |
|
Last edited on
|
|
| JLBorges (1754) | |||||
|
> Can I prevent leaks? How to protect code from the leak? Or is it impossible? It is very easy to prevent resource leaks - just use RAII consistently. Not this:
But this:
| |||||
|
|
|||||
| NwN (770) | |
|
Just out of interest: When you say RAII, at present it's mostly done so by using smart pointers and STL containers as you showed right? I mean as opposed to opening a resource (or initialising a pointer) in the constructor (and providing fail safety there) and cleaning it up in the destructor? All the best, NwN | |
|
|
|
| JLBorges (1754) | |||||
|
> Just out of interest: When you say RAII, at present it's mostly done so > by using smart pointers and STL containers as you showed right? Yes. The standard library also provides RAII mechanisms for other resources that it encapsulates; for example std::lock_guard and friends.> as opposed to opening a resource (or initialising a pointer) in the constructor (and providing fail safety there) > and cleaning it up in the destructor? In many programs, we have to deal with resources that are not provided by the standard library. For example, there may be a database library written in C that we want to use. And it has the usual suite of sandwich functions:
Now, if we use this as it is in several places, our C++ code would metamorphose into Java-like spaghetti. To avoid that, we wrap the management of the raw resource in a shim class:
Such a wrapper classes would have code that deals directly with raw resources; and they should be the only ones doing so. | |||||
|
|
|||||
| NwN (770) | |
|
All right, thanks very much for clearing that up :) All the best, NwN | |
|
|
|
| Banananas (17) | |
| Thank you Framework!!! At last I got the answer to my question! | |
|
|
|
| hot pie (9) | |
|
"Yes. The standard library also provides RAII mechanisms for other resources that it encapsulates; for example std::lock_guard and friends. " I was interested in it! Thanks for the clarification! | |
|
|
|
| Banananas (17) | |
|
Thank you all! Problem solved. Thread is closed | |
|
|
|