![]() |
|
#1
|
|||
|
|||
Strange C++ code memory leakage problemDear Friends,
I have a very strange (to me) memory leakage problem in my C++ source code. I have included CPP / C++ / C Code:
in the precompiled header file, and also included CPP / C++ / C Code:
in every .cpp files. In my main function, I added in the end CPP / C++ / C Code:
I did the above in order to let the debugger tells me where the memory leakage occurs. On top of that, I also intentionally included the following line before _CrtDumpMemoryLeaks() without de-allocating the memory, to make sure my added codes really shows me where the memory leakage occurs. CPP / C++ / C Code:
After all these, I get the following from the output window (.NET): Code:
I included the full output for the sake of completeness. My question is, as you can see, the intentionally allocated memory (int* array) is picked up by the debugger (the first line in the output), it shows me the file allocation and linenumber so that I can track back. However, the other memory leakages seem every strange. 1) it does not show me where the leakage occurs; 2) it shows a memory location but with 0 bytes long.. Do any of you have any suugestion as what may be the cause of these?? Your comments are highly appreciated. Thank you very much. David Last edited by LuciWiz : 02-Nov-2005 at 09:10.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#2
|
||||
|
||||
Re: Strange C++ code memory leakage problemHi David,
I dont know in depth about this but: I think you should delete the allocated array using the delete statement so that some of the memory leaks will be gone. You can find more information about those in this link: Memory Leaks Regarding the 0 byte memory leaks, I found some information and here it is: The C Runtime will allocate memory for the entry and in debug- version also for the "overwrite" marker at beginning and end of the allocation. The C runtime heap has to set aside workspace to hold that pointer, etc. Try allocating 1,000,000 0 byte blocks and see what happens - memory is definitely being consumed. Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#3
|
|||
|
|||
Re: Strange C++ code memory leakage problemDear Paramesh,
Many thanks for your comments I think it maybe useful to include the _CrtMemDumpStatistics() output as well, the two states shown here are snapshots taken at the very beginning and the very end in my main() function. Here goes: 0 bytes in 0 Free Blocks. 0 bytes in 0 Normal Blocks. 6293 bytes in 43 CRT Blocks. 0 bytes in 0 Ignore Blocks. 0 bytes in 0 Client Blocks. Largest number used: 7785 bytes. Total allocations: 9961 bytes. 0 bytes in 0 Free Blocks. 0 bytes in 32 Normal Blocks. 23605 bytes in 69 CRT Blocks. 0 bytes in 0 Ignore Blocks. 0 bytes in 0 Client Blocks. Largest number used: 66100082 bytes. Total allocations: 75856046 bytes. As can be seen, the second state has "0 bytes in 32 Normal Blocks.", and this is where I am so confused.. Thank you for any comment. David |
|
#4
|
|||
|
|||
Re: Strange C++ code memory leakage problemQuote:
This points to line number 75724. Is your file really that long? (Or have you included something else in your file that causes it to be that long?) It is indicating a block length of zero bytes. How can this happen? I have seen this in buggy code that had something like char *y = new char[0];. That is allocating an block of memory that has zero bytes in it (!) I don't think there is anything illegal about allocating a zero-length array, but you can't legally store anything there. But the point is, compilers cheerfully accept such code --- sometimes leading to (inconsistently) bad stuff at run time. Actually, the code looked something like this: CPP / C++ / C Code:
If n == 0 when the new operator is used like this, it creates an array of length zero. Along those same lines, maybe there is a class constructor with something like CPP / C++ / C Code:
Where the member y is a pointer to char. Now, if an object is instantiated with n == 0: CPP / C++ / C Code:
Maybe there are other ways of allocating memory blocks with length 0??? Check your code. Regards, Dave |
|
#5
|
|||
|
|||
Re: Strange C++ code memory leakage problemDear Dave,
Thanks a lot for your comments (as always). I tried your buggy code to allocate an array of 0 byte, and it showed me in the output window: Detected memory leaks! Dumping objects -> d:\c\test\mem_leak.cpp(51) : {45} normal block at 0x00322FB8, 0 bytes long. Data: <> Object dump complete. As you can see, the length is really 0 bytes long, however, I have been able to see where the problem lies, i.e. "d:\c\test\mem_leak.cpp(51)". However, as in my previous case, I had no idea where the problem came from. Last night, I managed to find where the problem is, by setting breakpoints on the memory numbers, for example, {75724}. And I can see that every time that function is called, a 0 byte long leakage occurs. But I still have no idea as why this is happening. Apparently, it has something to do with resizing the vector. Below is the buggy line in my source code: Code:
m_MotionInfoParam_SB is a vector of structures, and m_nNum_SBand_Pres is simply an int. Any comments? Many thanks! |
|
#6
|
|||
|
|||
Re: Strange C++ code memory leakage problemQuote:
This line is not buggy. I would guess that the function that it is calling is buggy. So: What does the Resize() member function look like in the class definition? Can you post the code for this function? Just a wild guess: sometimes functions with names like resize do some memory allocation. What do you think would happen if a resize function is called with 0 as an argument? (Looks like an ideal opportunity for a buggy function to create a memory leak, but how can anyone tell unless they see the code for the function?) Regards, Dave |
|
#7
|
|||
|
|||
Re: Strange C++ code memory leakage problemDear Dave,
Many thanks for your comments. I was afraid that the codes were too long, so I did not include those. But here they go: I have included all the functions, because they maybe helpful in reading the codes. CPP / C++ / C Code:
In my previous post, m_MotionInfoParam_SB is actually defined like this: CPP / C++ / C Code:
Where MOTION_INFO is a structure, defined as follows: CPP / C++ / C Code:
Types CVideoFrameFloatBuf* do inlcude a lot of memory allocations: CPP / C++ / C Code:
CPP / C++ / C Code:
Classs CBufBase2D is simply a 2D buffer, as follows. CPP / C++ / C Code:
Sorry for such a long post, I have tried to included the information that I think might be useful. Thank you very much for taking your time. Thanks a million! David Last edited by LuciWiz : 04-Nov-2005 at 06:48.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#8
|
|||
|
|||
Re: Strange C++ code memory leakage problemQuote:
It is useful to let me see what you are dealing with, although obviously I don't have enough information to debug the entire thing (And I probably wouldn't, anyhow.) In the first place, I don't think that being left with pointers that point to blocks of size zero necessarily represents a memory leak. (If the blocks themselves have been de-allocated, then that's OK.) They do present the possibility of corrupting memory if some other probram bug results in reusing that pointer for something after the memory is no longer valid. Ordinarily, we would set pointers to zero when the memory is deallocated. More importantly, since you are diligently searching for memory leaks, they confuse the statistics. Now: Since from your previous post, the debug routine showed problems associated with the Resize function, I would concentrate there. I have no way of knowing how your program actually uses these functions, but I would have to ask what would be the effect of calling Resize with an argument value of uNewsize = 0. I think it leaves a pointer that is pointing to memory that has been deallocated by the destroy() routine. The destroy() routine actually de-allocates the objects that the pointer was pointing to. But maybe (one might actually say "probably") I am wrong. It's up to you to see what the heck it's doing. Here's how I might approach the situation: Make a small program that puts a couple of objects in a list and see if the memory leak statistics show the puzzling output. Try resizing a few times, including resize zero. See what happens. Try to get the smallest possible main() program that exhibits the puzzling behavior. Put cout<< in the Resize function to see what values are being used. Trace the action by putting cout<< in destroy(), etc. Regards, Dave "We can face our problem. We can arrange such facts as we have with order and method." Hercule Poirot --- in Murder on the Orient Express |
Recent GIDBlog
Toyota - 2008 July Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problem with porting of Linux code to Windows, FORTRAN/C. | stormlab | Computer Software Forum - Linux | 0 | 14-Oct-2005 12:25 |
| Pointer Usage in C++: Beginner to Advanced | varunhome | CPP / C++ Forum | 0 | 19-Aug-2005 09:25 |
| Guidelines for posting requests for help - UPDATED! | WaltP | CPP / C++ Forum | 0 | 21-Apr-2005 02:44 |
| [Tutorial] Pointers in C (Part I) | Stack Overflow | C Programming Language | 1 | 08-Apr-2005 18:35 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The