Jump to content
  • Advertisement
Sign in to follow this  
AxeGuywithanAxe

Custom Memory Allocations and memory allocation

This topic is 704 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

Hey guys, so I seem to be having this issue with memory allocation. I am using a large linear allocator, and in 9 out of 10 cases it works perfectly, but for some reason, when I allocate a class I get a memory access violation when initializing a few of the variables in the constructor. if I allocate sizeof(T) + 1, then I don't have an access violation, I've been stuck on this for a while, and any suggestions would help.

Share this post


Link to post
Share on other sites
Advertisement
You're sure that you're not returning allocations that are outside the bounds of your large linear block?

Do you take alignof(T) into account during allocation?

Share this post


Link to post
Share on other sites

I've tried that also, and it doesn't work. For testing purposes, I wrote code like such : 


//allocate a memory block 
static uint8* GAllocator = new uint8[1024*1024];

CRenderView* AllocateView()
{
    return new(GAllocator)CRenderView();
}


void Main()
{
  auto* RenderView = AllocateView();
}

the access violation occurs in the constructor. As of now I'm guessing i'm having some major memory corruption somewhere in the application, but can't seem to figure out where.

Share this post


Link to post
Share on other sites

Well, I've moved the code to different sections of my project so there isn't a specific call stack that works and doesn't work. I'm not quite sure what the issue may be, I don't think it can be an issue of allocating over dll lines, because I have 5 other dlls that never suffer this issue. It is specifically this class alone that has an issue, and I can't seem to decipher what's different.

Share this post


Link to post
Share on other sites

It is specifically this class alone that has an issue

You probably need to share some code from its constructor then.
Somehow, it's trying to read data from (T*)-1... Which could be as simple as there being a member variable that's a pointer, which is read from before it's initialized.

 

I find that when making a custom allocator, it's a good idea to memset the returned allocations to something like 0xcd / etc in non-shipping builds, as this will make these kinds of "read from uninitialized pointer" bugs show up as "Access violation reading from 0xcdcdcdcdcdcdcdcd" as well as making uninitialized variables stand out like a sore thumb in the debugging watch windows or memory view.

Share this post


Link to post
Share on other sites

Yeah, i tried changing the memory allocated with "0xcd" and it still returned "0xFFFFFFFFF", that's why I'm so confused about where this issue is coming from. Below is the code,

and it breaks after "Matrix.ExtractLeftPlane(....)" , I tried changing around the order of extraction, but it doesn't seem to matter, it always causes an access violation after the second call to "ExtractXXXPlane" whether it's "ExtractFarPlane" or "ExtractLeftPlane", and etc... if I remove the "m_ViewFrustum.Init" from my code base, my code runs perfectly.

 

Share this post


Link to post
Share on other sites

if I remove the "m_ViewFrustum.Init" from my code base, my code runs perfectly

 

Are you sure you have the problem in allocater rather than your matrix bake or frustum init function?

Could you access the baked matrices correctly?

 

What may help is if you create a dump from your class and look if anything is located correctly in the memory you are operating on

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!