Custom Memory Allocations and memory allocation

Started by
10 comments, last by AxeGuywithanAxe 7 years, 5 months ago

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.

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?

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.

Can you post the exact access violation error message?

I use DLLs, but all DLLs use a shared allocator.

What's the call stack and code location of that exception?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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.

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.

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.

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

This topic is closed to new replies.

Advertisement