Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. overriding new/delete

overriding new/delete

Scheduled Pinned Locked Moved C / C++ / MFC
questiondebuggingperformancetutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Walter Gildersleeve
    wrote on last edited by
    #1

    I've posted this question on other sites/BBSs w/o success... So I'm trying to write custom new and delete functions, in order to find a bunch of memory leaks in a program I'm working on. (This is in MSVC 6.) My new function I've defined as follows: void *__cdecl operator new(size_t, debug_class); I invoke it with char* ptr = new (debug_class()) char[10]; my custom routine then storing appropriate info. This works fine. But for this to work correctly, I need to have a matching delete routine. I've tried something similar, defining it as so: void __cdecl operator delete(void*, debug_class); But how do I invoke it? I've tried several possibilities, to whit delete (debug_class()) ptr[]; delete (debug_class(), ptr); delete (ptr, debug_class()); etc., but mainly end up with compiler errors. The last example compiles fine, but calls the default delete operator nonetheless. Any ideas how I'd invoke it? TIA Walter Gildersleeve IVU-Umwelt GmbH Freiburg, Germany

    T 2 Replies Last reply
    0
    • W Walter Gildersleeve

      I've posted this question on other sites/BBSs w/o success... So I'm trying to write custom new and delete functions, in order to find a bunch of memory leaks in a program I'm working on. (This is in MSVC 6.) My new function I've defined as follows: void *__cdecl operator new(size_t, debug_class); I invoke it with char* ptr = new (debug_class()) char[10]; my custom routine then storing appropriate info. This works fine. But for this to work correctly, I need to have a matching delete routine. I've tried something similar, defining it as so: void __cdecl operator delete(void*, debug_class); But how do I invoke it? I've tried several possibilities, to whit delete (debug_class()) ptr[]; delete (debug_class(), ptr); delete (ptr, debug_class()); etc., but mainly end up with compiler errors. The last example compiles fine, but calls the default delete operator nonetheless. Any ideas how I'd invoke it? TIA Walter Gildersleeve IVU-Umwelt GmbH Freiburg, Germany

      T Offline
      T Offline
      Tim Deveaux
      wrote on last edited by
      #2

      In a his discussion of the placement new operations Stroustrup ($10.4.11) describes a deletion mechanism using a Destroy fn which: 1. Calls the destructor of the object explicitly. 2. Frees the memory associated with it (in this case calling free(), since the operator new called alloc). delete is not used. On another tack, note also that the array version of delete cannot be overridden by one at class scope: From "The operator delete Function" (MSDN): "There are global and class-scoped operator delete functions. Only one operator delete function can be defined for a given class; if defined, it hides the global operator delete function. The global operator delete function is always called for arrays of any type" You can, I think, overload a global operator delete[] if arrays are the problem, but this is not somewhere I have visited. The _crtBreakAlloc mechanism is a great way to track leaks, but it's ease of use is lessened if your allocations are not predictable. Sounds like a fun class - might be interesting to see more of the layout. Refs: [Stroustrup] The C++ Programming Language, 3rd Edition, 1977, Addison Wesley

      1 Reply Last reply
      0
      • W Walter Gildersleeve

        I've posted this question on other sites/BBSs w/o success... So I'm trying to write custom new and delete functions, in order to find a bunch of memory leaks in a program I'm working on. (This is in MSVC 6.) My new function I've defined as follows: void *__cdecl operator new(size_t, debug_class); I invoke it with char* ptr = new (debug_class()) char[10]; my custom routine then storing appropriate info. This works fine. But for this to work correctly, I need to have a matching delete routine. I've tried something similar, defining it as so: void __cdecl operator delete(void*, debug_class); But how do I invoke it? I've tried several possibilities, to whit delete (debug_class()) ptr[]; delete (debug_class(), ptr); delete (ptr, debug_class()); etc., but mainly end up with compiler errors. The last example compiles fine, but calls the default delete operator nonetheless. Any ideas how I'd invoke it? TIA Walter Gildersleeve IVU-Umwelt GmbH Freiburg, Germany

        T Offline
        T Offline
        Tim Deveaux
        wrote on last edited by
        #3

        Here's a generic test, for discussion purposes...

        #include #include struct test{
        int a;
        int b;
        int c;

        void \* operator new(size\_t);
        void operator delete(void\*);
        
        void \* operator new\[\] (size\_t); 
        void operator delete\[\] (void\*);
        

        };

        void * test::operator new(size_t sz)
        {
        return malloc(sz);
        }

        void __cdecl test::operator delete(void* t)
        {
        free(t);
        }

        void * test::operator new[](size_t sz)
        {
        return malloc(sz);
        }

        void test::operator delete[](void* t)
        {
        free(t);
        }

        int main(int argc, char* argv[])
        {
        printf("Hello World!\n");

        // calls t::new & t::delete
        test\* t = new test;
        delete t;
        
        // calls t::new\[\] & t::delete\[\]
        t = new test\[20\];
        delete \[\]t;
        
        // calls global new and delete
        int \* p = new int;
        delete p;
        
        return 0;
        

        }

        This works as 'expected' on both Borland CPP Builder 4 and MS VC6, but leaves me with another question - I didn't expect to be able to provide an operator delete[] at class scope! Interestingly, if I try to scope the new and delete of the test class (e.g. test::new), I get errors - Borland simply gives 'Expression syntax', whereas VC6 says 'operator xxx must be globally qualified'. What gives?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups