From: John L. <jla...@gm...> - 2013-11-17 18:37:50
|
On Sun, Nov 17, 2013 at 9:21 AM, Andreas Falkenhahn <an...@fa...>wrote: > Hi, > > what's the recommended way to prevent objects from being garbage collected? > For example, I create a wxImageList and pass it to > wxNotebook::SetImageList(). > As wxNotebook::SetImageList() doesn't take ownership of the list it could > happen that the garbage collector kills the wxImageList that I passed to > SetImageList(). > > To prevent it from being collected I could assign the wxImageList to a > global > variable but that doesn't look like a nice solution.... it would be nicer > if it were possible to reference the wxImageList somehow but I don't see > how this is possible. wxObject has a Ref() method but that doesn't seem to > be up for the job. > > You're right, wxObject::Ref() is not what you want, in wxLua it will cause a memory leak unless you remember to call UnRef() on it. The best way is to either make it a global or attach the image list to some other object that will live at least as long as the window it's attached to. You can also simply add it to the window itself. local notebook = wx.wxNotebook(...) local imagelist = wx.wxImageList(...) notebook.imagelist = imagelist -- it is now a member of the notebook's table notebook.SetImageList(imagelist) Regards, John |