Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Creating controls during runtime

Creating controls during runtime

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresperformancehelptutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Baatezu
    wrote on last edited by
    #1

    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

    X B 2 Replies Last reply
    0
    • B Baatezu

      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

      X Offline
      X Offline
      xxhimanshu
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • B Baatezu

        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

        B Offline
        B Offline
        Baatezu
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups