ERROR : Dereferencing free block in GUI_ALLOC_h2p().

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

    • ERROR : Dereferencing free block in GUI_ALLOC_h2p().

      Hi,

      My question is as stated in the title is there a difference between calling WM_DeleteWindow or sending the message WM_DELETE via WM_SendMessageNoPara ? Because when i call GUI_EndDialog(hWin, 0) (which according to reference guide : "In case a non-blocking dialog is closed, this function works the same way the function WM_DeleteWindow() works") in the initialization function just after receiving WM_INIT_DIALOG, sometimes the window don't get deleted and i don't receive WM_DELETE message. But in the other hand, when calling WM_SendMessageNoPara(hWin, WM_DELETE) i always get the WM_DELETE message.


      Another thing when using WM_SendMessageNoPara(hWin, WM_DELETE) does it free the memory dynamically allocated by the window as WM_DeleteWindow does or not ?

      I hope i have made myself clear and if you need any clarification feel free to ask.
    • Hi,

      A WM_DeleteWindow() does not only sends a message to the window as WM_SendMessageNoPara() does. WM_DeleteWindow() takes care of deleting all the child windows and frees the memory used by these window/widgets.

      Unfortunately, GUI_EndDialog() does not send send a message to the given window. If you don't need the return value passed along with GUI_EndDialog() you can use WM_DeleteWindow().

      If you want to react on WM_DELETE and use the function GUI_EndDialog() send the WM_DELETE message before calling GUI_EndDialog().

      Regards,

      Sven
      Please read the forum rules before posting.

      Keep in mind, this is *not* a support forum.
      Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
      Should you be entitled to support you can contact us via our support system: segger.com/ticket/

      Or you can contact us via e-mail.
    • Hi,

      I tried to debug my problem and when calling WM_DeleteWindow() during the initialization function of a dialog, I get the following error :

      GUI_Alloc.c:Dereferencing free block in GUI_ALLOC_h2p()

      So how can I solve this problem ? Deleting Window during dialog initialization, is it possible ? is there a message that indicates the end of the initialization ?

      With the following code, you can reproduce the error by pressing ENTER.

      C Source Code

      1. #include "DIALOG.h"
      2. /*********************************************************************
      3. *
      4. * Defines
      5. *
      6. **********************************************************************
      7. */
      8. #define RECOMMENDED_MEMORY (1024L * 5)
      9. #define ID_WINDOW_0 (GUI_ID_USER + 0x00)
      10. #define ID_WINDOW_1 (GUI_ID_USER + 0x01)
      11. int oldTime;
      12. WM_HWIN dialog;
      13. static void _cbDialog1(WM_MESSAGE * pMsg);
      14. static void _cbDialog2(WM_MESSAGE * pMsg);
      15. /*********************************************************************
      16. *
      17. * Static data
      18. *
      19. **********************************************************************
      20. */
      21. /*********************************************************************
      22. *
      23. * _aDialogCreate
      24. */
      25. static const GUI_WIDGET_CREATE_INFO _aDateDialog1[] = {
      26. { WINDOW_CreateIndirect, "Window1", ID_WINDOW_0, 0, 0, 240, 320 }
      27. };
      28. static const GUI_WIDGET_CREATE_INFO _aDateDialog2[] = {
      29. { WINDOW_CreateIndirect, "Window2", ID_WINDOW_1, 0, 0, 240, 320 }
      30. };
      31. /*********************************************************************
      32. *
      33. * Static code
      34. *
      35. **********************************************************************
      36. */
      37. /*********************************************************************
      38. *
      39. * _cbFocus
      40. */
      41. static void _cbFocus(WM_MESSAGE * pMsg) {
      42. //WM_KEY_INFO * pKeyInfo;
      43. //WM_HWIN hItem;
      44. switch (pMsg->MsgId) {
      45. //
      46. // This is quite important. Without this callback the dialog wouldn't
      47. // accept a focus and therefore no key input.
      48. //
      49. case WM_SET_FOCUS:
      50. //
      51. // Tell the window manager that we accept the focus by setting pMsg->Data.v to 0.
      52. //
      53. if (pMsg->Data.v) {
      54. pMsg->Data.v = 0;
      55. }
      56. break;
      57. default:
      58. WINDOW_Callback(pMsg);
      59. break;
      60. }
      61. }
      62. /*********************************************************************
      63. *
      64. * _cbDialog1
      65. */
      66. static void _cbDialog1(WM_MESSAGE * pMsg) {
      67. switch (pMsg->MsgId) {
      68. case WM_INIT_DIALOG:
      69. //
      70. // Set a custom callback for this dialog, sounds strange but is
      71. // necessary for key input. In general a dialog can't get the focus
      72. // and therefore wouldn't receive any key messages. So we have to
      73. // overwrite it's callback.
      74. //
      75. WM_SetCallback(pMsg->hWin, _cbFocus); //comment for the other method
      76. WINDOW_SetBkColor(pMsg->hWin, GUI_RED);
      77. break;
      78. case WM_DELETE:
      79. dialog = GUI_CreateDialogBox(_aDateDialog2, GUI_COUNTOF(_aDateDialog2), _cbDialog2, WM_HBKWIN, 0, 0);
      80. //
      81. // Set focus on the main window
      82. //
      83. WM_SetFocus(dialog);
      84. break;
      85. case WM_KEY:
      86. if (SIM_GUI_GetTime() - oldTime > 250) {
      87. oldTime = SIM_GUI_GetTime();
      88. switch (((WM_KEY_INFO*)(pMsg->Data.p))->Key) {
      89. case GUI_KEY_ENTER:
      90. GUI_EndDialog(pMsg->hWin, 0);
      91. break;
      92. case GUI_KEY_UP:
      93. break;
      94. case GUI_KEY_DOWN:
      95. break;
      96. default:
      97. break;
      98. }
      99. }
      100. break;
      101. default:
      102. WM_DefaultProc(pMsg);
      103. break;
      104. }
      105. }
      106. /*********************************************************************
      107. *
      108. * _cbDialog2
      109. */
      110. static void _cbDialog2(WM_MESSAGE * pMsg) {
      111. switch (pMsg->MsgId) {
      112. case WM_INIT_DIALOG:
      113. //
      114. // Set a custom callback for this dialog, sounds strange but is
      115. // necessary for key input. In general a dialog can't get the focus
      116. // and therefore wouldn't receive any key messages. So we have to
      117. // overwrite it's callback.
      118. //
      119. WM_SetCallback(pMsg->hWin, _cbFocus); //comment for the other method
      120. WINDOW_SetBkColor(pMsg->hWin, GUI_GREEN);
      121. // When calling GUI_EndDialog in dialog intiliazation procedure nothing happens, if it's get called from key event it works fine, why?
      122. //GUI_EndDialog(pMsg->hWin, 0);
      123. // When calling WM_DeleteWindow, i get the following error "Dereferencing free block in GUI_ALLOC_h2p()"
      124. WM_DeleteWindow(pMsg->hWin);
      125. break;
      126. case WM_DELETE:
      127. dialog = GUI_CreateDialogBox(_aDateDialog1, GUI_COUNTOF(_aDateDialog1), _cbDialog1, WM_HBKWIN, 0, 0);
      128. //
      129. // Set focus on the main window
      130. //
      131. WM_SetFocus(dialog);
      132. break;
      133. case WM_KEY:
      134. if (SIM_GUI_GetTime() - oldTime > 250) {
      135. oldTime = SIM_GUI_GetTime();
      136. switch (((WM_KEY_INFO*)(pMsg->Data.p))->Key) {
      137. case GUI_KEY_ENTER:
      138. GUI_EndDialog(pMsg->hWin, 0);
      139. break;
      140. case GUI_KEY_UP:
      141. break;
      142. case GUI_KEY_DOWN:
      143. break;
      144. default:
      145. break;
      146. }
      147. }
      148. break;
      149. default:
      150. WM_DefaultProc(pMsg);
      151. break;
      152. }
      153. }
      154. /*********************************************************************
      155. *
      156. * Public code
      157. *
      158. **********************************************************************
      159. */
      160. /*********************************************************************
      161. *
      162. * MainTask
      163. */
      164. void MainTask(void) {
      165. //
      166. // Set flag for automatic use of memory devices. Calling before GUI_Init() makes sure the desktop window uses them, too.
      167. //
      168. WM_SetCreateFlags(WM_CF_MEMDEV);
      169. //
      170. // Initialize GUI
      171. //
      172. GUI_Init();
      173. //
      174. // Check if recommended memory for the sample is available
      175. //
      176. if (GUI_ALLOC_GetNumFreeBytes() < RECOMMENDED_MEMORY) {
      177. GUI_ErrorOut("Not enough memory available.");
      178. return;
      179. }
      180. dialog = GUI_CreateDialogBox(_aDateDialog1, GUI_COUNTOF(_aDateDialog1), _cbDialog1, WM_HBKWIN, 0, 0);
      181. //
      182. // Set focus on the main window
      183. //
      184. WM_SetFocus(dialog);
      185. while (1) {
      186. GUI_Delay(100);
      187. }
      188. }
      Display All
    • Hi,

      The difference is that WM_DeleteWindow() deletes the given window and all of its child windows immediately. The problem is that while the program is in WM_INIT_DIALOG the handles of the child windows are getting messed up and it crashes.

      With GUI_EndDialog() the deletion gets delayed and processed after the intialization precess has been completed.

      But why do you wan't to delete a window/dialog in its initialization right after it has been created?

      Sven
      Please read the forum rules before posting.

      Keep in mind, this is *not* a support forum.
      Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
      Should you be entitled to support you can contact us via our support system: segger.com/ticket/

      Or you can contact us via e-mail.
    • Thanks for the explication.

      But when using GUI_EndDialog(), the deletion never get done. You can check that nothing is done by running the previous code and replacing WM_DeleteWindow() by GUI_EndDialog().

      Concerning why I want to delete a window/dialog in its initialization right after it has been created :

      In the application I am creating, during the initialization, i call multiple functions to get the values i want to display or to activate other tasks... but these functions could return an error and in that case i want to switch to an error screen instantly that's why i want to delete the dialog directly after its creation.


      So, have you an idea how can i achieve that ?
    • Hi,

      If I run your example and exchange the WM_DeleteWindow() with a GUI_EndDialog() it working perfectly (at least I guess, its changing between a red and a green window).

      Which emWin version are you running?
      Did you tried a newer/older one?

      Try the simulation, does it work here?
      It can be downloaded here:
      segger.com/downloads/emwin/

      Regards,
      Sven
      Please read the forum rules before posting.

      Keep in mind, this is *not* a support forum.
      Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
      Should you be entitled to support you can contact us via our support system: segger.com/ticket/

      Or you can contact us via e-mail.
    • Hi again,

      I tested my code on 3 versions (v5.40-v5.46-v5.48), I'm still having the same problem.
      Maybe i am missing something or i didn't explain my problem clearly , so I will try explain it better.

      When I'm in the first screen (the red one) and i press "enter" the app switch to the second screen (the green one) and stays there.
      But in the second screen initialization, i called GUI_EndDialog() so the normal behavior should be (I presume) : the green screen gets deleted and the app switch back to the first one (the red screen) automatically. But that never happens.


      Really thank your for your patience. I'm looking forward for your reply.

      My problem is located on the _cbDialog2 callback function of the attached code.

      C Source Code

      1. #include "DIALOG.h"
      2. /*********************************************************************
      3. *
      4. * Defines
      5. *
      6. **********************************************************************
      7. */
      8. #define RECOMMENDED_MEMORY (1024L * 5)
      9. #define ID_WINDOW_0 (GUI_ID_USER + 0x00)
      10. #define ID_WINDOW_1 (GUI_ID_USER + 0x01)
      11. int oldTime;
      12. WM_HWIN dialog;
      13. static void _cbDialog1(WM_MESSAGE * pMsg);
      14. static void _cbDialog2(WM_MESSAGE * pMsg);
      15. /*********************************************************************
      16. *
      17. * Static data
      18. *
      19. **********************************************************************
      20. */
      21. /*********************************************************************
      22. *
      23. * _aDialogCreate
      24. */
      25. static const GUI_WIDGET_CREATE_INFO _aDateDialog1[] = {
      26. { WINDOW_CreateIndirect, "Window1", ID_WINDOW_0, 0, 0, 240, 320 }
      27. };
      28. static const GUI_WIDGET_CREATE_INFO _aDateDialog2[] = {
      29. { WINDOW_CreateIndirect, "Window2", ID_WINDOW_1, 0, 0, 240, 320 }
      30. };
      31. /*********************************************************************
      32. *
      33. * Static code
      34. *
      35. **********************************************************************
      36. */
      37. /*********************************************************************
      38. *
      39. * _cbFocus
      40. */
      41. static void _cbFocus(WM_MESSAGE * pMsg) {
      42. //WM_KEY_INFO * pKeyInfo;
      43. //WM_HWIN hItem;
      44. switch (pMsg->MsgId) {
      45. //
      46. // This is quite important. Without this callback the dialog wouldn't
      47. // accept a focus and therefore no key input.
      48. //
      49. case WM_SET_FOCUS:
      50. //
      51. // Tell the window manager that we accept the focus by setting pMsg->Data.v to 0.
      52. //
      53. if (pMsg->Data.v) {
      54. pMsg->Data.v = 0;
      55. }
      56. break;
      57. default:
      58. WINDOW_Callback(pMsg);
      59. break;
      60. }
      61. }
      62. /*********************************************************************
      63. *
      64. * _cbDialog1
      65. */
      66. static void _cbDialog1(WM_MESSAGE * pMsg) {
      67. switch (pMsg->MsgId) {
      68. case WM_INIT_DIALOG:
      69. //
      70. // Set a custom callback for this dialog, sounds strange but is
      71. // necessary for key input. In general a dialog can't get the focus
      72. // and therefore wouldn't receive any key messages. So we have to
      73. // overwrite it's callback.
      74. //
      75. WM_SetCallback(pMsg->hWin, _cbFocus); //comment for the other method
      76. WINDOW_SetBkColor(pMsg->hWin, GUI_RED);
      77. break;
      78. case WM_DELETE:
      79. dialog = GUI_CreateDialogBox(_aDateDialog2, GUI_COUNTOF(_aDateDialog2), _cbDialog2, WM_HBKWIN, 0, 0);
      80. //
      81. // Set focus on the main window
      82. //
      83. WM_SetFocus(dialog);
      84. break;
      85. case WM_KEY:
      86. if (SIM_GUI_GetTime() - oldTime > 250) {
      87. oldTime = SIM_GUI_GetTime();
      88. switch (((WM_KEY_INFO*)(pMsg->Data.p))->Key) {
      89. case GUI_KEY_ENTER:
      90. GUI_EndDialog(pMsg->hWin, 0);
      91. break;
      92. case GUI_KEY_UP:
      93. break;
      94. case GUI_KEY_DOWN:
      95. break;
      96. default:
      97. break;
      98. }
      99. }
      100. break;
      101. default:
      102. WM_DefaultProc(pMsg);
      103. break;
      104. }
      105. }
      106. /*********************************************************************
      107. *
      108. * _cbDialog2
      109. */
      110. static void _cbDialog2(WM_MESSAGE * pMsg) {
      111. switch (pMsg->MsgId) {
      112. case WM_INIT_DIALOG:
      113. //
      114. // Set a custom callback for this dialog, sounds strange but is
      115. // necessary for key input. In general a dialog can't get the focus
      116. // and therefore wouldn't receive any key messages. So we have to
      117. // overwrite it's callback.
      118. //
      119. WM_SetCallback(pMsg->hWin, _cbFocus); //comment for the other method
      120. WINDOW_SetBkColor(pMsg->hWin, GUI_GREEN);
      121. //***********************************************************************************************************************************//
      122. //***********************************************************************************************************************************//
      123. //***********************************************************************************************************************************//
      124. //***********************************************************************************************************************************//
      125. //***********************************************************************************************************************************//
      126. // When calling GUI_EndDialog in dialog intiliazation procedure nothing happens, why?
      127. GUI_EndDialog(pMsg->hWin, 0);
      128. // The wanted behaviour is that the window gets deleted instantly
      129. //***********************************************************************************************************************************//
      130. //***********************************************************************************************************************************//
      131. //***********************************************************************************************************************************//
      132. //***********************************************************************************************************************************//
      133. //***********************************************************************************************************************************//
      134. //***********************************************************************************************************************************//
      135. break;
      136. case WM_DELETE:
      137. dialog = GUI_CreateDialogBox(_aDateDialog1, GUI_COUNTOF(_aDateDialog1), _cbDialog1, WM_HBKWIN, 0, 0);
      138. //
      139. // Set focus on the main window
      140. //
      141. WM_SetFocus(dialog);
      142. break;
      143. default:
      144. WM_DefaultProc(pMsg);
      145. break;
      146. }
      147. }
      148. /*********************************************************************
      149. *
      150. * Public code
      151. *
      152. **********************************************************************
      153. */
      154. /*********************************************************************
      155. *
      156. * MainTask
      157. */
      158. void MainTask(void) {
      159. //
      160. // Set flag for automatic use of memory devices. Calling before GUI_Init() makes sure the desktop window uses them, too.
      161. //
      162. WM_SetCreateFlags(WM_CF_MEMDEV);
      163. //
      164. // Initialize GUI
      165. //
      166. GUI_Init();
      167. //
      168. // Check if recommended memory for the sample is available
      169. //
      170. if (GUI_ALLOC_GetNumFreeBytes() < RECOMMENDED_MEMORY) {
      171. GUI_ErrorOut("Not enough memory available.");
      172. return;
      173. }
      174. dialog = GUI_CreateDialogBox(_aDateDialog1, GUI_COUNTOF(_aDateDialog1), _cbDialog1, WM_HBKWIN, 0, 0);
      175. //
      176. // Set focus on the main window
      177. //
      178. WM_SetFocus(dialog);
      179. while (1) {
      180. GUI_Delay(100);
      181. }
      182. }
      Display All
    • Hi,

      if I get you right you will later on check if something went wrong and you will delete the window immediately on failure.

      How about to not delete the dialog in its init sequence and delete it right after GUI_CreateDialogBox() returns. Maybe like this (in _cbDialog1):

      C Source Code

      1. case WM_DELETE:
      2. dialog = GUI_CreateDialogBox(_aDateDialog2, GUI_COUNTOF(_aDateDialog2), _cbDialog2, WM_HBKWIN, 0, 0);
      3. WM_DeleteWindow(dialog);
      4. //
      5. // Set focus on the main window
      6. //
      7. WM_SetFocus(dialog);
      8. break;
      Regards,
      Sven
      Please read the forum rules before posting.

      Keep in mind, this is *not* a support forum.
      Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
      Should you be entitled to support you can contact us via our support system: segger.com/ticket/

      Or you can contact us via e-mail.