Strategies For Tracking Down Memory Leaks When You've Done Everything Wrong

后端 未结 13 1756
醉话见心
醉话见心 2020-12-14 12:50

My program, alas, has a memory leak somewhere, but I\'ll be damned if I know what it is.

Its job is to read in a bunch of ~2MB files, do some parsing and string repl

13条回答
  •  感情败类
    2020-12-14 13:21

    Similar to Charlie Martin, you can do something like this:

    static unigned __int64 _foo_id = 0;
    foo::foo()
    {
        ++_foo_id;
        if (_foo_id == MAGIC_BAD_ALLOC_ID)
            DebugBreak();
        std::werr << L"foo::foo @ " << _foo_id << std::endl;
    }
    foo::~foo()
    {
        --_foo_id;
        std::werr << L"foo::~foo @ " << _foo_id << std::endl;
    }
    

    If you can recreate it, even once or twice with the same allocation id, this will let you look at what is happening right then and there (obviously TLS/threading has to be handled as well, if needed, but I left it out for clarity).

提交回复
热议问题