Jump to content
  • Advertisement
Sign in to follow this  
PyemanKOP

Custom memory allocator with Polymorphism

This topic is 1131 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
    
      public:
    
    #pragma region Getter
    
       bool GetActive() const { return this->m_active; }
    
       const char * GetName() const { return this->m_name; }
    
    #pragma endregion
    
      public:
    
       virtual void Initialize(Actor * _owner) = 0;
    
       virtual void Update(const AngelSubSystem::TimeManager &timer) = 0;
    
       virtual void Render() = 0;
    
      protected:
    
       Actor* GetOwner()             { return this->m_owner; }
    
    
       void SetOwner(Actor * _owner)          { this->m_owner = _owner; }
    
      private:
    
       bool      m_active;
       const char *    m_name;
       Actor *     m_owner;

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

protected:
  virtual void Initialize(ActorPtr _owner) override;
  virtual void Update(const AngelSubSystem::TimeManager &timer) override;
  virtual void Render() override;
 private:
  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();
     this->m_componentMemory->Init(2000);
    GUIText * t = reinterpret_cast<GUIText*>(this->m_componentMemory->Allocate(sizeof(GUIText)));
     t->Initialize(this);

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
Advertisement
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);
....
t->~GUIText();
m_componentMemory-?Release(t);

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!