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

  • Home
  • SEARCH
  • 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 7078893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:36:13+00:00 2026-05-28T06:36:13+00:00

Alright, so I’m currently working on a game and ran into a memory issue

  • 0

Alright, so I’m currently working on a game and ran into a memory issue after refactoring some of the code today.

It uses a component based design and I was modifying how the components were allocated and passed off to entities. Originally some components were allocated as member variables within the entities but now I want to have them allocated elsewhere and passed off to the entity via pointer.

You can see how I implemented this below with sample code from my project. I essentially iterate through all the entities and allocate the components for them. The problem is I’m hitting a access violation at the first line of “starting up” the “instanceObject” on the 6th iteration and have no idea why. Using the debugger, it doesn’t look like any of the variables point to a invalid address.

Here is what I’m doing to create the entities and components.

for (unsigned int i = 0; i < 512; ++i) {
    InstanceObject* _pInstanceObject = new InstanceObject;

    // Initialize temporary variables
    XMFLOAT3 _position, _rotation;
    float _angle = (i / 512.0f) * (2.0f * XM_PI),
          _scale = (float)pResourceManager->numberGenerator.GetInt(50, 5);

    _position.x = 100000.0f * cos(_angle) + pResourceManager->numberGenerator.GetInt(50000, -25000);
    _position.y =(float) pResourceManager->numberGenerator.GetInt(50000, -25000);
    _position.z = 100000.0f * sin(_angle) + pResourceManager->numberGenerator.GetInt(50000, -25000);

    _rotation.x = (XM_PI * 2) * (pResourceManager->numberGenerator.GetInt(100, 0) / 100.0f);
    _rotation.y = (XM_PI * 2) * (pResourceManager->numberGenerator.GetInt(100, 0) / 100.0f);
    _rotation.z = (XM_PI * 2) * (pResourceManager->numberGenerator.GetInt(100, 0) / 100.0f);

    // Set component's state using the temporary variables.
    _pInstanceObject->StartUp(&_position,
        &_rotation,
        &XMFLOAT3(_scale, _scale, _scale),
        &XMFLOAT3(0.0f, 0.0f, 1.0f),
        &XMFLOAT3(1.0f, 0.0f, 0.0f),
        &XMFLOAT3(0.0f, 1.0f, 0.0f)
        );

    // Hand pointer of the component to entity.
    // Entity will handle deallocating component
}

And here is the relevant code from the component.

class InstanceObject {
private:
    XMVECTOR anteriorAxis,
         lateralAxis,
         normalAxis,
         position,
         rotationQuaternion,
         scale;

    XMMATRIX translationMatrix,
         rotationMatrix,
         scaleMatrix;
    void SetAnteriorAxis(const XMFLOAT3 *_anteriorAxis) { anteriorAxis = XMLoadFloat3(_anteriorAxis); }
    void SetLateralAxis(const XMFLOAT3 *_lateralAxis)   { lateralAxis = XMLoadFloat3(_lateralAxis); }
    void SetNormalAxis(const XMFLOAT3 *_normalAxis)     { normalAxis = XMLoadFloat3(_normalAxis); }
public:
    InstanceObject(void)  { }
    InstanceObject(const InstanceObject& _object) : anteriorAxis(_object.anteriorAxis), lateralAxis(_object.lateralAxis),
        normalAxis(_object.normalAxis), position(_object.position), rotationQuaternion(_object.rotationQuaternion), scale(_object.scale),
        translationMatrix(_object.translationMatrix), rotationMatrix(_object.rotationMatrix), scaleMatrix(_object.scaleMatrix) {}
    ~InstanceObject(void) { }

    bool StartUp(const XMFLOAT3 *_position, const XMFLOAT3 *_rotation, const XMFLOAT3 *_scale,
        const XMFLOAT3 *_lookAxis, const XMFLOAT3 *_strafeAxis, const XMFLOAT3 *_upAxis);

    void SetPosition(const XMFLOAT3* _position) { position = XMLoadFloat3(_position); }
    void SetRotationQuaternion(const XMFLOAT3 *_rotation) { rotationQuaternion = XMQuaternionRotationRollPitchYaw(_rotation->x, _rotation->y, _rotation->z); }
    void SetScale(const XMFLOAT3 *_scale) { scale = XMLoadFloat3(_scale); }
}

bool InstanceObject::StartUp(const XMFLOAT3 *_position, const  XMFLOAT3 *_rotation, const XMFLOAT3 *_scale,
        const XMFLOAT3 *_lookAxis, const XMFLOAT3 *_strafeAxis, const XMFLOAT3 *_upAxis) {
    SetPosition(_position);
    SetRotationQuaternion(_rotation);
    SetScale(_scale);   
    SetAnteriorAxis(_lookAxis);
    SetLateralAxis(_strafeAxis);
    SetNormalAxis(_upAxis);

    return true;
}

Any idea of what could be causing this behavior and how should I fix it?

  • 1 1 Answer
  • 0 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-28T06:36:14+00:00Added an answer on May 28, 2026 at 6:36 am

    I believe the issue is that the XMVECTOR in your InstanceObject class need to be 16 byte aligned, and the new operator won’t guarantee this for you. You can add a quick check into your code to confirm – check whether the InstanceObject pointer & 0xF is non zero in the iteration in which it crashes.

    If that is the case you can write a custom allocator that guarantees the right alignment and use placement new.

    It seems to be a fairly common problem with XMVECTOR when used as a member (here’s an existing Connect bug report on this issue, there are a few web pages if you search).

    If you just want to fix it quickly to let you get on with other things, you can add a static operator new and delete to your class declaration implemented something like the following snippet:

    void* InstanceObject::operator new( size_t size )
    {
        // _aligned_malloc is a Microsoft specific method (include malloc.h) but its
        // straightforward to implement if you want to be portable by over-allocating
        // and adjusting the address
        void *result = _aligned_malloc( size, 16 );
        if( result )
            return result;
    
        throw std::bad_alloc(); 
    }
    
    void InstanceObject::operator delete( void* p )
    {
        if( p ) _aligned_free(p);
    }
    

    If the InstanceObject share a common lifetime you could replace the use of _aligned_malloc with your own aligned arena allocator and make the delete a no-op to improve efficiency.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, I'm injecting some code into another process using the CreateRemoteThread/LoadLibrary trick. I end
Alright, I've got a quick question. I'm currently working with a legacy database, so
Alright, I'm currently working to create on an account mainpage a applet to show
Alright, I'm not the best with JOIN's and after all these years of working
Alright. So I figure it's about time I get into unit testing, since everyone's
Alright, currently I have my SWF hitting a php file that will go and
Alright, I'm trying to read a comma delimited file and then put that into
Alright, after doing a ton of research and trying almost every managed CPP Redist
Alright y'all. I am attempting to write a bit of code that places typographic
Alright I am having a world of difficulty tracking down this memory leak. When

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.