Boost logo

Boost Users :

From: Georgios Diamantopoulos (georgiosd_at_[hidden])
Date: 2006-01-11 05:09:31


Hello everyone,

I am experiencing a memory leak while using pool_allocator with
std::vector. I am detecting this with _CrtDumpMemoryLeaks() on MSVC8.
The full program listing is at the end of the program.

The leaks reported are:

Detected memory leaks!
Dumping objects ->
{122} normal block at 0x00356518, 520 bytes long.
Data: < e5 e5 $e5 (e5 > 1C 65 35 00 20 65 35 00 24 65 35 00 28 65 35 00
{121} normal block at 0x003563D0, 264 bytes long.
Data: < c5 c5 c5 c5 > D4 63 35 00 D8 63 35 00 DC 63 35 00 E0 63 35 00
{120} normal block at 0x00356308, 136 bytes long.
Data: < c5 c5 c5 c5 > 0C 63 35 00 10 63 35 00 14 63 35 00 18 63 35 00
Object dump complete.

This could be due to a static object being destroyed after main()
returns but I couldn't know that without knowing the implementation of
pool_allocator. I have tried getting mem checkpoints at the beginning
and end of main and detecting the leaks that way which I *think* would
get around static objects but without any difference.

Thanks,

Georgios

#define CRTDBG_MAP_ALLOC
#include <cstdlib>
#include <crtdbg.h>

#include <stdexcept>

#if defined(_DEBUG)
inline void* __cdecl operator new(size_t nSize)
{
    void* pResult = _malloc_dbg(nSize, _NORMAL_BLOCK, NULL, 0);
    if (pResult != NULL)
        return pResult;
    else
        throw std::bad_alloc();
    return pResult;
}

inline void __cdecl operator delete(void* p)
{
    _free_dbg(p, _NORMAL_BLOCK);
}

inline void* __cdecl operator new[](size_t nSize)
{
    return ::operator new(nSize);
}

inline void __cdecl operator delete[](void* p)
{
    ::operator delete(p);
}

inline void* __cdecl operator new(size_t nSize, int nType, const char *
lpszFileName, int nLine)
{
    void* pResult = _malloc_dbg(nSize, nType, lpszFileName, nLine);
    if (pResult != NULL)
        return pResult;
    else
        throw std::bad_alloc();

    return pResult;
}

inline void* __cdecl operator new[](size_t nSize, int nType, const char
* lpszFileName, int nLine)
{
    return ::operator new(nSize, nType, lpszFileName, nLine);
}

inline void* __cdecl operator new(size_t nSize, const char *
lpszFileName, int nLine)
{
    return ::operator new(nSize, _NORMAL_BLOCK, lpszFileName, nLine);
}

inline void* __cdecl operator new[](size_t nSize, const char *
lpszFileName, int nLine)
{
    return ::operator new[](nSize, _NORMAL_BLOCK, lpszFileName, nLine);
}

inline void __cdecl operator delete(void* pData, const char * /*
lpszFileName */, int /* nLine */)
{
    ::operator delete(pData);
}

inline void __cdecl operator delete[](void* pData, const char * /*
lpszFileName */, int /* nLine */)
{
    ::operator delete(pData);
}

#define DEBUG_NEW new(THIS_FILE, __LINE__)

#endif // _DEBUG

#include <vector>
#include <boost/pool/pool_alloc.hpp>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

int main(void)
{
    std::vector<int, boost::pool_allocator<int> > data_points;
    for(int i = 0; i < 100; ++i) {
        data_points.push_back(i);
    }

    data_points.~vector();

    boost::singleton_pool<boost::pool_allocator_tag,
sizeof(int)>::release_memory();

    _CrtDumpMemoryLeaks();

    return 0;
}


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