Creating controls during runtime
-
I am trying to create some controls during runtime. I resolved all my errors but I think it now has a memory leak. Not sure how to test that, but I don't delete array at the end. I have commented out once place I was trying to delete the array in the WM_CLOSE section. Can anyone help me be able to delete the array and get no errors? here is the code I am using to do this. It also requires a dialog box named IDD_DIALOG1 (generic name) and a push buton IDOK. (just add a dialog into the resource editor and it should work. ---BEGIN CODE--- #include #include #include "resource.h" HWND HWND_DLG_MAIN; HINSTANCE ghInstance; //made global to attempt to reduce possible problems CString szTmp; HWND *StaticArr; HWND *temp; int cnt = 0; int x = 10; int y = 10; int h = 75; int w = 25; int loopctr = 0; BOOL MainWndProc(HWND, UINT, WPARAM, LPARAM);//Main window procedure BOOL MainOnCommand(HWND, WORD, WORD, HWND);//WM_COMMAND procedure int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR, int) { ghInstance = hInstance; DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)MainWndProc, 0);//Create the main dialog box return FALSE; } BOOL MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch(uMsg) { case WM_INITDIALOG: HWND_DLG_MAIN = hWnd; break; case WM_COMMAND: MainOnCommand(HWND_DLG_MAIN, LOWORD(wParam), HIWORD(wParam), (HWND)lParam); break; case WM_CLOSE: for(loopctr = 0; loopctr < cnt; loopctr++) DestroyWindow(StaticArr[loopctr]); //delete [] StaticArr; EndDialog(HWND_DLG_MAIN, 0); DestroyWindow(hWnd); break; } return FALSE; } BOOL MainOnCommand(HWND hWnd, WORD wCommand, WORD wNotify, HWND hControl) { switch(wCommand) { case IDOK: if(cnt > 1) { temp = new HWND[cnt]; } else { temp = new HWND[1]; } temp = StaticArr; if(cnt > 1) { delete [] StaticArr; } StaticArr = new HWND[++cnt]; delete [] temp; szTmp.Format("Label-%d", cnt); StaticArr[cnt-1] = CreateWindowEx( NULL, // extended window style "STATIC", // pointer to registered class name szTmp, // pointer to window name WS_CHILD | WS_VISIBLE | WS_BORDER,// window style x, // horizontal position of window y, // vertical position of window h, // window width w, // window height HWND_DLG_MAIN, // handle to parent or owner window NULL, // handle to menu, or child-win
-
I am trying to create some controls during runtime. I resolved all my errors but I think it now has a memory leak. Not sure how to test that, but I don't delete array at the end. I have commented out once place I was trying to delete the array in the WM_CLOSE section. Can anyone help me be able to delete the array and get no errors? here is the code I am using to do this. It also requires a dialog box named IDD_DIALOG1 (generic name) and a push buton IDOK. (just add a dialog into the resource editor and it should work. ---BEGIN CODE--- #include #include #include "resource.h" HWND HWND_DLG_MAIN; HINSTANCE ghInstance; //made global to attempt to reduce possible problems CString szTmp; HWND *StaticArr; HWND *temp; int cnt = 0; int x = 10; int y = 10; int h = 75; int w = 25; int loopctr = 0; BOOL MainWndProc(HWND, UINT, WPARAM, LPARAM);//Main window procedure BOOL MainOnCommand(HWND, WORD, WORD, HWND);//WM_COMMAND procedure int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR, int) { ghInstance = hInstance; DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)MainWndProc, 0);//Create the main dialog box return FALSE; } BOOL MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch(uMsg) { case WM_INITDIALOG: HWND_DLG_MAIN = hWnd; break; case WM_COMMAND: MainOnCommand(HWND_DLG_MAIN, LOWORD(wParam), HIWORD(wParam), (HWND)lParam); break; case WM_CLOSE: for(loopctr = 0; loopctr < cnt; loopctr++) DestroyWindow(StaticArr[loopctr]); //delete [] StaticArr; EndDialog(HWND_DLG_MAIN, 0); DestroyWindow(hWnd); break; } return FALSE; } BOOL MainOnCommand(HWND hWnd, WORD wCommand, WORD wNotify, HWND hControl) { switch(wCommand) { case IDOK: if(cnt > 1) { temp = new HWND[cnt]; } else { temp = new HWND[1]; } temp = StaticArr; if(cnt > 1) { delete [] StaticArr; } StaticArr = new HWND[++cnt]; delete [] temp; szTmp.Format("Label-%d", cnt); StaticArr[cnt-1] = CreateWindowEx( NULL, // extended window style "STATIC", // pointer to registered class name szTmp, // pointer to window name WS_CHILD | WS_VISIBLE | WS_BORDER,// window style x, // horizontal position of window y, // vertical position of window h, // window width w, // window height HWND_DLG_MAIN, // handle to parent or owner window NULL, // handle to menu, or child-win
I guess thats the only thing you are doing is deleting it at a wrong time, what i suggest it you initialize the array in constructor of the application and delete it in destructor. or if you wanna go like this then too i can't understand this bit.. DestroyWindow(StaticArr[loopctr]); //delete [] StaticArr; EndDialog(HWND_DLG_MAIN, 0); DestroyWindow(hWnd); why don't you just delete array and enddialog() then destroywindow. thats all i can suggest at this time.. cheers:( Himanshu
-
I am trying to create some controls during runtime. I resolved all my errors but I think it now has a memory leak. Not sure how to test that, but I don't delete array at the end. I have commented out once place I was trying to delete the array in the WM_CLOSE section. Can anyone help me be able to delete the array and get no errors? here is the code I am using to do this. It also requires a dialog box named IDD_DIALOG1 (generic name) and a push buton IDOK. (just add a dialog into the resource editor and it should work. ---BEGIN CODE--- #include #include #include "resource.h" HWND HWND_DLG_MAIN; HINSTANCE ghInstance; //made global to attempt to reduce possible problems CString szTmp; HWND *StaticArr; HWND *temp; int cnt = 0; int x = 10; int y = 10; int h = 75; int w = 25; int loopctr = 0; BOOL MainWndProc(HWND, UINT, WPARAM, LPARAM);//Main window procedure BOOL MainOnCommand(HWND, WORD, WORD, HWND);//WM_COMMAND procedure int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR, int) { ghInstance = hInstance; DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)MainWndProc, 0);//Create the main dialog box return FALSE; } BOOL MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch(uMsg) { case WM_INITDIALOG: HWND_DLG_MAIN = hWnd; break; case WM_COMMAND: MainOnCommand(HWND_DLG_MAIN, LOWORD(wParam), HIWORD(wParam), (HWND)lParam); break; case WM_CLOSE: for(loopctr = 0; loopctr < cnt; loopctr++) DestroyWindow(StaticArr[loopctr]); //delete [] StaticArr; EndDialog(HWND_DLG_MAIN, 0); DestroyWindow(hWnd); break; } return FALSE; } BOOL MainOnCommand(HWND hWnd, WORD wCommand, WORD wNotify, HWND hControl) { switch(wCommand) { case IDOK: if(cnt > 1) { temp = new HWND[cnt]; } else { temp = new HWND[1]; } temp = StaticArr; if(cnt > 1) { delete [] StaticArr; } StaticArr = new HWND[++cnt]; delete [] temp; szTmp.Format("Label-%d", cnt); StaticArr[cnt-1] = CreateWindowEx( NULL, // extended window style "STATIC", // pointer to registered class name szTmp, // pointer to window name WS_CHILD | WS_VISIBLE | WS_BORDER,// window style x, // horizontal position of window y, // vertical position of window h, // window width w, // window height HWND_DLG_MAIN, // handle to parent or owner window NULL, // handle to menu, or child-win
I found the answer. :) I was setting the pointers equal and when I deleted I freed the chunk of memory both were pointing to, and when I created new it created a new chunk. Took a few to get out of sync. I just had to add in a for loop and it works all better now. Thanks for anyway working to try and get me an answer. I changed the IF statement structure section to this ---BEG CODE--- if(cnt < 1) { StaticArr = new HWND[++cnt]; } else if(cnt > 0) { temp = new HWND[cnt]; for(loopctr=0; loopctr < cnt; loopctr++) { temp[loopctr] = StaticArr[loopctr]; } delete [] StaticArr; StaticArr = new HWND[++cnt]; for(loopctr=0; loopctr < cnt; loopctr++) { StaticArr[loopctr] = temp[loopctr]; } delete [] temp; } ---END CODE--- The wisest of the wise may err. - Aeschylus