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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Dynamically allocating static variables

Dynamically allocating static variables

Scheduled Pinned Locked Moved C / C++ / MFC
lounge
5 Posts 3 Posters 1 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.
  • C Offline
    C Offline
    capricious_001
    wrote on last edited by
    #1

    Hey there, I'm building a gui that has multiple tabs, and what I had done was set the hwnd variables as static variables such as below: BOOL CALLBACK OptionsDialogProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static HWND* tab1_hwnd = new HWND; static HWND* tab2_hwnd = new HWND; static HWND* tab3_hwnd = new HWND; static HWND* tab4_hwnd = new HWND; static HWND* tab5_hwnd = new HWND; static HWND* tab6_hwnd = new HWND; static HWND* CurrentTabHwnd = new HWND; static int* CurrentTabIndex = new int; static HWND* hwndTab = new HWND; switch(msg) { case WM_INITDIALOG: { LPSTR tab1_title = "General"; LPSTR tab2_title = "Player Edit"; LPSTR tab3_title = "Stats Attributes"; LPSTR tab4_title = "Stats Format"; LPSTR tab5_title = "File Upload"; LPSTR tab6_title = "Advanced"; INITCOMMONCONTROLSEX tabctl; tabctl.dwSize = sizeof(tabctl); tabctl.dwICC = ICC_TAB_CLASSES; InitCommonControlsEx(&tabctl); *hwndTab = GetDlgItem(hwnd,IDC_TAB1); TCITEM ItemStruct; ItemStruct.mask = TCIF_TEXT; ItemStruct.pszText = tab1_title; ItemStruct.cchTextMax = sizeof(tab1_title); ItemStruct.iImage = 0; ItemStruct.lParam = 0; SendMessage(*hwndTab, TCM_INSERTITEM, 0, (LPARAM)&ItemStruct); ItemStruct.pszText = tab2_title; ItemStruct.cchTextMax = sizeof(tab2_title); SendMessage(*hwndTab, TCM_INSERTITEM, 1, (LPARAM)&ItemStruct); ItemStruct.pszText = tab3_title; ItemStruct.cchTextMax = sizeof(tab2_title); SendMessage(*hwndTab, TCM_INSERTITEM, 2, (LPARAM)&ItemStruct); ItemStruct.pszText = tab4_title; ItemStruct.cchTextMax = sizeof(tab2_title); SendMessage(*hwndTab, TCM_INSERTITEM, 3, (LPARAM)&ItemStruct); ItemStruct.pszText = tab5_title; ItemStruct.cchTextMax = sizeof(tab2_title); SendMessage(*hwndTab, TCM_INSERTITEM, 4, (LPARAM)&ItemStruct); ItemStruct.pszText = tab6_title; ItemStruct.cchTextMax = sizeof(tab2_title); SendMessage(*hwndTab, TCM_INSERTITEM, 5, (LPARAM)&ItemStruct); *tab1_hwnd = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_OPTIONS_GENERAL), *hwndTab, General_OptionsDialogProc, 0); *tab2_hwnd = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_OPTIONS_PLAYEREDIT), *hwndTab, PlayerEdit_OptionsDialogProc, 0); *tab3_hwnd = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_OPTIONS_STATSATTR), *hwn

    L V 2 Replies Last reply
    0
    • C capricious_001

      Hey there, I'm building a gui that has multiple tabs, and what I had done was set the hwnd variables as static variables such as below: BOOL CALLBACK OptionsDialogProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static HWND* tab1_hwnd = new HWND; static HWND* tab2_hwnd = new HWND; static HWND* tab3_hwnd = new HWND; static HWND* tab4_hwnd = new HWND; static HWND* tab5_hwnd = new HWND; static HWND* tab6_hwnd = new HWND; static HWND* CurrentTabHwnd = new HWND; static int* CurrentTabIndex = new int; static HWND* hwndTab = new HWND; switch(msg) { case WM_INITDIALOG: { LPSTR tab1_title = "General"; LPSTR tab2_title = "Player Edit"; LPSTR tab3_title = "Stats Attributes"; LPSTR tab4_title = "Stats Format"; LPSTR tab5_title = "File Upload"; LPSTR tab6_title = "Advanced"; INITCOMMONCONTROLSEX tabctl; tabctl.dwSize = sizeof(tabctl); tabctl.dwICC = ICC_TAB_CLASSES; InitCommonControlsEx(&tabctl); *hwndTab = GetDlgItem(hwnd,IDC_TAB1); TCITEM ItemStruct; ItemStruct.mask = TCIF_TEXT; ItemStruct.pszText = tab1_title; ItemStruct.cchTextMax = sizeof(tab1_title); ItemStruct.iImage = 0; ItemStruct.lParam = 0; SendMessage(*hwndTab, TCM_INSERTITEM, 0, (LPARAM)&ItemStruct); ItemStruct.pszText = tab2_title; ItemStruct.cchTextMax = sizeof(tab2_title); SendMessage(*hwndTab, TCM_INSERTITEM, 1, (LPARAM)&ItemStruct); ItemStruct.pszText = tab3_title; ItemStruct.cchTextMax = sizeof(tab2_title); SendMessage(*hwndTab, TCM_INSERTITEM, 2, (LPARAM)&ItemStruct); ItemStruct.pszText = tab4_title; ItemStruct.cchTextMax = sizeof(tab2_title); SendMessage(*hwndTab, TCM_INSERTITEM, 3, (LPARAM)&ItemStruct); ItemStruct.pszText = tab5_title; ItemStruct.cchTextMax = sizeof(tab2_title); SendMessage(*hwndTab, TCM_INSERTITEM, 4, (LPARAM)&ItemStruct); ItemStruct.pszText = tab6_title; ItemStruct.cchTextMax = sizeof(tab2_title); SendMessage(*hwndTab, TCM_INSERTITEM, 5, (LPARAM)&ItemStruct); *tab1_hwnd = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_OPTIONS_GENERAL), *hwndTab, General_OptionsDialogProc, 0); *tab2_hwnd = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_OPTIONS_PLAYEREDIT), *hwndTab, PlayerEdit_OptionsDialogProc, 0); *tab3_hwnd = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_OPTIONS_STATSATTR), *hwn

      L Offline
      L Offline
      Laxman Auti
      wrote on last edited by
      #2

      wrote:

      EndDialog(hwnd, 0); delete tab1_hwnd; delete tab2_hwnd; delete tab3_hwnd; delete tab4_hwnd; delete tab5_hwnd; delete tab6_hwnd; delete CurrentTabHwnd; delete CurrentTabIndex; delete hwndTab;

      Instead of this try to call the EndDialog(hwnd, 0); after deleting all the tabs.it may resolve your problem :~ Knock out 't' from can't, You can if you think you can :cool:

      C 1 Reply Last reply
      0
      • L Laxman Auti

        wrote:

        EndDialog(hwnd, 0); delete tab1_hwnd; delete tab2_hwnd; delete tab3_hwnd; delete tab4_hwnd; delete tab5_hwnd; delete tab6_hwnd; delete CurrentTabHwnd; delete CurrentTabIndex; delete hwndTab;

        Instead of this try to call the EndDialog(hwnd, 0); after deleting all the tabs.it may resolve your problem :~ Knock out 't' from can't, You can if you think you can :cool:

        C Offline
        C Offline
        capricious_001
        wrote on last edited by
        #3

        Hey man, lol thanks for the fix, but I ran into a BIG problem. When I ran the program and closed the dialog box, I got my 8th (i think?) ever BSoD and some very weird exorcist sounds coming from my speakers. So dont think that worked out too well. Any other ides you may have? Robbie

        1 Reply Last reply
        0
        • C capricious_001

          Hey there, I'm building a gui that has multiple tabs, and what I had done was set the hwnd variables as static variables such as below: BOOL CALLBACK OptionsDialogProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static HWND* tab1_hwnd = new HWND; static HWND* tab2_hwnd = new HWND; static HWND* tab3_hwnd = new HWND; static HWND* tab4_hwnd = new HWND; static HWND* tab5_hwnd = new HWND; static HWND* tab6_hwnd = new HWND; static HWND* CurrentTabHwnd = new HWND; static int* CurrentTabIndex = new int; static HWND* hwndTab = new HWND; switch(msg) { case WM_INITDIALOG: { LPSTR tab1_title = "General"; LPSTR tab2_title = "Player Edit"; LPSTR tab3_title = "Stats Attributes"; LPSTR tab4_title = "Stats Format"; LPSTR tab5_title = "File Upload"; LPSTR tab6_title = "Advanced"; INITCOMMONCONTROLSEX tabctl; tabctl.dwSize = sizeof(tabctl); tabctl.dwICC = ICC_TAB_CLASSES; InitCommonControlsEx(&tabctl); *hwndTab = GetDlgItem(hwnd,IDC_TAB1); TCITEM ItemStruct; ItemStruct.mask = TCIF_TEXT; ItemStruct.pszText = tab1_title; ItemStruct.cchTextMax = sizeof(tab1_title); ItemStruct.iImage = 0; ItemStruct.lParam = 0; SendMessage(*hwndTab, TCM_INSERTITEM, 0, (LPARAM)&ItemStruct); ItemStruct.pszText = tab2_title; ItemStruct.cchTextMax = sizeof(tab2_title); SendMessage(*hwndTab, TCM_INSERTITEM, 1, (LPARAM)&ItemStruct); ItemStruct.pszText = tab3_title; ItemStruct.cchTextMax = sizeof(tab2_title); SendMessage(*hwndTab, TCM_INSERTITEM, 2, (LPARAM)&ItemStruct); ItemStruct.pszText = tab4_title; ItemStruct.cchTextMax = sizeof(tab2_title); SendMessage(*hwndTab, TCM_INSERTITEM, 3, (LPARAM)&ItemStruct); ItemStruct.pszText = tab5_title; ItemStruct.cchTextMax = sizeof(tab2_title); SendMessage(*hwndTab, TCM_INSERTITEM, 4, (LPARAM)&ItemStruct); ItemStruct.pszText = tab6_title; ItemStruct.cchTextMax = sizeof(tab2_title); SendMessage(*hwndTab, TCM_INSERTITEM, 5, (LPARAM)&ItemStruct); *tab1_hwnd = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_OPTIONS_GENERAL), *hwndTab, General_OptionsDialogProc, 0); *tab2_hwnd = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_OPTIONS_PLAYEREDIT), *hwndTab, PlayerEdit_OptionsDialogProc, 0); *tab3_hwnd = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_OPTIONS_STATSATTR), *hwn

          V Offline
          V Offline
          Viorel
          wrote on last edited by
          #4

          I think the problem is because of multiple delete operations performed on the same pointer. You probably believe that a line like

          static HWND* tab1_hwnd = new HWND;
          

          within a function allocates the object on each execution of that function. This is not true: the pointer is allocated once. You can change it like this:

          static HWND* tab1_hwnd = NULL;
          ...
          case WM_INITDIALOG:
              tab1_hwnd = new HWND;
              . . .
          
          case WM_CLOSE:
              ::DestroyWindow(*tab1_hwnd);
              delete tab1_hwnd;
              tab1_hwnd = NULL;
              . . .
          

          Actually HWND is not so a big object. It takes only 4 bytes. Therefore you can work without pointers:

          static HWND tab1_hwnd = NULL;
          
          C 1 Reply Last reply
          0
          • V Viorel

            I think the problem is because of multiple delete operations performed on the same pointer. You probably believe that a line like

            static HWND* tab1_hwnd = new HWND;
            

            within a function allocates the object on each execution of that function. This is not true: the pointer is allocated once. You can change it like this:

            static HWND* tab1_hwnd = NULL;
            ...
            case WM_INITDIALOG:
                tab1_hwnd = new HWND;
                . . .
            
            case WM_CLOSE:
                ::DestroyWindow(*tab1_hwnd);
                delete tab1_hwnd;
                tab1_hwnd = NULL;
                . . .
            

            Actually HWND is not so a big object. It takes only 4 bytes. Therefore you can work without pointers:

            static HWND tab1_hwnd = NULL;
            
            C Offline
            C Offline
            capricious_001
            wrote on last edited by
            #5

            Ok thats cool, I'll just take out the pointers since its easier that way. Thanks for your help guys.

            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