Lenard Lindstrom avatar 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.

Comments (0)

Files changed (1)

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):
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.