[wxPython] Re: [1999-12-23] Bug in wxImageList - New Info.

win32
wxPython-2.2.15

Robin Dunn discussed an issue with wxImageList (Dec 23, 1999) that causes the images to get garbage-collected prematurely. He proposed a work-around in maintaining a reference to the wxImageList to prevent it from getting garbage-collected. However, not only is it necessary to maintain a reference to the list itself, but also to the window that contains the list.

Below is a program that demonstrates the bug. The workaround is to use

def OnInit(self):
self.win = ListFrame(None, -1, "")
self.win.Show(true)

however, this seems clumsy (I have to maintain a reference to every window that uses a wxImageList) and it took me quite some time to figure it out.

# ========== code start =================

from wxPython.wx import *

class ListFrame(wxFrame):
def __init__(self, parent, id, title):
wxFrame.__init__(self, parent, id, title)
list = wxListCtrl(self, -1, style = wxLC_REPORT)
list.InsertColumn(0, "col 0")
self.il = wxImageList(16, 16)
list.SetImageList(self.il, wxIMAGE_LIST_SMALL)
icon = wxBitmap('smiles.bmp', wxBITMAP_TYPE_BMP)
index = self.il.Add(icon)
for i in range(10):
list.InsertImageStringItem(0, 'label %d' % i, index)

class Application(wxApp):
def OnInit(self):
win = ListFrame(None, -1, "")
win.Show(true)
return true

app = Application()
app.MainLoop()