[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Referencing instances by memory address



On Mon, Jul 12, 2010 at 9:25 AM, éæå <onpon4@xxxxxxxxx
<mailto:onpon4@xxxxxxxxx>> wrote:

    In my game that I'm working on, Senso (which is my first game in
    Pygame), there is a main game class, Game, which handles things on
    the global level. One thing it does is store each instance of game
    objects in dictionaries, with the dictionary keys being each
    instance's memory address, grabbed with id(). When an instance needs
    to be removed, part of the process involves removing the dictionary
    reference of the instance, by calling "del
    game.myclassdict[id(self)]" (replacing "myclassdict" with the actual
    name of the correct dictionary). This seems to cause a problem:
    sometimes, this action seems to reference a nonexistant key in the
    respective dictionary. I've only experienced it with bullets. I've
    checked and double-checked the code, and it doesn't seem that at any
    point I forget to add an instance to the dictionary.

Like Brian Fisher wrote, you probably shouldn't be storing references this way, since there doesn't seem to be a good reason for it. (We got into a fun discussion once about "bullet theology". Should bullet objects be destroyed or recycled when their time runs out?)

My guess is that you're experiencing a problem with Pygame's "garbage collection" system, which destroys objects once there are no more references to them. Maybe Python is destroying your object's ID reference before the deletion command is fully carried out, causing the id() command to fail?