How to find the source filename and line no. from memory pointer in debug version.

19 04 2008


It will be nice, if its possible to find out the source filename and line number where the pointer is allocated by just using the memory pointer itself. Yes its possible! But only in debug version.


When you call new, the c runtime library allocates the memory and returns the pointer. But infront of the given pointer, one CRT MemoryBlock Header is secretly kept in debug version. see the declarations of Debug MemoryBlock header below.

typedef struct _CrtMemBlockHeader
{
struct _CrtMemBlockHeader * pBlockHeaderNext;
struct _CrtMemBlockHeader * pBlockHeaderPrev;
char *                      szFileName;
int                         nLine;
size_t                      nDataSize;
int                         nBlockUse;
long                        lRequest;
unsigned char               gap[nNoMansLandSize];
} _CrtMemBlockHeader;

In this header, several information about the allocated memory block is available – such as size of memory block, source filename and line no. where the memory is allocated, whether the memory block currently in use etc etc.


From the pointer, access the MemoryBlock header. Then all these informations will be available. See the code snippet below.

// decleration of MemoryBlock header from CRT Source.
#define nNoMansLandSize 4
typedef struct _CrtMemBlockHeader
{
struct _CrtMemBlockHeader * pBlockHeaderNext;
struct _CrtMemBlockHeader * pBlockHeaderPrev;
char *                      szFileName;
int                         nLine;
size_t                      nDataSize;
int                         nBlockUse;
long                        lRequest;
unsigned char               gap[nNoMansLandSize];
} _CrtMemBlockHeader;

...

// Allocate some memory.
float* pFloat = new float[100];

// Get the memory block header.
_CrtMemBlockHeader* pMemoryBlockHeader =
    reinterpret_cast<_CrtMemBlockHeader*>( pFloat );
--pMemoryBlockHeader;

// Source File and location where the memory is allocated.
CString csSourceFileName = pMemoryBlockHeader->szFileName;
DWORD dwLineNumber = pMemoryBlockHeader->nLine;


The memory allocation is different in debug and release. isn’t it? 😉


Targeted Audience – Advanced.

Advertisements




How to update bulk amount of data to GUI controls without flicker.

19 04 2008


Usually for Listbox and Tree control, may be you need to add and delete thousands of entries. While doing so, its obvious that the control will flicker. It might be annoying to the user. So how can you avoid it?


You can use the api – LockWindowUpdate() and UnlockWindowUpdate() for remove the flicker while updating the UI. If a window is locked by calling LockWindowUpdate(), the the further call for getting the device context will return one with empty visible region. When you call UnlockWindowUpdate(), system invalidates the area and will send WM_PAINT. The WM_PAINT will be send only if some drawing is done to the window after locking.

You can download sample from here, which shows the difference.

See sample code snippet below.

// Lock the EditBox.
m_EditBox.LockWindowUpdate();

// Add any number of strings to it.
for( DWORD Index = 0; Index < 5000; ++Index )
{
    CString csMessage;
    csMessage.Format( _T("Item %d"), Index );

    m_EditBox.AddString( csMessage );
}

// After updating, unlock it.
m_EditBox.UnlockWindowUpdate();


Please note that, only one window can be locked at a time.


Targeted Audience – Beginners.