Pygame segmentation fault: issue with classing and recursion?

Question:

The error is:

Fatal Python error: (pygame parachute) Segmentation Fault
Aborted (core dumped)

On a different machine there is an output about max recursion depth. There are a few places where recursion could be occurring.

There is a pygame while event loop that is called within a class called Menu. Any time the enter key is pressed the while event loop stops. Then a new Menu class is called. And then this new Menu class starts a new while event loop. The Menu classes are all subclasses of a main Menu class.

As each new Menu class is initiated within the previous Menu this is where recursion might be occurring.

For example Menu:OpeningMenu() runs the first while events loop.
Then if the first item is selected Menu:CreateCharacter() runs a new while events loop.
Then if the second item is selected Menu:CreateCharacterAutomatic() runs a new while events loop.
Then if the first item is selected Menu:StoryScreen() runs a new while events loop.
And now select the first item about 60 times. This alternates between new instances of Menu:StoryScreen() and Menu:DayScreen(). After about the 60th time the segfault will occur.

What is interesting is that the segfault does not seem to occur when iterating anywhere before the Menu:DayScreen() code. So going from Menu:OpeningMenu() to Menu:HighScores() and back again endlessly will not cause the segfault. However, here is what is interesting: When going to the first instance of Menu:DayScreen() it immediately segfaults. That is, whatever counter that requires 60 of something is already set when first opening Menu:DayScreen() and the segfault indeed occurs.

Running previous_menu.__del__() does not seem to solve the issue. Although this indeed exits the previous menu there is still a segmentation fault that occurs during Menu:DayScreen().

Update

Now seeing:

RuntimeError: maximum recursion depth exceeded while getting the str of an object

This is after implementing two calls for every inheriting class menu:

Menu.__init__(self)
previous_menu.__del__()

Where previous_menu is the last Menu instance that started the present instance. So is this an issue with Pygame or an issue with how the code is calling the classes in Python?

To reproduce the bug: Select "Start", Select "Auto", Select "Begin Your Adventure", Select "Start Day", Select "Next Day". Now continue doing "Start Day" and "Next Day" for about 120 times (60 times each). At about this time the recursion bug will fire.

Here is the full output:

Traceback (most recent call last):
  File "Trumpocalypse.py", line 826, in <module>
    GameState()
  File "Trumpocalypse.py", line 495, in __init__
    self.current_screen = OpeningMenu() # Start the events while loop.
  File "Trumpocalypse.py", line 789, in __init__
    self.keypressFunction()
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 766, in __init__
    self.keypressFunction()
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 638, in __init__
    self.keypressFunction(Text,32)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__
    self.keypressFunction(text,32,20,300) #Looks the same as highscore
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 745, in __init__
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 725, in __init__

...
     
    self.keypressFunction(text,32,60,250) # Pass text (text,font size,top allignment,height of box)
  File "Trumpocalypse.py", line 275, in keypressFunction
    self.keypressArray[chosen_position](self)
  File "Trumpocalypse.py", line 689, in __init__
    Menu.__init__(self)
RuntimeError: maximum recursion depth exceeded while calling a Python object
__del__ <__main__.DayScreen instance at 0x7f7bee5eddd0>
__del__ <__main__.StoryScreen instance at 0x7f7bee5edcb0>
__del__ <__main__.DayScreen instance at 0x7f7bee5edb00>
__del__ <__main__.StoryScreen instance at 0x7f7bee5ed9e0>
__del__ <__main__.DayScreen instance at 0x7f7bee5ed830>
__del__ <__main__.StoryScreen instance at 0x7f7bee5ed710>
__del__ <__main__.DayScreen instance at 0x7f7bee5ed560>
__del__ <__main__.StoryScreen instance at 0x7f7bee5ed440>
__del__ <__main__.DayScreen instance at 0x7f7bee5ed290>
__del__ <__main__.StoryScreen instance at 0x7f7bee5ed170>
__del__ <__main__.DayScreen instance at 0x7f7bee5e7f80>
__del__ <__main__.StoryScreen instance at 0x7f7bee5e7e60>
__del__ <__main__.DayScreen instance at 0x7f7bee5e7cb0>
__del__ <__main__.StoryScreen instance at 0x7f7bee5e7b90>
__del__ <__main__.DayScreen instance at 0x7f7bee5e79e0>
__del__ <__main__.StoryScreen instance at 0x7f7bee5e78c0>
__del__ <__main__.DayScreen instance at 0x7f7bee5e7710>
__del__ <__main__.StoryScreen instance at 0x7f7bee5e75f0>
__del__ <__main__.DayScreen instance at 0x7f7bee5e7440>
__del__ <__main__.StoryScreen instance at 0x7f7bee5e7320>
__del__ <__main__.DayScreen instance at 0x7f7bee5e7170>
__del__ <__main__.StoryScreen instance at 0x7f7bee5e7050>
__del__ <__main__.DayScreen instance at 0x7f7bee5e1e60>
__del__ <__main__.StoryScreen instance at 0x7f7bee5e1d40>
__del__ <__main__.DayScreen instance at 0x7f7bee5e1b90>
__del__ <__main__.StoryScreen instance at 0x7f7bee5e1a70>
__del__ <__main__.DayScreen instance at 0x7f7bee5e18c0>
__del__ <__main__.StoryScreen instance at 0x7f7bee5e17a0>

__del__ <__main__.DayScreen instance at 0x7f7bee5e15f0>
__del__ <__main__.StoryScreen instance at 0x7f7bee5e14d0>
__del__ <__main__.DayScreen instance at 0x7f7bee5e1320>
__del__ <__main__.StoryScreen instance at 0x7f7bee5e1200>
__del__ <__main__.DayScreen instance at 0x7f7bee5e1050>
__del__ <__main__.StoryScreen instance at 0x7f7bee659ef0>
__del__ <__main__.DayScreen instance at 0x7f7bee659d40>
__del__ <__main__.StoryScreen instance at 0x7f7bee659c20>
__del__ <__main__.DayScreen instance at 0x7f7bee659a70>
__del__ <__main__.StoryScreen instance at 0x7f7bee659950>
__del__ <__main__.DayScreen instance at 0x7f7bee6597a0>
__del__ <__main__.StoryScreen instance at 0x7f7bee659680>
__del__ <__main__.DayScreen instance at 0x7f7bee6594d0>

...

__del__ <__main__.StoryScreen instance at 0x7f7bee92bd40>
__del__ <__main__.DayScreen instance at 0x7f7bef066710>
__del__ <__main__.StoryScreen instance at 0x7f7bef08e6c8>
__del__ <__main__.CreateCharacterAutomatic instance at 0x7f7bee92b638>
__del__ <__main__.CreateCharacter instance at 0x7f7bee92b9e0>
__del__ <__main__.OpeningMenu instance at 0x7f7bee6b3b48>

Solution

The class was calling another class and thus recursing. (Passing the parent instance to the child for the child to instruct the parent to end itself was either not working or not implemented correctly.) For example:

class Menu1():
    def __init__(self):
        Menu2(self)
class Menu2():
    def __init__(self, previous):
        previous.__del__() # Not working.

The solution for now is to have a parent class direct which classes to call. For example:

class Menu1():
    def __init__(self):
        pass
class Menu2():
    def __init__(self):
        pass
a=Menu1()
a=Menu2()

This works and does not run into the issue with recursion.

Asked By: There

||

Answers:

You have to rethink your menu system. The keypressFunction and the __init__ methods of the Menu subclasses cause the recursion.

In every __init__ method of the Menu subclasses you call self.keypressFunction which then instantiates the next Menu subclass in line 275 self.keypressArray[chosen_position](self) which calls self.keypressFunction in its __init__ again and so on until Python and Pygame crash, because the recursion limit of 1000 recursions is exceeded.

Here’s a nice example of a simple finite state machine by iminurnamez which you can use to implement scenes and menus.

Answered By: skrx
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.