Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9202907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:21:34+00:00 2026-06-17T23:21:34+00:00

I’m using _CrtDumpMemoryLeaks(); from the stdlib.h and crtdbg.h to detect memory leaks, but I

  • 0

I’m using _CrtDumpMemoryLeaks(); from the stdlib.h and crtdbg.h to detect memory leaks, but I noticed something strange in my code.

If I do:

int _tmain(int argc, _TCHAR* argv[])
{
    MyClass* myClass = new MyClass();
    _CrtDumpMemoryLeaks(); //I get a memory leak warning
}

However, if I do:

class MyClass
{
    public:
        char* NewChar();
};
char* MyClass::NewChar()
{
    char* test = new char[100];
    return test;
}

MyClass myClass; //Globally declared
int _tmain(int argc, _TCHAR* argv[])
{        
    char* charPointer = myClass.NewChar();
    _CrtDumpMemoryLeaks(); //No warnings
}

Shouldn’t I get the warning since my program exited (right after _CrtDumpMemoryLeaks()) and there was still a new char that wasn’t deleted?

How can I detect these memory leaks?

Also, using the same example above, if I add the code:

char* anotherPointer = charPointer; //previously filled
delete[] anotherPointer;

Will this delete the new char from inside the class preventing memory leaks, or should I call delete on charPointer as well?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:21:35+00:00Added an answer on June 17, 2026 at 11:21 pm

    If you want to catch (or have a decent shot at catching) global object leaks, try setting the CRT debug flag _CRTDBG_LEAK_CHECK_DF at the start of main. The flag forces a dump of detected leaks after global destructors.

    To answer your questions:

    Shouldn’t I get the warning since my program exited (right after _CrtDumpMemoryLeaks()) and there was still a new char that wasn’t deleted?

    Define “warning”. The call to _CrtDumpMemoryLeaks() should dump whatever is outstanding at the time. Your program exiting afterward will not do another dump unless configured to do so.

    How can I detect these memory leaks?

    They should be detected properly provided you are using the debug CRT and have the _Crt configuration setup properly, which it mostly is by default.


    The following code sets up the _Crt dump system to dump all objects on-demand and right before final exit (after main() is finished and global statics are destroyed).

    class MyLeak
    {
    public:
        MyLeak() { new unsigned char[1024]; }
    
        char * NewChar() { return new char[1024]; }
    };
    
    MyLeak myLeak;
    int main(int argc, char *argv[])
    {
        int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
        tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
        _CrtSetDbgFlag(tmpFlag);
    
        _CrtMemState ms = {0};
        _CrtMemCheckpoint(&ms);
    
        char *ptr = myLeak.NewChar();
        _CrtMemDumpAllObjectsSince(&ms);
    
        OutputDebugString("Exiting main()\n");
        return EXIT_SUCCESS;
    }
    

    Debug Output Log

    Dumping objects ->
    {69} normal block at 0x000000000048B800, 1024 bytes long.
     Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD 
    Object dump complete.
    Exiting main()
    Detected memory leaks!
    Dumping objects ->
    {69} normal block at 0x000000000048B800, 1024 bytes long.
     Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD 
    {68} normal block at 0x000000000048B390, 1024 bytes long.
     Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD 
    Object dump complete.
    

    Note that the first dump records just the inner allocation, since I used a checkpoint then a dump-since call. The second dump (invoked after main() is finished) still records them both, as they’re both outstanding.

    That you’re leak dump after your dynamic allocation (which granted was from an object in global memory space, but that has nothing to do with the allocation, its just code allocating memory and returning it to you) does not look correct. You should be getting an object dump of all outstanding CRT allocations with any _CrtDumpMemoryLeaks() invoke since the start of the program.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am using jsonparser to parse data and images obtained from json response. When
I am using JSon response to parse title,date content and thumbnail images and place
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.