How to check memory leak with code?

D.D.K-2637 966 Reputation points
2023-05-01T20:56:15.8633333+00:00

Hi,

From this post, I want to check for memory leaks. however it still shows the leak.

Am I missing something?

 _CrtMemState sOld;
    _CrtMemState sNew;
    _CrtMemState sDiff;
    _CrtMemCheckpoint(&sOld); //take a snapshot
    

    std::wstring f{L"abc"};

    _CrtMemCheckpoint(&sNew); //take a snapshot 
    if (_CrtMemDifference(&sDiff, &sOld, &sNew)) // if there is a difference
    {
        OutputDebugString(L"-----------_CrtMemDumpStatistics ---------");
        _CrtMemDumpStatistics(&sDiff);
        OutputDebugString(L"-----------_CrtMemDumpAllObjectsSince ---------");
        _CrtMemDumpAllObjectsSince(&sOld);
        OutputDebugString(L"-----------_CrtDumpMemoryLeaks ---------");
        _CrtDumpMemoryLeaks();
    }
Developer technologies C++
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 49,536 Reputation points
    2023-05-01T21:44:57.0666667+00:00

    Let the CRT debug heap detect memory leaks and report on them at program termination. Refer to Find memory leaks with the CRT library


1 additional answer

Sort by: Most helpful
  1. فاطمة شحات 6 Reputation points
    2023-05-01T23:53:23.0433333+00:00

    هناك أسلوب آخر لتحديد مكان تسرب الذاكرة يتضمن أخذ لقطات من حالة ذاكرة التطبيق في نقاط رئيسية. لأخذ لقطة من حالة الذاكرة في نقطة معينة في التطبيق الخاص بك ، قم بإنشاء بنية _CrtMemStateوتمريرها إلى _CrtMemCheckpointالوظيفة.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.