[WinCE] Sometimes an access violation or a bus error occurs when creating a form with a menu
Original Reporter info from Mantis: PascalDragon @PascalDragon
-
Reporter name: Sven Barth
Original Reporter info from Mantis: PascalDragon @PascalDragon
- Reporter name: Sven Barth
Description:
I have a form that I show as a modal dialog which contains a menu. When this form is shown rather often then sooner or later an access violation or bus error occurs.
This exception is located in WinCEWSMenus.CeSetMenu when accessing LCLMenu.Items as one can see in the following backtrace:
MENUS_TMENUITEM___GETCOUNT
$LONGINT + 24 in section .text
WINCEWSMENUS_CESETMENULONGWORD
LONGWORD$TMENU + 516 in section .text
WINCEINT_TWINCEWIDGETSET___SETMENU
LONGWORDLONGWORD
$BOOLEAN + 208 in section .text
LCLINTF_SETMENULONGWORD
LONGWORD$$BOOLEAN + 64 in section .text
MENUS_TMAINMENU___SETWINDOWHANDLE
LONGWORD + 104 in section .text
FORMS_TCUSTOMFORM_$__UPDATEMENU + 192 in section .text
FORMS_TCUSTOMFORM_$__CREATEWND + 72 in section .text
FORMS_TFORM_$__CREATEWND + 64 in section .text
CONTROLS_TWINCONTROL_$__CREATEHANDLE + 64 in section .text
CONTROLS_TWINCONTROL_$__HANDLENEEDED + 184 in section .text
CONTROLS_TWINCONTROL__DOALLAUTOSIZE_CHECKHANDLEALLOCATED
TWINCONTROL$$BOOLEAN
+ 88 in section .text
CONTROLS_TWINCONTROL_$__DOALLAUTOSIZE + 192 in section .text
CONTROLS_TCONTROL_$__ENABLEAUTOSIZING + 312 in section .text
CONTROLS_TCONTROL___SETVISIBLE
BOOLEAN + 476 in section .text
FORMS_TCUSTOMFORM___SETVISIBLE
BOOLEAN + 184 in section .text
FORMS_TCUSTOMFORM_$__SHOW + 48 in section .text
FORMS_TCUSTOMFORM___SHOWMODAL
$LONGINT + 616 in section .text
Additional information:
The problem seems to be the following:
In TWinCEWidgetSet.SetMenu (located in wincewinapi.inc) the menu handle is searched in a list MenuHandleList and then the returned index is used to obtain the corresponding TMenu object. These two lists are filled in WinCEWSMenus.TWinCEWSMenu.CreateHandle, but once an entry is added it isn't removed anywhere. So when the handle is freed in WinCEWSMenus.TWinCEWSMenuItem.DestroyHandle (the handle of the top menu item is the handle of the menu) Windows is free to use the handle value again (which it does sometimes). So then another entry with this handle is added, but the search in SetMenu will stop at the first (old) entry and return the now invalid object.
Attached is
- an example application showing the problem (just hit the "Open" and the "Close" button often enough)
- a patch which solves the problem by deleting the entries in WinCEWSMenus.TWinCEWSMenuITem.DestroyHandle (thus also the two lists keep small)
Mantis conversion info:
- Mantis ID: 19898
- Version: 0.9.30
- Fixed in revision: 31869 (#826d94a2)