Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7526101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:44:35+00:00 2026-05-30T03:44:35+00:00

I am writing a simple platform game, and I’ve found that when removing ‘ghost’

  • 0

I am writing a simple platform game, and I’ve found that when removing ‘ghost’ instances, they persist and are not garbage collected. It seems that although I am removing all references, the ghost objects have some kind of internal references that are preventing them being garbage collected. Specifically they have attributes which are method switches.

The following code illustrates my problem:

import weakref

weak_ghosts = weakref.WeakKeyDictionary()

class Ghost(object):
    def __init__(self):
        #pass
        self.switch = {'eat':self.eat, 'sleep':self.sleep}

    def eat(self):
        pass

    def sleep(self):
        pass

ghost = Ghost()
weak_ghosts[ghost] = None
#ghost.switch = {}    # uncomment this line and ghost is successfully removed
del ghost
print "number of ghosts =", len(weak_ghosts)

#output:
number of ghosts = 1

Questions:

  1. What is actually going on?
  2. What should I be doing to avoid this situation?
  3. Am I using the correct methodology for making a switchable dictionary of methods?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:44:37+00:00Added an answer on May 30, 2026 at 3:44 am

    There is a circular reference created by self.switch referencing the object it’s part of. Check this out:

    import weakref
    
    class Ghost(object):
        def __init__(self):
            #pass
            self.switch = {'eat':self.eat, 'sleep':self.sleep}
    
        def eat(self):
            pass
    
        def sleep(self):
            pass
    
    ghost = Ghost()
    
    def callback(o):
        print 'callback', o
    
    wref = weakref.ref(ghost, callback)
    print 'del ghost'
    del ghost
    print 'after del ghost'
    

    Prints:

    del ghost
    after del ghost
    callback <weakref at 00B55FC0; dead>
    

    So the actual object was just cleaned on shutdown.

    You can run the GC manually to see the effect. Add this to the end of the script above:

    print 'gc.collect'
    import gc
    gc.collect()
    print 'after gc.collect'
    

    Now you’ll see:

    del ghost
    after del ghost
    gc.collect
    callback <weakref at 00B55FC0; dead>
    after gc.collect
    

    Note that by default, this GC is enabled and will run from time to time. It will clean up your ghost objects because they become unreachable circular references.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing simple site that requires users and profiles to be handled. The
I'm writing a simple OpenGL application that uses GLUT . I don't want to
I'm writing a simple app that's going to have a tiny form sitting in
I am currently writing a simple, timer-based mini app in C# that performs an
I am writing a simple checkers game in Java. When I mouse over the
I'm writing a simple blogging platform with ASP.NET MVC. My question is regarding forms
I am writing a simple business app that retrieves data from a server for
I'm currently writing a game of immense sophistication and cunning, that will fill you
I'm interested in writing games (simple ones thankfully) that take advantage of the Adobe
I am writing a simple file browser app with Nokia Qt4.7 on Symbian^3 platform.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.