|  | ofstream, locales and freestore |  | |
| | | Irek |  |
| Posted: Thu Sep 04, 2008 10:58 pm Post subject: ofstream, locales and freestore |  |
Hi,
I have unit tests which I test for memory leaks. At the beginning and at the end of a unit test I'm checking for the memory allocated by the freestore. If the numbers of bytes allocated differ, then a memory leak is reported. However, an implementation of a standard library can allocate or release memory during a test, which will trigger a false alarm. A sample piece of code is this:
{ std::ofstream("test"); }
I debugged that piece of code and found out that the code that deals with locales allocates memory, which is released at the program exit. Can I do something so that I don't get these false reports on memory leaks? Like reinitializing the part of the standard library that deals with locales?
Thanks & best, Irek LINK
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Maciej Sobczak |  |
| Posted: Fri Sep 05, 2008 5:24 pm Post subject: Re: ofstream, locales and freestore |  |
| |  | |
On 5 Wrz, 00:58, Irek <irek.szczesn...@gmail.com> wrote:
| Quote: | I have unit tests which I test for memory leaks. At the beginning and at the end of a unit test I'm checking for the memory allocated by the freestore.
|
I think that your problem results exactly from this narrow scope of the check. By expecting balanced allocations at that level, you basically forbid all lazy deallocation schemes that might be implemented by lots of code which might not be even explicit. Even if you find some way to work around the IOStream memory management issues, there might be other supporting code with the same behavior, so the fundamental problem will still be there.
Can you restructure your unit tests so that they become separate and self-contained programs? This way, you can avoid lazy deallocation schemes, since there will be no opportunity to be laze (the end of unit is the end of program).
Consider using valgrind or something similar to check for leaks at this level.
-- Maciej Sobczak * LINK * LINK
[ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Ulrich Eckhardt |  |
| Posted: Mon Sep 08, 2008 7:58 pm Post subject: Re: ofstream, locales and freestore |  |
Irek wrote:
| Quote: | I have unit tests which I test for memory leaks.[...]A sample piece of code is this:
{ std::ofstream("test"); }
I debugged that piece of code and found out that the code that deals with locales allocates memory, which is released at the program exit. Can I do something so that I don't get these false reports on memory leaks?
|
As Maciej already pointed out, this code might do lazy allocations of the locales, which are shared between different streams. You can distinguish that from a memory leak by simply running the code twice and only using the difference between before and after the second run.
Uli
-- Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
[ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Irek |  |
| Posted: Mon Sep 08, 2008 8:05 pm Post subject: Re: ofstream, locales and freestore |  |
| |  | |
Thanks for your input, Maciej.
On 5 Wrz, 19:24, Maciej Sobczak <see.my.homep...@gmail.com> wrote:
| Quote: | Can you restructure your unit tests so that they become separate and self-contained programs? This way, you can avoid lazy deallocation schemes, since there will be no opportunity to be laze (the end of unit is the end of program).
|
The problem is that we have thousands of unit tests. Running each test as a separate process would degrade performance. And hundreds of these unit tests are frequently run, and so they need to finish quick.
Another problem is that I don't know how I could measure the memory allocated on the heap before the C++ standard library gets initialized.
| Quote: | Consider using valgrind or something similar to check for leaks at this level.
|
Yes, I'm using Valgrind to debug leaks. Our memory checker offers early and fast detection as opposed to Valgrind.
The problem of ofstream objects was the most outstanding, and I managed to work around it by first creating a number of ofstream objects and then closing them before running the unit tests. The memory checker now works fine, but surely it's vulnerable to similar problems in the future.
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Irek |  |
| Posted: Tue Sep 09, 2008 10:04 pm Post subject: Re: ofstream, locales and freestore |  |
Thanks for your email, Uli.
On September 8, 21:58, Ulrich Eckhardt <eckha...@satorlaser.com> wrote:
| Quote: | As Maciej already pointed out, this code might do lazy allocations of the locales, which are shared between different streams. You can distinguish that from a memory leak by simply running the code twice and only using the difference between before and after the second run.
|
I'm using something similar. Before running unit tests I have a piece of code that makes the standard library allocate global data, that would be otherwise allocated by unit tests.
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
|
|