max0x7ba / atomic_queue

C++ lockless queue.
MIT License
1.47k stars 176 forks source link

Problem of using atomic_queue with debug-version of malloc and free. #24

Closed ghost closed 4 years ago

ghost commented 4 years ago

The problem occurs when I use the queue under DEBUG mode with memleak checking in MSVC. The excerpt of code like below:

#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#define new new(_NORMAL_BLOCK, THIS_FILE, __LINE__)
#endif

class TestAtomicQueue {
    atomic_queue::AtomicQueue<int, 1, -1> m_queue;
};

int main()
{
#ifdef _DEBUG
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
    auto pObject = new TestAtomicQueue;
    delete pObject; // <-- broken here
}
max0x7ba commented 4 years ago

gcc reports an error in your code:

error: ‘new’ of type ‘TestAtomicQueue’ with extended alignment 64 [-Werror=aligned-new=]
  auto pObject = new TestAtomicQueue;
                     ^~~~~~~~~~~~~~~
note: uses ‘void* operator new(std::size_t)’, which does not have an alignment parameter
note: use ‘-faligned-new’ to enable C++17 over-aligned new support

The error message also contains the solution for your problem.

When you post bug reports you should specify the error message verbatim. "broken here" is less than useful.