pebal / sgcl

Smart Garbage Collection Library for C++
zlib License
163 stars 8 forks source link

Memory leaks? #7

Open cbing66 opened 3 months ago

cbing66 commented 3 months ago

I wrote a small test to start using the library. To test for memory leaks, I added a helper class that uses the functions in VS2022. From the output view it looks like there are blocks of memory not deallocated:

Dumping objects ->
{176} normal block at 0x0000023A799A47F0, 16 bytes long.
 Data: <@  y:           > 40 04 9A 79 3A 02 00 00 00 00 00 00 00 00 00 00 
{175} normal block at 0x0000023A799A0440, 32 bytes long.
 Data: < G y:           > F0 47 9A 79 3A 02 00 00 00 00 00 00 00 00 00 00 
{171} normal block at 0x0000023A7999AF60, 80 bytes long.
 Data: <                > C0 00 A0 01 F6 7F 00 00 00 00 00 00 00 00 00 00 
{170} normal block at 0x0000023A799B8610, 1496 bytes long.
 Data: <`  y:      y:   > 60 AF 99 79 3A 02 00 00 F0 8F 9A 79 3A 02 00 00 
{169} normal block at 0x0000023A799A85B0, 65560 bytes long.

I did some step-by-step checking and in some cases the deallocation functions are not called (e.g.{169} -> block->page_count == Block::PageCount is not checked and consequently the deletion to the next line is skipped). Am I doing something wrong? Is this a VisualStudio-specific problem?

My code:

#include "sgcl\sgcl.h"
#include <iostream>

#ifdef _DEBUG
struct TestMemory {
  _CrtMemState m_checkPt;

  TestMemory(int id)
  {
    _CrtMemCheckpoint(&m_checkPt);

    if (id>0) {
      _CrtSetBreakAlloc(id);
    }
  }

  ~TestMemory()
  {
    _CrtMemDumpAllObjectsSince(&m_checkPt);
  }
};

TestMemory testMemory(0);
#endif // DEBUG

using namespace sgcl;

int main()
{
  auto unique = make_tracked<int>(42);
}
cbing66 commented 3 months ago

Post scriptum: In unique_ptr.h, Line 36, I fixed it this way, to avoid a compile error (VS2022):

assert(this->get() != nullptr);

pebal commented 3 months ago

The memory block was not freed because allocator of the main thread is buffering memory of the one page. This allocator is destroyed after the collector thread ends. I will fix this.