|
|
|
|
Hi All,
i have an MFC application receive event message and based on the message type to new "Order" object. the created object stored in a map and never delete thro' the application cycle.
my "Order" class size only < 1k byte. however, the application memory grow as 100k per order inputted.
i added the CRT memory leak statement (use _CrtMemDifference, _CrtMemCheckpoint) to see the difference after executed function for each event. the result can show the object size (Order class) i created. however, the 100K grow rate still not able to show.
questions:
A) i wonder i dont know what reflect in those functions. anyone have idea on this?
B) more effective question is how can i print the memory size of my application?
so, i can check the memory usage difference for each big function call and drill down the problematic function.
like this :
printf("b4 handler"); printMemSize();
BigHandler();
printf("after handler"); printMemSize();
any reply will be highly appreciated!
thanks!
jim
|
|
|
|
|
Maybe try something like this:
#ifdef _DEBUG
class _MemStateCheck
{
public:
CMemoryState oldMemState, newMemState, diffMemState;
_MemStateCheck();
~_MemStateCheck();
};
_MemStateCheck::_MemStateCheck()
{
oldMemState.Checkpoint();
}
_MemStateCheck::~_MemStateCheck()
{
newMemState.Checkpoint();
if( diffMemState.Difference( oldMemState, newMemState ) )
{
TRACE( "************************\n" );
TRACE( "Memory leaks Detected\n" );
TRACE( "************************\n" );
diffMemState.DumpStatistics();
diffMemState.DumpAllObjectsSince();
}
else
{
TRACE( "************************\n" );
TRACE( "No memory leaks Detected\n" );
TRACE( "************************\n" );
}
}
_MemStateCheck MemCheckObj;
#endif //#ifdef _DEBUG
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
The code in the first block checks for leaks between the construction and destruction of the
static _MemStateCheck MemCheckObj variable. It also shows how you can get memory checkpoints
and dump the difference between them to the debugger output window.
The code in the second block sets the use of the debug new operator and insures file/linenumber
info is available so the leak dump has more useful information.
Mark
This episode brought to you by the number 5
|
|
|
|
|
Thanks! Mark.
This is what i did but i used the _CRTMemXXXX function from crtdbg.h. i guess your CMemoryState is from MFC.
I have i question. If there is no memory leak, but i want to print the app's allocated memory size (sth like the "memory" column in task manager). Any function can serve this purpose?
thanks!
jim
|
|
|
|
|
|
Mark,
Thank you for your quick response!
let me take a look!
Jim
|
|
|
|
|
How can i print application Memory in some file?
Pankaj
|
|
|
|
|
__FILE__ is replaced (during compilation) with the name of the file
__LINE__ is replaced (during compilation) with the current line
But how do I get the name of the function I'm just in?
I'm using VisualStudio 6.0, programming in pure C. I want to call a function writing text to a Window like:
OutputWindow("functionname: bla bla bla %d");
The functionname should be replaced by the function that called OutputWindow()
OutputWindow("%s: bla bla bla %d", __FOONAME__);
Thanks for any help!
|
|
|
|
|
AdAkta wrote: But how do I get the name of the function I'm just in?
I'm using VisualStudio 6.0, programming in pure C.
No way that I'm familar with. See here for another possibility.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Thanks for your hint! I will try xtrace.
|
|
|
|
|
You can use __FUNCTION__ as per the following from MSDN[^]
__FUNCTION__ Valid only within a function and returns the undecorated name of the enclosing function (as a string). __FUNCTION__ is not expanded if you use the /EP or /P compiler option.
The only down side is that it's Microsoft specific
In my own code I put a first line into each fuction like this
CONTEXT( _TXT( "CCoolClass::FantasticFunction" ) );
The CONTEXT Macro vanishes in Release builds but in Debug builds it expands into a FunctionContext object which goes on the stack and has all sorts of useful functions. The current FunctionContext object is tracked so I can do my own custom stack traces at runtime limited to my code of course, detect stack corruption, maintain a trace-level variable that records how deep down the funtion stack I am and allows easier formatting of runtime debug output, retrieve the function name anywhere I need it and it even has a special registry within it so I can hook temporary heap allocations to it and unwind them automatically if an exception is thrown that would cause them to leak So much good stuff for adding one line of code to each function is not to be missed.
Nothing is exactly what it seems but everything with seems can be unpicked.
|
|
|
|
|
Sounds great! Have you published your macro here?
The __FUNCTION__ or __func__ intrinsics do not work with VC++ 6.0 - as far as I know! Only with other compilers like GCC
|
|
|
|
|
AdAkta wrote: The __FUNCTION__ or __func__ intrinsics do not work with VC++ 6.0
That's a pity. I hope you have found a successful solution elsewhere.
I haven't published my Function Context Macro on CP yet but I hope to eventually. I'm working on a Patterns related article and after that hopefully a comprehensive Error/Exception handling scheme which will definitely include Function Context.
Nothing is exactly what it seems but everything with seems can be unpicked.
|
|
|
|
|
What is the correct way to do this, I have a dialog and when I hit enter after editing a number in an edit box, it's acting like another button is being pressed even though that's not active by tab order or whatever.
I'm sure I remember seeing a way that you could trap the Enter key so it doesn't do anything unexpected
t.i.a
Lee
|
|
|
|
|
See if this helps.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
thanks, I have OnOk and OnCancel managed manually, and my program correctly exits via the red X button on the title bar, and through File->Exit
my problem is not that the dialog is closing but that pressing the Enter key while in an edit control is somehow activating a press of another button thats adding that data to a listview elsewhere. Since the user is typing into the edit control, I assumed the focus was there, so don't understand how that is turning into the same action as pressing this other button (that is not at the top of the tab order tree, so it's not like it's defaulting to that)
|
|
|
|
|
Does the edit control have the ES_WANTRETURN style?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I've tried it with (by setting the "want return" style in the visual dialog editor) and without (which is was before). same result!
|
|
|
|
|
Fair enough. Without walking through the code myself, I've no more suggestions.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
OK, removing the "default button" style now works, but NOW it closes the dialog, even though I have overridden the OnOk and OnCancel functions.
|
|
|
|
|
which is fine, I've now stopped OnOk from closing the diagram
thanks chaps!
|
|
|
|
|
When you press enter the framework calls the default action for OK (just like pressing the IDOK button).
So you either have to override OnOk method or set that edit box to "Want Return"(which you can do from the Properties window)
|
|
|
|
|
no, it's not exiting, it's acting like a button is being pressed. the edit control is Tab oRder 5, contains a number, there is another numeric edit at tab order 6, then tab order 7 is a button which adds some calculated data to a listview report. hitting enter after the data in edit box 5 is causing the data to be added to the table.
Is there any way I can track what's causing this?
|
|
|
|