Results 1 to 3 of 3

Thread: Overriding global new

  1. #1

    Default Overriding global new

    Hello all,
    I would be most grateful for your help. What I want to do in my application is to override new and delete not just for one class, but for the entire application. C++ allows you to do that by defining global versions of these operators. Here’s how I override global new and delete:
    void* operator new (size_t size)
    {
    void *p = malloc(size);
    return p;
    }

    void operator delete (void *p)
    {
    free(p);
    }
    The behavior I am facing after I have overridden global new and delete in this way is that at the beginning of the application (initialization that takes place before the event loop has been started) the overridden new is being invoked on some objects (not all), but after the event loop has started there is no invocation of the overridden new operator although there are objects that are dynamically created.
    I would appreciate any suggestions on this issue.
    Regards,

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Overriding global new

    Qt does it's own internal memory management, making use of smart pointers, copy-on-change and many other customized memory allocations/deallocations. You can dig around in the source code, or search on "Qt Memory Management" on Google, but the short answer is that Qt almost certainly isn't going to honor your modified new() and delete() operators because it implements it's own versions that will override yours.

    Generally, overriding new() and delete() is considered bad programming practice, and conflicts like these are only one example of the problems it can cause. If you're going to do custom memory management, you should just do it, using your own routines, rather than overriding routines that are expected to have certain behavior. Same applies to the Trolls, who should also avoid such practices imho.

  3. #3

    Default Re: Overriding global new

    Hi,
    Thank you for your reply. There is one thing I would like to know: Does Qt's new operator returns 0 on unsuccessful memory allocation or it throws bad_alloc memory exception?
    Regards,

Similar Threads

  1. overriding QListWidget advice
    By codebehind in forum Qt Programming
    Replies: 3
    Last Post: 28th September 2010, 22:39
  2. Replies: 2
    Last Post: 5th July 2010, 21:53
  3. Overriding drawRubberBand()
    By andrew.nguyen in forum Qwt
    Replies: 3
    Last Post: 21st April 2010, 06:58
  4. Overriding QListView painting
    By efegea in forum Qt Programming
    Replies: 2
    Last Post: 29th November 2008, 17:16
  5. overriding the QWidget::contextMenuEvent
    By smarinr in forum Qt Programming
    Replies: 1
    Last Post: 9th May 2008, 21:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.