WM_SetUserData() destroys Window Id!!

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

  • WM_SetUserData() destroys Window Id!!

    I'm trying to create an IMAGE widget with extra user data bytes through IMAGE_CreateUser().

    After calling the function, I check the Window ID with WM_GetId() and it's ok.

    Next I call WM_SetUserData() and check again the Id with WM_GetId(). This time, the Id is wrong!

    If I set the correct Id with WM_SetId(), I destroy user data!
  • Source Code

    1. void HMI_init(void) {
    2. GUI_Init();
    3. WM_SetSize(WM_HBKWIN, LCD_GetXSize(), LCD_GetYSize());
    4. int id;
    5. IMAGE_Handle hImg;
    6. struct UserData { int x; int y; int z; } data;
    7. hImg = IMAGE_CreateUser(0, 0, 100, 100, WM_HBKWIN, WM_CF_SHOW, 0, GUI_ID_IMAGE0, sizeof(data));
    8. id = WM_GetId(hImg);
    9. assert(id == GUI_ID_IMAGE0);
    10. WM_SetUserData(hImg, &data, sizeof(data));
    11. id = WM_GetId(hImg);
    12. assert(id == GUI_ID_IMAGE0);
    13. }
    Display All


    The second assert is raised on my side.
  • Hello,

    Please note that you should use <WIDGET>_SetUserData() for Widgets.
    Because a Widget could used more memory than a Window Manager Object.

    Unfortunately, the "IMAGE" widget does not have a function to set and get the UserData,
    but we will implement it in the next emWin release.

    Temporary you could use the following code snippet for using the UserData with the "IMAGE" - Widget:

    C Source Code

    1. //
    2. // Include the IMAGE_Private.h to be able to access the IMAGE_OBJ structure.
    3. //
    4. #include "IMAGE_Private.h"
    5. struct UserData {
    6. int x;
    7. int y;
    8. int z;
    9. };
    10. struct UserData Data = { 1, 2, 3 };
    11. struct UserData DataNew;
    12. /*********************************************************************
    13. *
    14. * MainTask
    15. *
    16. */
    17. void MainTask(void) {
    18. IMAGE_Handle hImg;
    19. int Id;
    20. GUI_Init();
    21. hImg = IMAGE_CreateUser(0, 0, 100, 100, WM_HBKWIN, WM_CF_SHOW, 0, GUI_ID_IMAGE0, sizeof(Data));
    22. Id = WM_GetId(hImg);
    23. //
    24. // Temporarily use this 2 functions to set and get UserData
    25. //
    26. WM__SetUserDataEx(hImg, &Data, sizeof(Data), sizeof(IMAGE_OBJ));
    27. WM__GetUserDataEx(hImg, &DataNew, sizeof(Data), sizeof(IMAGE_OBJ));
    28. Id = WM_GetId(hImg);
    29. }
    Display All


    Best regards,
    Thorsten
  • Please note that you should use _SetUserData() for Widgets.
    Because a Widget could used more memory than a Window Manager Object.
    Ok, I got it.
    Temporary you could use the following code snippet for using the UserData with the "IMAGE" - Widget:
    It doesn't work on my PC for simulation project. I receive "Wrong handle type" error when I try to use IMAGE_SetBitmap() after calling WM__SetUserDataEx() as you described. If I remove WM__SetUserDataEx() everything works well.

    sizeof(IMAGE_OBJ) is 136, but by calling GUI_ALLOC_GetNumUsedBytes() before and after calling IMAGE_CreateEx(), I calculated an allocation of 152 bytes. Are these additional 16 bytes used by the allocation library or the IMAGE_OBJ structure is really 16 bytes greater than sizeof(IMAGE_OBJ)? Why is there this discrepancy?

    I modified your code in the following way:

    Source Code

    1. void IMAGE_SetUserData(WM_HWIN hImg, void *udata, size_t size) {
    2. WM__SetUserDataEx(hImg, udata, size, sizeof(IMAGE_OBJ) + 16);
    3. }
    4. void IMAGE_GetUserData(WM_HWIN hImg, void *udata, size_t size) {
    5. WM__GetUserDataEx(hImg, udata, size, sizeof(IMAGE_OBJ) + 16);
    6. }

    This trick seems do the job, but I'm not sure it's a correct solution for the problem. I am worried I'm writing on unused memory blocks.
  • Hello,

    Unfortunately, I am not able to reproduce this behavior.
    Please note that the function <Widget>_CreateUser() should be used to be able to add user data to a widget.
    The additional 12 bytes describe overhead used by the emWin memory management for the particular memory block allocated for the according widget.

    Could you please send me the code which shows the behavior you described?

    Best regards,
    Thorsten
  • Could you please send me the code which shows the behavior you described?

    C Source Code

    1. GUI_Init();
    2. struct { int x, y, z; } data;
    3. IMAGE_Handle hImg;
    4. hImg = IMAGE_CreateUser(0, 0, 0, 0, WM_HBKWIN, WM_CF_SHOW, IMAGE_CF_AUTOSIZE, 0, sizeof(data));
    5. WM__SetUserDataEx(hImg, &data, sizeof(data), sizeof(IMAGE_OBJ));
    6. IMAGE_SetBitmap(hImg, &bmsettings);


    After calling IMAGE_SetBitmap, I receive the popup message: IMAGE.c, Wrong handle type or Object not init'ed in IMAGE__LockH()

    If I comment WM__SetUserDataEx(), the error disappears.
  • I am able to reproduce this behavior in emWin version 5.26.

    This problem is fixed in the current emWin version 5.28.



    Please contact your provider to get the latest emWin version.
    It is a very bad news. I don't know when NXP will release the latest 5.28 version of emWin.

    In the meantime, could you suggest a workaround? I don't know what is the cause of the bug, maybe it is sufficient to add some offset to WM__SetUserDataEx()/WM__GetUserDataEx() or other similar things.

    For example:

    C Source Code

    1. void IMAGE_SetUserData(WM_HWIN hImg, void *udata, size_t size) {
    2. WM__SetUserDataEx(hImg, udata, size, sizeof(IMAGE_OBJ) + 16);
    3. }
    4. void IMAGE_GetUserData(WM_HWIN hImg, void *udata, size_t size) {
    5. WM__GetUserDataEx(hImg, udata, size, sizeof(IMAGE_OBJ) + 16);
    6. }