Boost logo

Boost Users :

Subject: Re: [Boost-users] managed_shared_memory memory leak(?)
From: Michael Herrmann (mherrmann.at_at_[hidden])
Date: 2012-04-28 08:16:31


Dear Ion,

thank you for your reply. I should have said, sorry, that I am using
Boost 1.49.0. I can see that such singletons would show up as memory
leaks. In your example, already including
boost/interprocess/managed_shared_memory.hpp seems to lead to the
creation of some singletons that are falsely reported as memory leaks.
However, there seems to be more than just these singletons, that is
additionally created when managed_shared_memory::construct is actually
used. Please consider the following example:

     // The following three directives are required for MSVC's automatic
memory
     // leak detection.
     #define _CRTDBG_MAP_ALLOC
     #include <stdlib.h>
     #include <crtdbg.h>

     #include <boost/interprocess/managed_shared_memory.hpp>

     #include <Windows.h> // Sleep
     #include <iostream> // std::cout, std::endl

     int main(int argc, char *argv[])
     {
         _CrtMemState beginMemoryState;
         // capture the memory state at the beginning
         _CrtMemCheckpoint(&beginMemoryState);
         {
             using namespace boost::interprocess;
             static const char* shMemIdentifier = "MySharedMemory";
             struct ShMemCleaner {
                 ShMemCleaner() {
shared_memory_object::remove(shMemIdentifier); }
                 ~ShMemCleaner()
{shared_memory_object::remove(shMemIdentifier); }
             } cleaner;

             // Create a new segment with given name and size
             managed_shared_memory segment(create_only, shMemIdentifier,
1024);

             int* intInShMem = segment.construct<int>("myInt")[1](2);
             segment.destroy<int>("myInt");
         }
         _CrtMemState endingMemoryState;
         // capture the memory state at the end
         _CrtMemCheckpoint(&endingMemoryState);

         _CrtMemState memoryStateDiff;
         // if there is a difference between the two then there is a
"memory leak"
         if (_CrtMemDifference(&memoryStateDiff, &beginMemoryState,
&endingMemoryState)) {
             std::cout << "Memory leak detected!" << std::endl;
             // Ensure the user has time to see the above message:
             Sleep(1000);
             _CrtMemDumpAllObjectsSince(&memoryStateDiff);
             _CrtMemDumpStatistics(&memoryStateDiff);
         }
         return 0;
     }

I am comparing memory state before and after using managed_shared_memory
which I believe rules out the remainders that your example highlights. I
assume that there are some other singletons that are only created on
demand. Is there a way for me to either explicitly initialize (so I can
include it in the beginMemoryState dump) or clean up these singletons?

Thank you again!
Michael

On 26/04/2012 18:51, Ion Gaztañaga wrote:
> El 25/04/2012 12:35, Michael Herrmann escribió:
>> Hello everyone,
>>
>> the following trivial example highlights a memory leak according to MSVC
>> 2010. Does anybody know why this is and how it can be avoided?
>
> Interprocess creates some singletons for expensive operations and you
> are testing for leaks before those singletons are destroyed. The
> following test, in visual 10:
>
> #define _CRTDBG_MAP_ALLOC
> #include <stdlib.h>
> #include <crtdbg.h>
>
> #include <boost/interprocess/managed_shared_memory.hpp>
>
> static struct leak_detector
> {
> ~leak_detector()
> { _CrtDumpMemoryLeaks(); }
> } leak_detector_object;
>
> int main(int argc, char *argv[])
> {
> _CrtDumpMemoryLeaks();
> return 0;
> }
>
> The CrtDumpMemoryLeaks() call in main will be executed first and will
> report leaks (singletons are not still destroyed). The second
> _CrtDumpMemoryLeaks() called from the static variable will be called
> after interprocess singletons are destroyed (it's compiler dependent,
> but in my configuration the static variable is destroyed after
> interprocess singletons are destroyed) and reports no leaks.
>
> Ion
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net