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

  • SEARCH
  • Home
  • 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 7983641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:01:15+00:00 2026-06-04T11:01:15+00:00

I’m currently working on my own memory leak tracking system. I’m using Microsoft Visual

  • 0

I’m currently working on my own memory leak tracking system.

I’m using Microsoft Visual C++ 2008 and I know they have a built in one, but I’ve been making one for myself, just for the fun of it.

However, when I override the new and new[] commands, I get a function redefinition error, no matter what I do.

I don’t know much about Microsoft Visual C++’s internal bindings on things, but I’ve heard it is something with the CRT already defining the exact same macro I am.

I’ve seen articles on here and other places directed at this same exact question, but
people can never seem to solve the problem or never give a definite answer on how they
solved it.

Here is all the code I have up till this point:

MLD.h : http://pastebin.com/SfHzNaeN
MLD.cpp : http://pastebin.com/aRhsTZpv

All of the code is loosely based off a old flipcode article (How To Detect Memory Leaks).
Sorry I cannot give you a direct link, because I don’t have 10 rep to post more than 2 hyperlinks.

Thanks for your time.

  • 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-04T11:01:16+00:00Added an answer on June 4, 2026 at 11:01 am

    The “function redefinition error” is probably because you’re using MFC.

    The runtime library should not be in the business of defining such allocation and deallocation functions.

    The current code’s

    struct MemLeakInfo
    {
            unsigned int addr;
            unsigned int line;
            unsigned int size;
            unsigned char file;
    };
    

    is ungood. An unsigned int is not guaranteed to be large enough to hold an address, even though it is in 32-bit Windows. Instead, use intptr_t.

    Also, the current code’s

    void* operator new(unsigned int Size, int Line, const char* File);
    

    is ungood. That should be …

    void* operator new( size_t Size, int Line, const char* File );
    

    And you need a corresponding operator delete, like …

    void* operator delete( void* p, int Line, const char* File );
    

    in order to deallocate memory from a failed constructor call. It’s only called in that very specific situation. But if you don’t have it, then you have a leak, as MFC once had for debug builds.


    EDIT: fixed versions of the code you now provided:

    file [minimal.h]:

    • _MINIMAL_H is invalid, because it starts with underscore followed by uppercase letter, which is reserved. Changed to MINIMAL_H.
    • to use size_t you need to include <stddef.h>.
    • _DEBUG is not a standard macro. it’s a microsoftism. the standard macro (look up documentation of assert) for this purpose is NDEBUG.

     

    #ifndef MINIMAL_H
    #define MINIMAL_H
    
    #include <stddef.h>     // std::size_t
    
    #ifndef NDEBUG
    
    void* operator new( size_t Size, int Line, const char* File );
    void* operator new[]( size_t Size, int Line, const char* File );
    
    void operator delete( void* ptr, int Line, const char* File );
    void operator delete[]( void* ptr, int Line, const char* File ); 
    
    #endif 
    
    #ifndef NDEBUG
    
    #define DEBUG_NEW new( __LINE__, __FILE__ )
    
    #else
    
    #define DEBUG_NEW new
    
    #endif 
    
    #endif //MINIMAL_H
    

    file [minimal.cpp]:

    • to use malloc you need to include stdlib.h.
    • when you #define new you’re wreaking havoc with the new keywords in the following code.
    • in C you should never cast the result of malloc, and in C++ you should only cast something when there is a need for it. there is no such need here. casts only mask bugs, and that’s not a good idea.
    • missing return in error case. for error you need to throw std::bad_alloc. that’s by the holy standard’s specification of these functions.

     

    #include "Minimal.h"
    
    //#define new DEBUG_NEW
    
    #ifndef NDEBUG
    #include <stdlib.h>     // malloc
    #include <exception>    // std::bad_alloc
    
    void* operator new( size_t const size, int, const char*)
    {
        void* const ptr = malloc( size );
    
        if( !ptr ) { throw std::bad_alloc(); }
        return ptr;
    };
    
    void* operator new[]( size_t const size, int, const char*)
    {
        void* const ptr = malloc( size );
    
        if( !ptr ) { throw std::bad_alloc(); }
        return ptr;
    }
    
    void operator delete(void* const ptr, int, const char*)
    {
        free( ptr ); 
    };
    
    void operator delete[]( void* const ptr, int, const char* )
    {
        free( ptr ); 
    }; 
    
    #endif
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the

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.