Commits

Lenard Lindstrom committed 7cd7afd

Ensure Surface reference count bug fix (changeset 3705fa59dd5b) actually works.

Add unit test to confirm that the view instance returned by Surface.get_view releases its reference count on the parent surface.
Thanks Christoph Gohlke for reporting the problem on the Pygame mailing list.

  • Participants
  • Parent commits 3705fa5

Comments (0)

Files changed (1)

File test/surface_test.py

 from pygame.compat import xrange_, as_bytes, as_unicode
 from pygame._view import View
 
+import gc
+import weakref
+
 def intify(i):
     """If i is a long, cast to an int while preserving the bits"""
     if 0x80000000 & i:
         s = pygame.Surface((2, 4), 0, 32)
         s.get_view(as_unicode('2'))
         s.get_view(as_bytes('2'))
+        
+        # Garbage collection
+        s = pygame.Surface((2, 4), 0, 32)
+        weak_s = weakref.ref(s)
+        v = s.get_view('3')
+        weak_v = weakref.ref(v)
+        gc.collect()
+        self.assertTrue(weak_s() is s)
+        self.assertTrue(weak_v() is v)
+        del v
+        gc.collect()
+        self.assertTrue(weak_s() is s)
+        self.assertTrue(weak_v() is None)
+        del s
+        gc.collect()
+        self.assertTrue(weak_s() is None)
 
     def test_set_colorkey(self):