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 5848137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:49:00+00:00 2026-05-22T12:49:00+00:00

I am trying to replace the global operator new and delete. In Linux this

  • 0

I am trying to replace the global operator new and delete. In Linux this works fine, but in windows( MSVC 10 ), it sometimes allocates using the system version of operator new and then tries to deallocate it using my operator delete. Since i am storing some context information while allocation, my operator delete expects the same during deallocation. How would i make sure that the windows picks up my operator new at all times?

edit:
I have tried various stuffs. These are the declaration

//Global new and delete overload
void* operator new (std::size_t bytes) throw(...);
void operator delete(void* p) throw();

void* operator new( std::size_t size, const std::nothrow_t&) throw();
void operator delete( void* mem, const std::nothrow_t&) throw();

void* operator new[] ( std::size_t bytes) throw(...);
void operator delete[](void* p) throw();

void* operator new[]( std::size_t size, const std::nothrow_t&) throw();
void operator delete[](void* mem, const std::nothrow_t&) throw();

#ifdef WIN32
void *__CRTDECL operator new(std::size_t size) _THROW1(_STD bad_alloc);
#endif

These are the definitions

#ifdef WIN32
void *__CRTDECL operator new(std::size_t bytes) _THROW1(_STD bad_alloc)
{
    void* p = edb::g_getCurrentMemoryContext()->alloc(bytes);
    if (p==0) // did malloc succeed?
        throw std::bad_alloc(); // ANSI/ISO compliant behavior
    return p;
}

#endif

void operator delete(void* p) throw()
{
    edb::MemContext::free(p);
}

void* operator new( std::size_t bytes, const std::nothrow_t&) throw()
{
    return edb::g_getCurrentMemoryContext()->alloc(bytes);
}

void operator delete(void* p, const std::nothrow_t&) throw()
{
    edb::MemContext::free(p);
}

void* operator new[] ( std::size_t bytes) throw(...)
{
    void* p = edb::g_getCurrentMemoryContext()->alloc(bytes);
    if (p==0) // did malloc succeed?
        throw std::bad_alloc(); // ANSI/ISO compliant behavior
    return p;
}

void operator delete[](void* p) throw()
{
    edb::MemContext::free(p);
}

void* operator new[]( std::size_t bytes, const std::nothrow_t&) throw()
{
    return edb::g_getCurrentMemoryContext()->alloc(bytes);
}

void operator delete[](void* p, const std::nothrow_t&) throw()
{
    edb::MemContext::free(p);
}

And sometimes it picks up my definitions and sometimes it doesn’t.

Thanks,
Gokul.

  • 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-05-22T12:49:01+00:00Added an answer on May 22, 2026 at 12:49 pm

    Overridding the global new/delete in C++ is a tar baby. Yeah, it looks simple enough, but then you make one exception after another and you keep digging a deeper and deeper hole for yourself.

    The reason why you’re getting ‘their new’ and ‘your delete’ is that you’re probably using some DLL that’s already loaded and ‘linked’ to the existing new before yours is loaded. They allocate the object and then you’re expected to delete it. You could try statically linking your program to resolve that.

    Another way of handling this would be to refactor your code and override the new/delete as the base class and then make all of your classes inherit from that. Tedious for a large project with a lot of classes but it will disambiguate which new/delete gets called for whom.

    If you’re doing this as a presumably ‘cheap’ way of isolating a memory leak, consider something else. See if any of the vendors of instrumentation tools(purify for example) still have a ‘2-week’ trial or run your linux version through valgrind.

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

Sidebar

Related Questions

I'm trying to use this code to replace spaces with _, it works for
im trying to replace the default 'Required' message in this validator: $this->setValidator('email', new sfValidatorAnd(array(
I'm trying to replace each , in the current file by a new line:
I'm trying to run this script: import re, os def build_pool(cwd): global xtn_pool, file_pool
I am trying to bind <C-return> . I tried global-set-key but it didn't work.
I trying to replace string Resources.AppResource.Info; like this Switch(Info); Is this possible with regex?
When trying to replace the content of an XML file in C#.NET with a
I'm trying to replace the programs that run from my startup directory with a
I'm trying to replace every multiline import inside a Python source file.. So, the
I am trying to replace multiple rows in an Access database to follow a

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.