How do I test for/track memory leaks in a CLI/CPP library?

Hi:

I've got a CLI/CPP wrapper library which wraps a native C++ library.
That wrapper is then available to be consumed by .Net (managed) clients.

I'm about to start testing for memory leaks for objects created in the CLI/C++ layer.

What is the best way to test for memory leaks?

Will something like Video Leak Detector catch leaked memory allocated in the CLI/C++ layer?

Is there another tool I should be using or another way to tackle this?

Thanks,
JohnB
LVL 1
jxbmaSoftware ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
You can do that with the CRT's Debug APIs, e.g. like

int tmpFlag;

// Get the current state of the flag
// and store it in a temporary variable
tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );

// Turn On (OR) - Keep freed memory blocks in the
// heap’s linked list and mark them as freed
tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
tmpFlag |= _CRTDBG_CHECK_ALWAYS_DF;
tmpFlag |= _CRTDBG_ALLOC_MEM_DF;
tmpFlag |= _CRTDBG_DELAY_FREE_MEM_DF;

// Set the new state for the flag
_CrtSetDbgFlag( tmpFlag );

test(); // code that interacts with .NET

_CrtDumpMemoryLeaks ();

Open in new window


Or, since your code resides in a DLL:

BOOL WINAPI DllMain(
  _In_  HINSTANCE hinstDLL,
  _In_  DWORD fdwReason,
  _In_  LPVOID lpvReserved
  ) {

    int tmpFlag;

    switch (fdwReason) {

        case DLL_PROCESS_ATTACH: // DLL is loaded

            // Get the current state of the flag
            // and store it in a temporary variable
            tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );

            // Turn On (OR) - Keep freed memory blocks in the
            // heap’s linked list and mark them as freed
            tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
            tmpFlag |= _CRTDBG_CHECK_ALWAYS_DF;
            tmpFlag |= _CRTDBG_ALLOC_MEM_DF;
            tmpFlag |= _CRTDBG_DELAY_FREE_MEM_DF;

            // Set the new state for the flag
            _CrtSetDbgFlag( tmpFlag );

            break;

        case DLL_PROCESS_DEACH: // DLL is unloaded

            _CrtDumpMemoryLeaks ();

            break;
    }

    return TRUE;
}

Open in new window


The information provided by '_CrtDumpMemoryLeaks()' will even include the line numbers where the allocations where made. See also https://msdn.microsoft.com/en-us/library/x98tx3cf.aspx ("Finding Memory Leaks Using the CRT Library")
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jxbmaSoftware ConsultantAuthor Commented:
Spot on again man!
Thanks so much,
JB
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.