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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:08:02+00:00 2026-05-16T08:08:02+00:00

This is a design question, assuming C++ and a reference counted object hierarchy. A

  • 0

This is a design question, assuming C++ and a reference counted object hierarchy. A lot of classes in my codebase derive from a common base class (ObjectBase), which implements retain() and release() methods to increase and decrease the reference count of an object instance.

Every instance of an object may be created on the stack or on the heap, using a number of user definable memory allocators. In order for the object instance to commit suicide (delete this) in the release() method if the retainCount reaches 0, the instance must know which allocator it has been constructed with.

At the moment, I am allocating memory for an object instance using an arbitrary allocator, then call placement new to construct the object instance and call a setAllocator() method on the object to set the allocator it has been created with. If the object has been constructed on the stack, the allocator is set to NULL and release() will not call delete. This process is very redundant and potentially error prone (memory leaks, if I forget to call setAllocator, etc…) Ideally I would want to make this a one-step process like this:

Object* o = myPoolAllocator.allocate<Object>(constructor arguments... );

But this makes it very difficult to support and arbitrary number of constructor arguments.

I am just looking for ideas on how to solve this problem. I really like the idea of being able to reference count objects without having to rely on a smart pointer, especially since most classes derive from a common base, anyways.

Thanks for your help.

Florian

  • 1 1 Answer
  • 3 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-16T08:08:03+00:00Added an answer on May 16, 2026 at 8:08 am

    Have a look at this article: Overloading New in C++ . You could overload the new operator for ObjectBase so that it takes your allocator as a parameter and does the rest of the job:

    void *ObjectBase::operator new(size_t size, Allocator *allocator) {
      void *ptr = allocator->allocate(size);
    
      // Hack to pre-initialize member before constructor is called
      ObjectBase *obj = static_cast<ObjectBase *>(ptr);
      obj->setAllocator(allocator);
    
      return ptr;
    }
    

    Normally, the operator is supposed to just return a pointer to the allocated memory, but since you need access to the new object to call your setAllocator method, I’ve included a hack that should (but may not) work. Note that the actual ObjectBase constructor is called after the above function returns, so you should make sure that your constructor does not re-initialize the allocator member.

    And then a similar overload for delete:

    void ObjectBase::operator delete(void *ptr) {
      ObjectBase *obj = static_cast<ObjectBase *>(ptr);
      obj->getAllocator()->free(ptr);
    }
    

    You would then create objects by calling new (allocator) SomeClass(...) where SomeClass derives from ObjectBase.

    Edit: One potential problem with this is that you cannot allocate objects on the stack any more, because there is no way to initialize the allocator to NULL without affecting the how the overloaded new works.

    Update: There is one last (dirty) hack to get it working with both stack and dynamic allocation. You can make new set a global variable (a class static member would work as well) pointing to the current allocator and the constructor could consume this and reset it to NULL. At all other times, this global will already be NULL so an object constructed on the stack will get a NULL allocator.

    Allocator *currentAllocator = NULL;
    
    void *ObjectBase::operator new(size_t size, Allocator *allocator) {
      currentAllocator = allocator;
      return allocator->allocate(size);
    }
    
    ObjectBase::ObjectBase() {
      setAllocator(currentAllocator);
      currentAllocator = NULL;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a design principle question for classes dealing with mathematical/physical equations where the
This is a design question: when do I need to create/use a static method
This is a design question, I noticed that by time by LINQ-to-SQL Context gets
This is a design question. I'm trying to decide between 2 implementations. In order
This is more of a design question. I have a template class, and I
This is a practical Domain Driven Design question: Conceptually, I think I get Aggregate
This should be a very basic design question, but for some reason it doesn't
This is more of a general design question I guess... I have an Ajax
This is probably more a design or style question: I have just been considering
This question is more UI/Design-ish than hard-core programming is. Background: I've been coding in

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.