Sign in to follow this  

Hooking DirectX memory allocations

This topic is 4268 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'm working on implementing more in depth memory tracking, and I'm wondering if there's a way to hook the memory allocations for DX8 and DX9. I know the Xbox SDK exposed a few functions for exactly that purpose. As far as DX on PC goes, I've not been able to find anything comparable. Google has not been a great deal of help this round. The dx docs don't give me much hope in this case(unless I'm just blind). Any info would be a great help! Thanks! -dave

Share this post


Link to post
Share on other sites
The debug D3D runtime will report memory leaks associated with D3D when your app exits.

There's also AppVerifier which tracks all sorts of resources used by your app and will report when they are misused or leaked. You can also write your own shims for AppVerifier, hooking whatever you need.

I suppose if you wanted to track memory usage of your app while it's running you could use the ToolHelp APIs to walk the heaps used by your process (or use the snapshot features of HeapWalker).

I'm curious why you want to track the internal allocations made by D3D? On a console with a fixed amount of memory, this sort of tracking makes sense but on a PC you don't have those constraints. As long as you release the COM objects that you've created, the memory will be freed. As far as how much memory is being allocated when making a particular D3D API call, what is the benefit of knowing this? It's not like you can do much about it from the app level.

Share this post


Link to post
Share on other sites
The point in this case is not just leak detection. I'd like to be able to get an accurate view of of resource usage at any given point. This way you can find a point where you're using more memory than you expected by a given system, and can compensate for it.

Mainly it's for checks and balances.

The goal then is to optimize game content and application usage of those resources to reduce the usage of memory in those memory "hot spots".

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Can you say PIX?

Try it out. It's a really powerful D3D profiling tool included with newer DX SDKs.

Share this post


Link to post
Share on other sites
Yes, PIX is a fantastic tool. It does have resource counters.. I don't want to know how much memory was allocated on a given frame. I want to be able to track it at any given moment.

What I'm trying to do is streamline the process of tracking memory allocations and centralize it to one point rather than using several tools.

Share this post


Link to post
Share on other sites
When you say that you want to track memory usage at any given moment, would that be satisfied by something like GetBytesInUse(), that you'd call from your app to get a snapshot of current memory usage?

Or do you want the total bytes currently in use for your process written to a log file with an accompanying time stamp each time the value changes (i.e. each alloc/free)?

Share this post


Link to post
Share on other sites
Basically, the system works like this:

I have a handful of macros that wrap the allocations in order to track the size and type of allocation.

Example:
MEMORY_ALLOC(pMem = new char[256], MEM_TYPE_RENDERER, MEM_POOL_GLOBALHEAP);

MEMORY_FREE(delete [] pMem, MEM_TYPE_RENDERER, MEM_POOL_GLOBALHEAP);

These wrap a static singleton allocator class that handles the actual allocation of memory (ie. HeapAlloc or malloc).

This info is then stored and logged for later use. I have the ability to log the entire callstack for each allocation. This way I can review/compare allocations later using another tool.

So, I would like to manage each allocation if possible.. As an alternative, I would be ok with polling DirectX to give me info on its internal memory usage.

Share this post


Link to post
Share on other sites
In that case, you'll want to hook the APIs used by the D3D runtime to allocate system memory. I'm guessing that would be malloc/HeapAlloc/LocalAlloc. The AppVerifier shim might be worth investigating, or possibly Detours. Have a look at this webpage: http://www.codeproject.com/system/hooksys.asp.





Share this post


Link to post
Share on other sites

This topic is 4268 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this