Jump to content
  • Advertisement
Sign in to follow this  

Custom memory allocator with Polymorphism

This topic is 1210 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


?I write two custom memory allocators because I thought that my problem was from memory pool,but when I test a new type of custom memory allocation I realize that my problem is from classes inheritance.
I have a base class IComponent you can see code below:

virtual ~IComponent() {}
    #pragma region Setter
       void SetActive(bool _active) { this->m_active = _active; }
       void SetName(const char * _name) { this->m_name = _name; }
    #pragma endregion
    #pragma region Getter
       bool GetActive() const { return this->m_active; }
       const char * GetName() const { return this->m_name; }
    #pragma endregion
       virtual void Initialize(Actor * _owner) = 0;
       virtual void Update(const AngelSubSystem::TimeManager &timer) = 0;
       virtual void Render() = 0;
       Actor* GetOwner()             { return this->m_owner; }
       void SetOwner(Actor * _owner)          { this->m_owner = _owner; }
       bool      m_active;
       const char *    m_name;
       Actor *     m_owner;

And I have a component name GUIText that inherit from this class:

  virtual void Initialize(ActorPtr _owner) override;
  virtual void Update(const AngelSubSystem::TimeManager &timer) override;
  virtual void Render() override;
  Vector3 *               m_position;
  Vector3 *               m_scalling;
  Vector3 *               m_rotation;

some code omitted.
when I use my custom allocator like blow I got this error:

     Access violation reading location 0xCDCDCDD1

this->m_componentMemory = new AngelMemory::LinearAllocator();
    GUIText * t = reinterpret_cast<GUIText*>(this->m_componentMemory->Allocate(sizeof(GUIText)));

I find my problem that it is from inheritance and polymorphism but my question is how I can fix this?

Here is my custom allocator:

 void AngelCore::AngelMemory::LinearAllocator::Init(size_t size)
         this->m_currentPos = malloc(size);
         this->m_size = size;
         this->m_numAllocation = 0;
         this->m_usedMemory = 0;
        void *AngelCore::AngelMemory::LinearAllocator::Allocate(size_t size)
         void *address = m_currentPos;
         this->m_usedMemory += size;
    return (void*)address;


Share this post

Link to post
Share on other sites
C++ objects MUST be constructed before they can be used.
You can use "placement new" to construct an object within an existing memory allocation.
You should also manually call the destructor when releasing the object.
GUIText * t = new(this->m_componentMemory->Allocate(sizeof(GUIText), alignof(GUIText))) GUIText(this);

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!