|
and I want to know where to call "_CrtSetDbgFlag (ON);" is
the best? I put it in a class method's first line.
I have define new as ::new(_T(__FILE__), __LINE__)
jason
|
|
|
|
|
in Tip 2,you said :
Tip 2), the utility display the address in hexa, and you can add a small
code to the operator new function, just after the first malloc:
if (retPtr == (void*)0x76DA0)
dumb instruction; <- place a breakpoint on this one
so you can detect easily which line of your code called the operator new
to allocate memory at the specified address and wasn't freed.
but every time the program use different memory address,how can this
method work?
jason
|
|
|
|
|
I want to know where the the memory leak happened,I put #define new new(_T(__FILE__), __LINE__) in *.cpp file,but it doesn't work,Why?Can you help me?
Thanks a lot!
|
|
|
|
|
If any person out there who has a heart please help me. I am writing a task manager that does the same fuctions as the on in win98 in c++. I am have troubles because I am not good in programming and to make matters worse it must be finished by Tuesday morning. If any one who has got code for it or can write the code that can display the active programs that are running in the background, I will be very thank full. I have looked the entire web for code but found nothing, the one or two that I do find has got header files missing and does not work. Please any one help me and contact me at phienix3@hotmail.com.
T hanks
|
|
|
|
|
Hi
Try calling EnumProcesses() to enumerate the running processes. EnumProcesses() will get you an array of process ids. If you need the file name of the file that has spawned each process, try using GetModuleFileNameEx() . Before calling this make sure you enumerate all the modules associated with the process by calling EnumerateProcessModules() .
That complete's half of your work.
After you get the handles to all the processes, from the user interface you have designed take the user input(user clicked on xxx process). Try getting the handle to that process. Manage the process from there.
If you want to terminate the process user selected, use TerminateProcess() function.
The above process WILL NOT work for smart devices.
If your project is for smart devices, use the following technique:
<br />
#include "stdafx.h"<br />
#include "SmartPhoneTaskManager.h"<br />
#include "Tlhelp32.h"<br />
#include <windows.h><br />
#include <commctrl.h><br />
<br />
#define MAX_LOADSTRING 100<br />
<br />
HINSTANCE g_hInst;
HWND g_hWndMenuBar;
<br />
ATOM MyRegisterClass(HINSTANCE, LPTSTR);<br />
BOOL InitInstance(HINSTANCE, int);<br />
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);<br />
<br />
BOOL CALLBACK TaskManagerDlgProc(const HWND hTaskManDlg, const UINT uIMessage, <br />
const WPARAM wParam, LPARAM lParam);<br />
<br />
int WINAPI WinMain(HINSTANCE hInstance,<br />
HINSTANCE hPrevInstance,<br />
LPTSTR lpCmdLine,<br />
int nCmdShow)<br />
{<br />
MSG msg;<br />
<br />
if (!InitInstance(hInstance, nCmdShow)) <br />
{<br />
return FALSE;<br />
}<br />
<br />
<br />
while (GetMessage(&msg, NULL, 0, 0)) <br />
{<br />
{<br />
TranslateMessage(&msg);<br />
DispatchMessage(&msg);<br />
}<br />
}<br />
<br />
return (int) msg.wParam;<br />
}<br />
<br />
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)<br />
{<br />
WNDCLASS wc;<br />
<br />
wc.style = CS_HREDRAW | CS_VREDRAW;<br />
wc.lpfnWndProc = WndProc;<br />
wc.cbClsExtra = 0;<br />
wc.cbWndExtra = 0;<br />
wc.hInstance = hInstance;<br />
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SMARTPHONETASKMANAGER));<br />
wc.hCursor = 0;<br />
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);<br />
wc.lpszMenuName = 0;<br />
wc.lpszClassName = szWindowClass;<br />
<br />
return RegisterClass(&wc);<br />
}<br />
<br />
HWND hTaskManagerDlg;<br />
HWND hListWnd;<br />
DWORD WINAPI ProcessViewThread(PVOID lParam);<br />
<br />
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)<br />
{<br />
HWND hWnd;<br />
TCHAR szTitle[MAX_LOADSTRING];
TCHAR szWindowClass[MAX_LOADSTRING];
<br />
g_hInst = hInstance;
<br />
<br />
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); <br />
LoadString(hInstance, IDC_SMARTPHONETASKMANAGER, szWindowClass, MAX_LOADSTRING);<br />
<br />
hWnd = FindWindow(szWindowClass, szTitle); <br />
if (hWnd) <br />
{<br />
SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));<br />
return 0;<br />
} <br />
<br />
if (!MyRegisterClass(hInstance, szWindowClass))<br />
{<br />
return FALSE;<br />
}<br />
<br />
hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,<br />
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);<br />
<br />
if (!hWnd)<br />
{<br />
return FALSE;<br />
}<br />
<br />
ShowWindow(hWnd, nCmdShow);<br />
UpdateWindow(hWnd);<br />
<br />
hTaskManagerDlg = CreateDialog(hInstance,MAKEINTRESOURCE(IDD_DIALOG_TASKMAN),NULL,(DLGPROC)TaskManagerDlgProc);<br />
if(!hTaskManagerDlg)<br />
{<br />
MessageBox(NULL,TEXT("Could not start taskman"),TEXT("Taskman Failed"),MB_OK|MB_ICONERROR);<br />
return FALSE;<br />
<br />
}<br />
<br />
ShowWindow(hTaskManagerDlg,SW_SHOWMAXIMIZED);<br />
UpdateWindow(hTaskManagerDlg);<br />
hListWnd = ::GetDlgItem(hTaskManagerDlg,IDC_LIST_ENUM_TASKS);<br />
::SetFocus(hListWnd);<br />
DWORD threadID;<br />
CreateThread(NULL,0,&ProcessViewThread,0,0,&threadID);<br />
<br />
return TRUE;<br />
}<br />
<br />
<br />
<br />
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)<br />
{<br />
int wmId, wmEvent;<br />
PAINTSTRUCT ps;<br />
HDC hdc;<br />
<br />
<br />
switch (message) <br />
{<br />
case WM_COMMAND:<br />
wmId = LOWORD(wParam); <br />
wmEvent = HIWORD(wParam); <br />
switch (wmId)<br />
{<br />
case IDM_OK:<br />
break;<br />
case IDM_CANCEL:<br />
DestroyWindow(hWnd);<br />
default:<br />
return DefWindowProc(hWnd, message, wParam, lParam);<br />
<br />
}<br />
break;<br />
case WM_CREATE:<br />
SHMENUBARINFO mbi;<br />
<br />
memset(&mbi, 0, sizeof(SHMENUBARINFO));<br />
mbi.cbSize = sizeof(SHMENUBARINFO);<br />
mbi.hwndParent = hWnd;<br />
mbi.nToolBarId = IDR_MENU;<br />
mbi.hInstRes = g_hInst;<br />
<br />
if (!SHCreateMenuBar(&mbi)) <br />
{<br />
g_hWndMenuBar = NULL;<br />
}<br />
else<br />
{<br />
g_hWndMenuBar = mbi.hwndMB;<br />
}<br />
<br />
break;<br />
case WM_PAINT:<br />
hdc = BeginPaint(hWnd, &ps);<br />
<br />
EndPaint(hWnd, &ps);<br />
break;<br />
case WM_DESTROY:<br />
CommandBar_Destroy(g_hWndMenuBar);<br />
PostQuitMessage(0);<br />
break;<br />
<br />
<br />
default:<br />
return DefWindowProc(hWnd, message, wParam, lParam);<br />
}<br />
return 0;<br />
}<br />
<br />
<br />
<br />
BOOL CALLBACK TaskManagerDlgProc(const HWND hTaskManDlg, const UINT uIMessage, <br />
const WPARAM wParam, LPARAM lParam)<br />
{<br />
<br />
HWND statusHandle = GetDlgItem(hTaskManagerDlg,IDC_STATIC_STATUS);<br />
<br />
BOOL hIsHandled = TRUE;<br />
switch(uIMessage)<br />
{<br />
case WM_COMMAND:<br />
{ <br />
switch(LOWORD(wParam))<br />
{<br />
case IDCANCEL:<br />
hIsHandled = TRUE;<br />
DestroyWindow(hTaskManagerDlg);<br />
PostQuitMessage(0);<br />
break;<br />
case IDC_BUTTON_STOPPROCESS:<br />
<br />
hIsHandled = TRUE;<br />
default:<br />
hIsHandled = FALSE;<br />
}<br />
}<br />
case WM_PAINT:<br />
SetWindowText(statusHandle,TEXT("Running..."));<br />
break;<br />
<br />
}<br />
<br />
return hIsHandled;<br />
<br />
}<br />
<br />
<br />
<br />
DWORD WINAPI ProcessViewThread(PVOID lParam)<br />
{<br />
<br />
<br />
<br />
static int index = 0;<br />
HANDLE snapHand;<br />
PROCESSENTRY32 procEntry;<br />
procEntry.dwSize = sizeof(PROCESSENTRY32);<br />
snapHand = CreateToolhelp32Snapshot(TH32CS_SNAPALL|TH32CS_SNAPPROCESS,0);<br />
if(INVALID_HANDLE_VALUE == snapHand)<br />
return -1;<br />
if(!Process32First(snapHand,&procEntry))<br />
{<br />
if(GetLastError() == ERROR_NO_MORE_FILES)<br />
{<br />
SendMessage(hListWnd,LB_ADDSTRING,0,LPARAM(TEXT("EndOfListing")));<br />
}<br />
return -1;<br />
}<br />
procEntry.dwSize = sizeof(procEntry);<br />
do<br />
{<br />
SendMessage(hListWnd,LB_ADDSTRING,index,LPARAM(procEntry.szExeFile));<br />
SendMessage(hListWnd,LB_SETCURSEL,index,0);<br />
index++;<br />
}while(Process32Next(snapHand,&procEntry));<br />
<br />
HWND stopHandle = GetDlgItem(hTaskManagerDlg,IDC_BUTTON_STOPPROCESS);<br />
::SetFocus(stopHandle);<br />
<br />
<br />
CloseToolhelp32Snapshot(snapHand);<br />
<br />
<br />
<br />
return 0;<br />
<br />
<br />
}<br />
|
|
|
|
|
During debug I am getting following error on Desktop:
"The Connection to the target device has been broken.
Debugging will now stop.
You must now exit and restart eVC"
And the program is exiting with following message in debug window.
The program '...\...' has exited with code -1159943394 (0xBADCAB1E).
|
|
|
|
|
This error is not related to the memory leak detection code.
|
|
|
|
|
I receive this same error-message when i'm running the Remote Call Profile for some minutes.
What can be the cause of that?
|
|
|
|
|
Hi, iam getting the samw error in my project. what cud be the reson for this?.if any one knows the solution please help me out.
|
|
|
|
|
Hi,
Can any one of u tell me how to use this application?? plz provide me the correct & detail procedure on how to use this application to detect memory leaks?
Thanx,
|
|
|
|
|
It doesn't work for MFC classes, because CObject class already defined 'operator new', So, the other 'operator new' member is hidden.
Try to do this:
Intesad of
#define new new(_T(__FILE__), __LINE__)
You must add scope oprator :: for specifing global scope.
#define new ::new(_T(__FILE__), __LINE__)
It works well...
Have a nice day
Roberto
|
|
|
|
|
where (in which file? cpp/h? which position) is the first line of a mfc-application?
where do i have to put that
_CrtSetDbgFlag (ON);
??
thx
thx, Daniel
|
|
|
|
|
Well,
as you know, you should write that code at the beginning of your code in the cpp file. try?
At the moment where you want to activate the flag
Have a nice day
Roberto
|
|
|
|
|
which .cpp file?
thx, Daniel
|
|
|
|
|
hi all,
If you have a problem with this sentence, try to do this:
Instead this sentence:
extern "C" void WINAPIV NKDbgPrintffW .....
...
NKDbgPrintffW(....);
use this other:
char buf[1024];
printf(buf, "<message>",param1, param2,...);
OutputDebugString(buf);
It is more comfortable
have a nice day
Roberto
|
|
|
|
|
Hi all,
The program works well also to MFC, but I have the next warning:
WARNING C4291:
'VOID *__CDECL OPERATOR NEW(UNSIGNED INT,UNSIGNED SHORT *,INT)' : NO MATCHING OPERATOR DELETE FOUND; MEMORY WILL NOT BE FREED IF INITIALIZATION THROWS AN EXCEPTION
but at the end of my program the memory leaks are in the debug window
Can anybody explain to me how can I remove this warning?
Thanks in advanced
Roberto Campos Aliaga
|
|
|
|
|
here there the answer from the autor of the article:
"And the warnign you get it's because C++ will always warn you if you don't have a new-delete pair, that is a delete has to be there for any new operator and delete has to match new's prototype. That's because in some cases C++ needs to call delete operator and it does call the delete operator that matches the prototype of the new operator used.
So you'll simply have to define a delete operator that matches the
prototype."
Have a nice day
Roberto
|
|
|
|
|
When i compile the code it happens , didn't work redefine the operator 'new', how can i solve this problem? Are there other metodes to detect memory leaks for WinCe (vc++)?
Tank's
Georgia Albuquerque
|
|
|
|
|
I have the same problem (if someone is interested)
|
|
|
|
|
I know your post was almost a year ago, but here it is for the next guy who comes here. This worked for VC6, I assume it will for the others.
#if defined _DEBUG
void* operator new(size_t size, LPCSTR lpszFileName, int nLine)
#else
void* operator new(size_t size)
#endif
{
// insert your handler here...
}
Cheers,
Lok
|
|
|
|
|
I'm not clear where this code is suppossed to go
Also not clear what is to go in the
//insert handler here.. part
Any help would be very much appreciated
|
|
|
|
|
Uh ok i've tried, but where's the matching LIB file for WinCE? The normal x86 LIBCD.LIB doesn't work. Do you know where i can find the matching lib file for ARM procs?
Thank you...
|
|
|
|
|
with just litale change it's work fine with MFC
|
|
|
|
|
Hi,
Can you please tell me those lil changes needed for MFC. Because I didnot get it to work. Thanks in advance.
Jalaja.
|
|
|
|
|
Hello,
I tried to insert your code in my project in order to detect memory leaks but, during the link an error appeared telling me that the operators new and delete are already defined. This error appeared during linking with the library "uafxwced.lib(afxmem.obj)".
How did you solve this problem ?
Florian RIVAL
|
|
|
|
|