Code: Select all
EnableExplicit
Global NewList IconList(), menu, e
Procedure CreateImg()
Protected img
img = CreateImage(#PB_Any, 24, 24, 32)
StartDrawing(ImageOutput(img))
Box(0, 0, 24, 24, RGB(Random(255), Random(255), Random(255)))
StopDrawing()
ProcedureReturn img
EndProcedure
Procedure NewMenu()
Protected c, i, img
If menu
FreeMenu(menu)
EndIf
ClearList(IconList())
menu = CreateImageMenu(#PB_Any, WindowID(0))
MenuTitle("Test")
c = 64
For i = 0 To c
img = CreateImg()
AddElement(IconList())
IconList() = img
MenuItem(#PB_Any, "" + img, ImageID(img))
Next
EndProcedure
Procedure RetainCounts()
Protected img
ForEach IconList()
img = IconList()
Debug "" + img + ", " + CocoaMessage(0, ImageID(img), "retainCount")
Next
EndProcedure
OpenWindow(0, 0, 0, 400, 300, "Test")
ButtonGadget(1, 10, 10, 120, 30, "Create Menu")
ButtonGadget(2, 10, 50, 120, 30, "Retain Count")
ButtonGadget(3, 10, 90, 120, 30, "Free Menu")
Repeat
e = WaitWindowEvent()
Select e
Case #PB_Event_Gadget
Select EventGadget()
Case 1
NewMenu()
Case 2
RetainCounts()
Case 3
ForEach IconList()
; While CocoaMessage(0, IconList(), "retainCount") > 1
; CocoaMessage(0, IconList(), "release")
; Wend
Next
If menu
FreeMenu(menu)
menu = 0
EndIf
EndSelect
EndSelect
Until e = #PB_Event_CloseWindow
Click Retain Counts
Open/show/click menu
Click Retain Counts
Free Menu
Click Retain Counts
The images are still in memory after menu is freed? I would expect a crash because the object should not be there anymore...