1
+ import gc
1
2
import os
3
+ import sys
2
4
from typing import Tuple
3
- import gc
4
- import psutil
5
5
6
6
import imagehash
7
+ import psutil
7
8
from PIL import Image
8
9
9
10
import astc_encoder
@@ -145,6 +146,8 @@ def assert_memory_cleaned():
145
146
# a leak in C/C++ would most likely not align with this
146
147
assert (current_memory - initial_memory ) % 4096 == 0
147
148
149
+ total_obj_count = len (gc .get_objects ())
150
+
148
151
# create and destroy all object types
149
152
astc_encoder .ASTCSwizzle ()
150
153
assert_memory_cleaned ()
@@ -169,17 +172,28 @@ def assert_memory_cleaned():
169
172
# do a full round of compression/decompression
170
173
def full_run ():
171
174
swizzle = astc_encoder .ASTCSwizzle ()
175
+ astc_image_data = b"\00 " * 64
176
+ astc_image_data_refs = sys .getrefcount (astc_image_data )
172
177
astc_image = astc_encoder .ASTCImage (
173
- astc_encoder .ASTCType .U8 , 4 , 4 , data = b" \00 " * 64
178
+ astc_encoder .ASTCType .U8 , 4 , 4 , 1 , astc_image_data
174
179
)
175
180
config = astc_encoder .ASTCConfig (astc_encoder .ASTCProfile .LDR_SRGB , 4 , 4 )
176
181
context = astc_encoder .ASTCContext (config )
177
182
comp = context .compress (astc_image , swizzle )
178
183
astc_image_new = astc_encoder .ASTCImage (astc_encoder .ASTCType .U8 , 4 , 4 )
179
184
decomp = context .decompress (comp , astc_image_new , swizzle )
180
185
186
+ assert sys .getrefcount (astc_image ) == 2
187
+ assert sys .getrefcount (astc_image .data ) == astc_image_data_refs + 2
188
+ del astc_image
189
+ assert sys .getrefcount (astc_image_data ) == astc_image_data_refs
190
+
191
+ assert sys .getrefcount (astc_image_new ) == 3 # inp, astc_image_new, decomp
192
+ assert sys .getrefcount (astc_image_new .data ) == 3
193
+
181
194
full_run ()
182
195
assert_memory_cleaned ()
196
+ assert total_obj_count >= len (gc .get_objects ())
183
197
184
198
185
199
if __name__ == "__main__" :
0 commit comments