GUI_alloc errors in Simulator

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • GUI_alloc errors in Simulator

      Using the code below and clicking the Home button which performs GUI_EndDialog(pMsg->hWin, 0);
      we will get MAX_GRAPHS (3) of these
      "GUI_alloc.c: block to be deleted already locked in _Free()" errors
      and 1
      "GUI_alloc.c: GUI_ALLOC_h2p: illegal argument (0 handle) in GUI_ALLOC_h2p()." error.

      The question is, what is the best practice for cleaning up a graph window?

      Cheers,
      Joe

      Referencing threads:
      GUI_alloc erro
      GRAPH free allocated memory problem.

      C Source Code: GUI_alloc_erro_on_exit.c

      1. #include <stdlib.h>
      2. #include "DIALOG.h"
      3. #include "GRAPH.h"
      4. /*********************************************************************
      5. *
      6. * Defines
      7. *
      8. **********************************************************************
      9. */
      10. #define ID_BUTTON_2 (GUI_ID_USER + 0x05)
      11. #define ID_TEXT_0 (GUI_ID_USER + 0x23)
      12. #define ID_GRAPH_0 (GUI_ID_USER + 0x24)
      13. /*********************************************************************
      14. *
      15. * Static data
      16. *
      17. **********************************************************************
      18. */
      19. #define MAX_GRAPHS 3
      20. static GRAPH_DATA_Handle _ahData[MAX_GRAPHS]; // Array of handles for the GRAPH_DATA objects
      21. // Array of colors for the GRAPH_DATA objects
      22. static GUI_COLOR _aColor[] = {
      23. GUI_LIGHTGRAY, GUI_LIGHTGRAY, GUI_LIGHTGRAY, GUI_ORANGE,
      24. GUI_GREEN, GUI_GREEN, GUI_RED, GUI_RED,
      25. GUI_LIGHTBLUE, GUI_LIGHTBLUE
      26. };
      27. //
      28. // Dialog ressource
      29. //
      30. static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] = {
      31. { FRAMEWIN_CreateIndirect, "ChartWindow", 0, 0, 0, 780, 440, 0 },
      32. { GRAPH_CreateIndirect, 0, GUI_ID_GRAPH0, 20, 80, 740, 340 },
      33. { BUTTON_CreateIndirect, "Home", ID_BUTTON_2, 710, 10, 60, 60, 0, 0x0, 0 },
      34. };
      35. /*********************************************************************
      36. *
      37. * _AddValues
      38. *
      39. * Function description
      40. * This routine calculates new random values in dependence of the previous added values
      41. * and adds them to the GRAPH_DATA objects
      42. */
      43. static void _AddValues(void) {
      44. for (int i = 0; i < MAX_GRAPHS; i++)
      45. GRAPH_DATA_YT_AddValue(_ahData[i++], i * 30 + rand() % 30);
      46. }
      47. /*********************************************************************
      48. *
      49. * _cbCallback
      50. *
      51. * Function description
      52. * Callback function of the dialog
      53. */
      54. WM_SCROLL_STATE scrollState;
      55. static void _cbCallback(WM_MESSAGE * pMsg) {
      56. unsigned i;
      57. int NCode;
      58. int Id;
      59. WM_HWIN hItem;
      60. switch (pMsg->MsgId) {
      61. case WM_INIT_DIALOG:
      62. //
      63. // Add graphs
      64. //
      65. hItem = WM_GetDialogItem(pMsg->hWin, GUI_ID_GRAPH0);
      66. for (i = 0; i < MAX_GRAPHS; i++) {
      67. _ahData[i] = GRAPH_DATA_YT_Create(_aColor[i], 2000, 0, 0);
      68. GRAPH_AttachData(hItem, _ahData[i]);
      69. }
      70. // Initialization of 'Home'
      71. hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_2);
      72. BUTTON_SetText(hItem, "home");
      73. break;
      74. case WM_NOTIFY_PARENT:
      75. Id = WM_GetId(pMsg->hWinSrc);
      76. NCode = pMsg->Data.v;
      77. switch (NCode) {
      78. case WM_NOTIFICATION_RELEASED:
      79. switch (Id) {
      80. case ID_BUTTON_2: // Home
      81. GUI_EndDialog(pMsg->hWin, 0);
      82. break;
      83. }
      84. break;
      85. }
      86. break;
      87. default:
      88. WM_DefaultProc(pMsg);
      89. }
      90. }
      91. void MainTask(void) {
      92. GUI_Init();
      93. WM_SetDesktopColor(GUI_BLACK);
      94. WM_SetCreateFlags(WM_CF_MEMDEV);
      95. GUI_CreateDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), _cbCallback, 0, 0, 0);
      96. for (int i = 0; i < 100; i++)
      97. _AddValues();
      98. while (1) {
      99. GUI_Delay(20);
      100. _AddValues();
      101. }
      102. }
      Display All
    • Manually detaching and deleting the DATA objects appears to resolve the issue. As seen here:
      GRAPH free allocated memory problem.

      How do other folks go about doing this as they switch between screens?

      The updated code resolving the errors, for completeness:

      C Source Code: GUI_alloc_errors_resolved.c

      1. #include <stdlib.h>
      2. #include "DIALOG.h"
      3. #include "GRAPH.h"
      4. /*********************************************************************
      5. *
      6. * Defines
      7. *
      8. **********************************************************************
      9. */
      10. #define ID_BUTTON_2 (GUI_ID_USER + 0x05)
      11. #define ID_TEXT_0 (GUI_ID_USER + 0x23)
      12. #define ID_GRAPH_0 (GUI_ID_USER + 0x24)
      13. /*********************************************************************
      14. *
      15. * Static data
      16. *
      17. **********************************************************************
      18. */
      19. #define MAX_GRAPHS 3
      20. static GRAPH_DATA_Handle _ahData[MAX_GRAPHS]; // Array of handles for the GRAPH_DATA objects
      21. static unsigned stopped = 0;
      22. // Array of colors for the GRAPH_DATA objects
      23. static GUI_COLOR _aColor[] = {
      24. GUI_LIGHTGRAY, GUI_LIGHTGRAY, GUI_LIGHTGRAY, GUI_ORANGE,
      25. GUI_GREEN, GUI_GREEN, GUI_RED, GUI_RED,
      26. GUI_LIGHTBLUE, GUI_LIGHTBLUE
      27. };
      28. //
      29. // Dialog ressource
      30. //
      31. static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] = {
      32. { FRAMEWIN_CreateIndirect, "ChartWindow", 0, 0, 0, 780, 440, 0 },
      33. { GRAPH_CreateIndirect, 0, GUI_ID_GRAPH0, 20, 80, 740, 340 },
      34. { BUTTON_CreateIndirect, "Home", ID_BUTTON_2, 710, 10, 60, 60, 0, 0x0, 0 },
      35. };
      36. /*********************************************************************
      37. *
      38. * _AddValues
      39. *
      40. * Function description
      41. * This routine calculates new random values in dependence of the previous added values
      42. * and adds them to the GRAPH_DATA objects
      43. */
      44. static void _AddValues(void) {
      45. if (!stopped)
      46. for (int i = 0; i < MAX_GRAPHS; i++)
      47. GRAPH_DATA_YT_AddValue(_ahData[i++], i * 30 + rand() % 30);
      48. }
      49. /*********************************************************************
      50. *
      51. * _cbCallback
      52. *
      53. * Function description
      54. * Callback function of the dialog
      55. */
      56. WM_SCROLL_STATE scrollState;
      57. static void _cbCallback(WM_MESSAGE * pMsg) {
      58. unsigned i;
      59. int NCode;
      60. int Id;
      61. WM_HWIN hItem;
      62. switch (pMsg->MsgId) {
      63. case WM_INIT_DIALOG:
      64. //
      65. // Add graphs
      66. //
      67. hItem = WM_GetDialogItem(pMsg->hWin, GUI_ID_GRAPH0);
      68. for (i = 0; i < MAX_GRAPHS; i++) {
      69. _ahData[i] = GRAPH_DATA_YT_Create(_aColor[i], 2000, 0, 0);
      70. GRAPH_AttachData(hItem, _ahData[i]);
      71. }
      72. // Initialization of 'Home'
      73. hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_2);
      74. BUTTON_SetText(hItem, "home");
      75. break;
      76. case WM_NOTIFY_PARENT:
      77. Id = WM_GetId(pMsg->hWinSrc);
      78. NCode = pMsg->Data.v;
      79. switch (NCode) {
      80. case WM_NOTIFICATION_RELEASED:
      81. switch (Id) {
      82. case ID_BUTTON_2: // Home
      83. stopped = 1;
      84. hItem = WM_GetDialogItem(pMsg->hWin, GUI_ID_GRAPH0);
      85. for (i = 0; i < MAX_GRAPHS; i++) {
      86. GRAPH_DetachData(hItem, _ahData[i]);
      87. GRAPH_DATA_XY_Delete(_ahData[i]);
      88. }
      89. GUI_EndDialog(pMsg->hWin, 0);
      90. break;
      91. }
      92. break;
      93. }
      94. break;
      95. default:
      96. WM_DefaultProc(pMsg);
      97. }
      98. }
      99. void MainTask(void) {
      100. GUI_Init();
      101. WM_SetDesktopColor(GUI_BLACK);
      102. WM_SetCreateFlags(WM_CF_MEMDEV);
      103. GUI_CreateDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), _cbCallback, 0, 0, 0);
      104. for (int i = 0; i < 100; i++)
      105. _AddValues();
      106. while (1) {
      107. GUI_Delay(20);
      108. _AddValues();
      109. }
      110. }
      Display All
    • Hi,

      the problem is that you delete the graph while calling GUI_EndDialog() and try to access the data item attached to it.

      Try something like this:

      Add a static variable to remember the graph handle and check in _AddValues() if it is still valid.

      C Source Code

      1. static WM_HWIN _hGraph;
      2. static void _AddValues(void) {
      3. int i;
      4. if (WM_IsWindow(_hGraph)) {
      5. for (i = 0; i < MAX_GRAPHS; i++)
      6. GRAPH_DATA_YT_AddValue(_ahData[i++], i * 30 + rand() % 30);
      7. }
      8. }

      In the dialog callback simply initialize _hGraph.

      C Source Code

      1. _hGraph = hItem = WM_GetDialogItem(pMsg->hWin, GUI_ID_GRAPH0);

      At least on my end it solved the issue.

      Regards,
      Sven