Click here to Skip to main content
16,008,719 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: getting a temporary folder without ~? Pin
Michael Dunn9-Nov-02 7:52
sitebuilderMichael Dunn9-Nov-02 7:52 
GeneralRe: getting a temporary folder without ~? Pin
Dominik Reichl9-Nov-02 8:04
Dominik Reichl9-Nov-02 8:04 
Generalmemory question Pin
includeh109-Nov-02 0:15
includeh109-Nov-02 0:15 
GeneralRe: memory question Pin
S O S9-Nov-02 1:02
S O S9-Nov-02 1:02 
GeneralRe: memory question Pin
includeh109-Nov-02 2:20
includeh109-Nov-02 2:20 
GeneralRe: memory question Pin
ian mariano9-Nov-02 8:27
ian mariano9-Nov-02 8:27 
GeneralRe: memory question Pin
Anders Molin9-Nov-02 14:37
professionalAnders Molin9-Nov-02 14:37 
GeneralRe: memory question Pin
Gary R. Wheeler9-Nov-02 3:03
Gary R. Wheeler9-Nov-02 3:03 
You can hook memory allocations in the runtime library via something like the following (I pulled this out of some software of mine):
FILE *_AllocHookFile = NULL;
bool _AllocHookFileClose = false;

void _AllocHookInitialize()
{
    _AllocHookFile = fopen("AllocHook.Log","w");
    _CrtSetAllocHook(_AllocHook);
}

void _AllocHookTerminate()
{
    _AllocHookFileClose = true;

    if (_AllocHookFile != NULL) {
        fclose(_AllocHookFile);
        _AllocHookFile = NULL;
    }
}

int _AllocHook(int allocType, 
               void *userData, 
               size_t size, 
               int blockType, 
               long requestNumber, 
               const unsigned char *filename, 
               int lineNumber)
{
    if (requestNumber == 87) DebugBreak();

    if (blockType == _CRT_BLOCK) return TRUE;
    if (_AllocHookFileClose)     return TRUE;

    long        _request    = requestNumber;
    char        _allocType  = '?';
    char        _blockType  = '?';
    int         _size       = size;
    char        *_filename  = (char *)filename;
    int         _line       = lineNumber;

    switch (allocType) {
        case _HOOK_ALLOC:   _allocType = 'A';   break;
        case _HOOK_REALLOC: _allocType = 'R';   break;
        case _HOOK_FREE:    _allocType = 'F';   break;
    }

    switch (blockType) {
        case _FREE_BLOCK:   _blockType = 'F';   break;
        case _NORMAL_BLOCK: _blockType = 'N';   break;
        case _IGNORE_BLOCK: _blockType = 'I';   break;
        case _CLIENT_BLOCK: _blockType = 'C';   break;
    }

    if (allocType == _HOOK_FREE) {

        if (_size == 0) {
            _size = _msize_dbg(userData,blockType);
        }
            
        if (userData != NULL) {

            typedef struct _CrtMemBlockHeader
            {
               struct _CrtMemBlockHeader *pBlockHeaderNext; 
               struct _CrtMemBlockHeader *pBlockHeaderPrev; 
               char *szFileName;   // File name
               int nLine;          // Line number
               size_t nDataSize;   // Size of user block
               int nBlockUse;      // Type of block
               long lRequest;      // Allocation number
               unsigned char gap[4];  
            } _CrtMemBlockHeader;

            _CrtMemBlockHeader *header = (((_CrtMemBlockHeader *)userData)-1);

            if (_filename == NULL) {
                _filename = header->szFileName;
            }

            if (_line == 0) {
                _line     = header->nLine;
            }

            if (_request == 0) {
                _request = header->lRequest;
            }

        }

    }

    fprintf(_AllocHookFile,"%10d %10d %c %c %s;%d\n",
            _request,_size,_allocType,_blockType,_filename,_line);

    return TRUE;
}


The initialize function opens a file where allocations are logged and sets the allocation hook, and the terminate function closes it. The hook function gets called on each heap operation. You can look at the resulting log file to see where things are allocated and deallocated.


Software Zen: delete this;
GeneralTextOut under WinCE!?! Pin
Daniel Strigl8-Nov-02 23:40
Daniel Strigl8-Nov-02 23:40 
Questionhow do I prevent 'oledlg.dll' linker references? Pin
Jan Van Der Mix8-Nov-02 23:24
sussJan Van Der Mix8-Nov-02 23:24 
AnswerRe: how do I prevent 'oledlg.dll' linker references? Pin
Tim Smith9-Nov-02 13:39
Tim Smith9-Nov-02 13:39 
GeneralFrozen window Pin
S O S8-Nov-02 22:37
S O S8-Nov-02 22:37 
GeneralRe: Frozen window Pin
includeh109-Nov-02 0:27
includeh109-Nov-02 0:27 
GeneralRe: Frozen window Pin
S O S9-Nov-02 0:58
S O S9-Nov-02 0:58 
QuestionHow to use CHtmlView? Pin
Chen Jiadong8-Nov-02 20:44
Chen Jiadong8-Nov-02 20:44 
AnswerRe: How to use CHtmlView? Pin
Nasty_p24-Nov-02 15:52
Nasty_p24-Nov-02 15:52 
GeneralTo the person helping me with the edit control Pin
Anonymous8-Nov-02 16:29
Anonymous8-Nov-02 16:29 
GeneralRe: To the person helping me with the edit control Pin
includeh109-Nov-02 0:20
includeh109-Nov-02 0:20 
GeneralRe: To the person helping me with the edit control Pin
Anonymous9-Nov-02 1:37
Anonymous9-Nov-02 1:37 
GeneralRe: To the person helping me with the edit control Pin
includeh109-Nov-02 2:29
includeh109-Nov-02 2:29 
GeneralRe: THANK YOU!!!!!!!! Pin
georgiek509-Nov-02 6:05
georgiek509-Nov-02 6:05 
GeneralCCriticalSection & atomic operation Pin
zecodela8-Nov-02 16:25
zecodela8-Nov-02 16:25 
GeneralRe: CCriticalSection & atomic operation Pin
Anatari8-Nov-02 16:32
Anatari8-Nov-02 16:32 
QuestionHow to view function names in Workspace pane Pin
Member 18557708-Nov-02 15:36
Member 18557708-Nov-02 15:36 
AnswerRe: How to view function names in Workspace pane Pin
Stephane Rodriguez.9-Nov-02 2:35
Stephane Rodriguez.9-Nov-02 2:35 

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.