Click here to Skip to main content
15,886,740 members
Articles / Desktop Programming / MFC
Article

ObjectIsDynamic

Rate me:
Please Sign up or sign in to vote.
3.91/5 (4 votes)
12 Sep 20052 min read 32.8K   320   13   8
Thread-safe detection to find if an object is dynamically or statically allocated using MFC.

Sample Image - ObjectIsDynamic_ScreenShot.gif

Introduction

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.

License

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


Written By
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 http://www.mauroleggieri.com.ar

Comments and Discussions

 
GeneralAn alternative Pin
Emilio Garavaglia12-Sep-05 23:14
Emilio Garavaglia12-Sep-05 23:14 
GeneralRe: An alternative Pin
Mauro Leggieri13-Sep-05 2:57
Mauro Leggieri13-Sep-05 2:57 
GeneralGreat for third party libraries. Pin
WREY12-Sep-05 10:37
WREY12-Sep-05 10:37 
GeneralRe: Great for third party libraries. Pin
Mauro Leggieri12-Sep-05 11:33
Mauro Leggieri12-Sep-05 11:33 
GeneralIf Ya Really got to know... Pin
Blake Miller12-Sep-05 12:17
Blake Miller12-Sep-05 12:17 
GeneralRe: If Ya Really got to know... Pin
Blake Miller12-Sep-05 12:18
Blake Miller12-Sep-05 12:18 
GeneralCool. Pin
WREY12-Sep-05 12:49
WREY12-Sep-05 12:49 
GeneralRe: If Ya Really got to know... Pin
Mauro Leggieri12-Sep-05 16:13
Mauro 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.