|
Subject: Memory leak
Category: C++
Hi,
Does any one know why the following code creates a memory leak. It's a console application that uses the MFC library.
#include <iostream>
#include <afx.h>
using namespace std;
void main()
{
CMemoryState before,after,diff;
before.Checkpoint();
cout << 6 << endl;
after.Checkpoint();
if (diff.Difference(before,after)) {
fprintf(stdout,"memory leaked\n");
diff.DumpStatistics();
}
}
The problem is in the line: 'cout << 6 << endl;' because of the '6' if I replace the above line with 'cout << "Hello world" << endl;' there is no memory leak.
Thanks,
Shai
|
|
|
|
|
Don't know if this is a solution, but I think the 'leak' is either a problem related to how the CRT debug methods deal with the templated code in the std implementation - or it also be simply that the memory is not freed until the call to _free_crt after main exits.
This code localizes the problem to the std implementation - if you use iostream.h, there are no leaks reported. (Note this code also eliminates the MFC in the original code)
#include < iostream.h >
#include "crtdbg.h"
int main()
{
{
cout << 9 << endl;
}
_CrtDumpMemoryLeaks();
return 0;
}
I don't think there is a real leak here, but must admit to not seeing the whole picture. I think this code isolates the problem, though.
|
|
|
|
|
Hi, I need to get icon which related to a file extension without having real file.
For example, getting the icon which associate with .avi or .wav,
but without having local file which I can use ShGetFileInfo on it.
Is there a way to get this info except getting it directly from the registry ?
Thanks
yariv
|
|
|
|
|
You can use SHGetFileInfo() for this, just include SHGFI_USEFILEATTRIBUTES in the uFlags parameter, and pass a filename of "x.avi" or "x.wav" or whatever extension.
|
|
|
|
|
Hi,
please have a look at this small code snippet and tell a beginner, why the output is different in debug and release build:
int main(int argc, char* argv[])
{
printf("Entering try-catch block...\n");
try {
int one = 1;
int null = 0;
printf("Calling crash statement...\n");
int result = one / null;
}
catch (...) {
printf("Inside catch block. Handling error!");
}
return 0;
}
Debug output:
Entering try-catch block...
Calling crash statement...
Inside catch block. Handling error!
Release output:
Entering try-catch block...
Calling crash statement...
What's going on here???
Matthias
|
|
|
|
|
Ooo - nice one!
The problem stems from the compilers optimization code. If you enable debug info for the release build you'll see that the assignment for result is never executed. Change the optimizations for the release build to none and the code is executed.
Cool. I've read of optimization problems before, but this one's amazing...
|
|
|
|
|
Further notes...
Maybe amazing is the wrong word, but there is a grey area here.
The optimizer removes the code because the var result is never used. It could be argued that the compiler is wrong to overlook the side effect of the expression one / null, but that side effect is not a c++ exception - its a processor trap which the OS decides to pass on to the program - trapping these with catch(...) is murky ground.
Interesting - when you modify the code to make use of result, causing the compiler to compile the assignment, you get a warning C4723: potential divide by 0. So, while it may not be core C++, the compiler does know of the possible side effects of the expression.
Building with warning level 4 provides more clues, but stops short of the warning that would be most informative - i.e. that the code has been removed.
It does give an ureachable code warning for the catch(...), but it gives that for the case where the assignment is used and compiled as well. Makes sense, since no C++ exception is thrown.
I guess the simplest solution is to give in and reference result somewhere in the try block. Better might be to avoid this type of code and test null before attempting the divison.
|
|
|
|
|
I used in a programm the ButtonST Class from Codeproject...
this button class uses the SetCapture method to draw the
button ( win98 style ). now i initiated in the program that
the user must press a accerelator key ( F1 till F11 ) to
switch to another view.... Its an SDI application developed
under Visual C++ 6.0 ( no SP ). The acckey calls a function
OnKeyF1Pressed()... but when the mouse is over a buttonST
OnKeyF1Pressed won't be called..... I think there's a
problem with the buttonST ( esspecially with SetCapture ).
I tried to called RemoveCapture() when the button is drawed
but that isn't very fine because the buttonst isn't drawn
completely but the view is switched.
hope that someone will help me
BEST REGARDS
ingo eberhardt
SEC
|
|
|
|
|
I am looking for a resource editor component (Freware, Shareware or
commercial) that allows users dynamically select controls from a palette,
draw them on a layer, position them and manage their properties. The
mechanism should be Visio® like.
Thanks
|
|
|
|
|
Hello,
I'm trying to find the best way to use the common tooltip control but with the possibility to have diferent fonts like bold, normal and italic in it.
My first idea was to try the custom draw support of the tooltip control but that does not enable you to have different fonts in one and the same tooltip.
Ok, I thought lets try it with owner draw, but the thing is I'm not sure if that is possible or supported with the tooltip common control.
Is there anyone out there who can give me an explanation how to do this the best way? Is owner drawing supported by the tooltip common control?
My aim is to support an editor for a house made language with the "Parameter Info" feature like the one in VC++, so whenever the next possible argument is entered the tooltip should show the next argument highlighted like with a bold font, simple but...
Andreas.
|
|
|
|
|
I haven't tried this myself, but I would think you could owner-draw the entire tooltip when the NM_CUSTOMDRAW message arrives. The NMCUSTOMDRAW struct has a RECT member that tells you the RECT of the control.
|
|
|
|
|
If you decide to go the path of a owner drawn control then you are welcome to use my Extended Tooltip as a base. It only allows a single font, but maybe there is scope to extend it to provide RTF capabilites within the tooltip window that would provide what you want. A lot of work, I guess - but it would be cool
|
|
|
|
|
I am writing an application for a customer and they have selected that F10 and it's variations (Ctl, Alt, Shift) bring up different windows.
Now I know that F10 is a System key used for Menu's and such, but I have tried WM_SYSCOMMAND and WM_SYSKEYDOWN to no avail. Here is my code for OnSysCommand:
void CMyDialog::OnSysCommand(UINT nID, LPARAM lParam)
{
if (nID == SC_KEYMENU)
{
UINT nKey = VK_F10;
OnFunctionKey(&nKey);
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
This works fine...but it works whether the user presses F10 or just Alt, which I don't want. So the basic question is
"Has anybody used F10 for something other than a menu initializer?"
Thanks
Chris Mancini
|
|
|
|
|
Hi
I did this using
OnSysKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
..
if (VK_F10 == nChar)
..
and it works fine.
Use ::GetKeyState to find out about Alt, Ctrl and so on
|
|
|
|
|
How to make a replication through ODBC in MS Access?
|
|
|
|
|
I have a problem with DevStudio Sp3 running under Win2K. The attach to process dialog doesn't show any processes! Does anyone else have such a problem?
Jignesh
|
|
|
|
|
Found the problem..
See http://support.microsoft.com/support/kb/articles/Q235/4/34.ASP
|
|
|
|
|
One more developers web site is almost ready. Here is discussion board
www.mindcracker.com/forum
|
|
|
|
|
Let's say - for a setup-like application; you could pack an unpacker program and the compressed data in an unique file, for easier download. That's easy enough - just "adding" the compressed data at the end of the unpacker exe file.
But .. under Windows NT, you can open an exe file at runtime, using the standard CFile class, read it, and save in a temporary location part of the file; this method doesn't work under Win9x though.
Anybody knows a better way that works under every MS OSes ?
Thanks!
|
|
|
|
|
I am implementing a listview for a touch screen application. I have placed a CSpinButtonCtrl next to the listview to make it easy to scroll, without having to fiddle with the scroll bar. The question is, how do you PERMANENTLY remove the vertical scroll bars from the listview, without settings the no-scroll option in the dialog template editor.
|
|
|
|
|
hi
i... jeims
help me T_T
|
|
|
|
|
Does anybody know, how to play AVI-Files during an application ?
I would like to it with DirectShow, but i would have to Download the whole DirectX SDK (128Mb !!!).
Does anybody know, where i could download only the DirectShow SDK, or any other solution, how to play AVI-Files ?
|
|
|
|
|
Ahh you're making the same mistake I did!!
DirectShow is NOT part of the DirectX toolkit. MS for some unknown reason decided to move it to the DirectX Media SDK which is spearate! I only learn't this after a 128M download! I does actually say this on the web site, but it is not very clear at all. (Us lot over here in England put it download to our lack of ability to read American!)
Anyway why use DirectX? Try a CAnimateCtrl....
|
|
|
|
|
Isn't there any way to create a button(or adding to a menu) in a COM clients dialog through a function of the COM server(dll)
|
|
|
|
|
Isn't there any way to create a button(or adding to a menu) on a server dialog through a function of a COM server(dll)
|
|
|
|