Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to get and use filename and linenumber inside _crtsetallochook?
C++
int _cdecl customallochook (int type, void *pdata, size_t size, int use, long request, const unsigned char *file, int line)
{
//how to use *file and line here???
}
Posted
Updated 20-Feb-12 18:45pm
v3
Comments
Mohibur Rashid 21-Feb-12 3:51am    
I am confused with your question, what do you mean by file name and line number?
Stefan_Lang 21-Feb-12 5:15am    
What do you mean by that question? the parameters are passed to that function, so what is not clear about getting them? And how should we know how you intend to use them? We can't possibly help if you don't tell us what you want.
Member 8576081 22-Feb-12 1:25am    
I am sorry I should have mentioned it a bit more clearly. I got my answer..Thanx anyways ! :)
Cheers!!

This is a hook function as it is used by _CrtSetAllocHook of the C runtime system. When you compile your allocation with _DEBUG, these parameters will contain the file name and line number on which the allocation originated from.

You might want to use them for tracking down memory leaks. MFC implements a leak detection mechanism on this basis and your own hook function might want to do something similar. That is why these things are passed. If you don't need them in your hook function, just ignore them.
 
Share this answer
 
Check this[^] for some pre-defined macros.

HTH
Abhi
 
Share this answer
 
The hook receives the file name and line number only if the source file
that called 'new' or malloc was set up to use the debug versions of
'operator new' and malloc and pass them this information.

This can be done in the following way:

1) Usually in the precompiled header file:

XML
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#define DEBUG_NEW new(_NORMAL_BLOCK, THIS_FILE, __LINE__)


2) At the beginning of every source file with memory allocations:

XML
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


@nv3 - I guess you mean this right? Even after doing this I am not able to recieve the filename and linenumber in the parameters. Can you please help me with this compilation under _DEBUG thing in detail. You are right i need it for memory leaks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900