DirectObject and Garbage Collection [solved]

I’m trying to write a very simple method to switch between a main menu and the game world.

The Game is a DirectObject and MainMenu is a DirectObject. I only want one of them to be in memory at a time. The documentation says that to get a DirectObject scheduled for garbage collection to call DirectObject.IgnoreAll(). However, I wanted to double check to see if it was working and as far as I can tell MainMenu is not being garbage collected.

I’m using weakref to check garbage collection, here’s the code in three modules:

import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject

from game import Game
from mainMenu import MainMenu

import weakref

	import psyco

#TODO: Check if the instances are getting garbaged collected.

class SceneChooser(DirectObject):
	def __init__(self):
		self.accept('startMainMenu', self.startMainMenu)
		self.accept('startGame', self.startGame)
		self.tester = []
	def startMainMenu(self): = MainMenu()
	def startGame(self): = Game()
		print "in SceneChooser::startGame"
		print [instance() for instance in self.tester]

from direct.showbase.DirectObject import DirectObject

class Game(DirectObject):
	def __init__(self):
		print "Impliment the game; No problem!"
		self.accept('escape', self.gameMenu)
	def gameMenu(self):
		#TODO: implement a game menu, sending to main menu for now
import sys

from direct.showbase.DirectObject import DirectObject
from direct.gui.OnscreenText import OnscreenText
from direct.gui.DirectButton import DirectButton
from pandac.PandaModules import Vec3,Vec4

from game import Game

class MainMenu(DirectObject):
		def __init__(self):
			font = loader.loadFont("cmss12")
			self.title = OnscreenText(text = "Main Menu", font = font, style = 1, fg = (1,1,1,1), 
															pos = (0.0,0.5), scale = 0.5)

			self.gameButton = DirectButton(pos = Vec3(0,0,0), text = "Start Game",
																		scale = .1, pad = (.5, .5), text_font = font,
																		command = self.runGame)
			self.exitButton = DirectButton(pos = Vec3(-0.001,0,-0.3), text = "Exit Game",
																		scale = .1, pad = (0.745,0.5), text_font = font,
																		command = sys.exit)
		def runGame(self):

As far as I can tell I am correctly calling the ignoreAll() to remove the reference that the Panda3D engine has to them and I am not creating any new references in my own code that I can see.

Thanks for your help!

I decided to check to see if instances of game.Game were sticking around as well, they are not. So Game is being garbage collected.

If I comment the buttons out of the code in MainMenu then it does get garbage collected. So I assume I am not removing the button correctly. I have looked through the manual but I didn’t see any ways to remove buttons that does result in the MainMenu being garbage collected.

Thanks again!

Try button.destroy().

Many thanks that was it!