Skip to main content
GameDev.net gamedev.net
🔒 Locked

Hooking DirectX memory allocations

Started by davidlkoenig Apr 4, 2006 at 12:57 PM 6 replies 5.2k views
Original Post
davidlkoenig
davidlkoenig
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
don
don
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.

davidlkoenig
davidlkoenig
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".
davidlkoenig
davidlkoenig
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.
don
don
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)?

davidlkoenig
davidlkoenig
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.
don
don
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.





Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.