Click here to Skip to main content
13,090,299 members (77,006 online)
Click here to Skip to main content
Add your own
alternative version


13 bookmarked
Posted 12 Sep 2005


, 12 Sep 2005
Rate this:
Please Sign up or sign in to vote.
Thread-safe detection to find if an object is dynamically or statically allocated using MFC.

Sample Image - ObjectIsDynamic_ScreenShot.gif


I have seen too many people trying to determine if an object is allocated on the heap or not. It can help to create a garbage collection or find if an object with a reference counter should automatically call the delete operator or not when its counter reaches zero.

You can search in the web for many articles dealing with this but most of them have the problem that they rely on how the memory is initialized or they aren't thread safe.

But with the help of thread local storage that the operating system provides and a very rarely used stuff that MFC has, we can deal with this problem easily. MFC gives us some classes to store information private for each thread with the THREAD_LOCAL macro (you can see more in afxtls.cpp and afxtls_.h in the MFC source code). So taking advantage of this macro I created an internal class that is used when the object is constructed and when it is allocated on the heap, by overriding the new and delete operators.

The new operator of the base class initializes the internal detector of the thread that is currently allocating the object if it was not done previously and then sets a flag indicating that the object is being allocated on the heap. Later, when the constructor is called, the base class checks if this flag is on or not to determine if the object is on the heap or not.

Using the code

The only thing you must do to detect if your objects are allocated dynamically or not is to derive them from CDynamicObjectBase and then call the IsAllocatedOnDynamicMemory function anywhere you wish.

To use the code in your project, add the DynamicObjectBase.cpp and DynamicObjectBase.h files to your project and add CDynamicObjectBase as a base class for your classes.

class yourg_class : public CDynamicObjectBase

In the sample application you will see a class named CMyCustomObject that has a function that prints where it was allocated.

Known issues

  • To make the code MFC-independant you must create a copy of the functionality of most classes and functions located in afxtls_.h and afxtls.cpp. I did it and it is not difficult for an experienced programmer.
  • There is a function named DynamicObjectBaseReset that resets the internal state of the detector in the thread context of the caller. I didn't test what happens if an exception is thrown during the object's constructor. I think nothing happens because CDynamicObjectBase's constructor is called before any derived constructors (it is the base class), or you can modify the code to catch exceptions, but for any case the function is provided.

As a final note, suggestions for improvements are welcome.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Mauro Leggieri
Web Developer
Argentina Argentina
My name is Mauro Leggieri. I am 30 year-old, married and have a child.

I am a system engineer (UTN university) and am being programming for more than 20 years from the C64 to the PC and some microcontrollers.

Mostly of my time, I program games for Windows.

Soon, my site

You may also be interested in...

Comments and Discussions

GeneralAn alternative Pin
emilio_grv12-Sep-05 23:14
memberemilio_grv12-Sep-05 23:14 
GeneralRe: An alternative Pin
Mauro Leggieri13-Sep-05 2:57
memberMauro Leggieri13-Sep-05 2:57 
GeneralGreat for third party libraries. Pin
WREY12-Sep-05 10:37
memberWREY12-Sep-05 10:37 
GeneralRe: Great for third party libraries. Pin
Mauro Leggieri12-Sep-05 11:33
memberMauro Leggieri12-Sep-05 11:33 
GeneralIf Ya Really got to know... Pin
Blake Miller12-Sep-05 12:17
memberBlake Miller12-Sep-05 12:17 
GeneralRe: If Ya Really got to know... Pin
Blake Miller12-Sep-05 12:18
memberBlake Miller12-Sep-05 12:18 
GeneralCool. Pin
WREY12-Sep-05 12:49
memberWREY12-Sep-05 12:49 
GeneralRe: If Ya Really got to know... Pin
Mauro Leggieri12-Sep-05 16:13
memberMauro Leggieri12-Sep-05 16:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170813.1 | Last Updated 12 Sep 2005
Article Copyright 2005 by Mauro Leggieri
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid