Skip to content

Commit 22360ba

Browse files
committed
0.1.10 - fix decompress data refcount issue
1 parent ddc4301 commit 22360ba

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

Diff for: astc_encoder/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
The exception is aarch64, which uses the neon encoder by default.
77
"""
88

9-
__version__ = "0.1.9"
9+
__version__ = "0.1.10"
1010

1111
from .enum import (
1212
ASTCConfigFlags as ASTCConfigFlags,

Diff for: src/pybind.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,6 @@ PyObject *ASTCContext_method_decompress(ASTContextT *self, PyObject *args, PyObj
673673
}
674674

675675
// create a python bytes object from the decompressed data
676-
// Py_IncRef(py_image_data); // ref count is already increased by PyBytes_FromStringAndSize
677676
Py_DecRef(py_image->data);
678677
py_image->data = py_image_data;
679678

Diff for: tests/test_main.py

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import gc
12
import os
3+
import sys
24
from typing import Tuple
3-
import gc
4-
import psutil
55

66
import imagehash
7+
import psutil
78
from PIL import Image
89

910
import astc_encoder
@@ -145,6 +146,8 @@ def assert_memory_cleaned():
145146
# a leak in C/C++ would most likely not align with this
146147
assert (current_memory - initial_memory) % 4096 == 0
147148

149+
total_obj_count = len(gc.get_objects())
150+
148151
# create and destroy all object types
149152
astc_encoder.ASTCSwizzle()
150153
assert_memory_cleaned()
@@ -169,17 +172,28 @@ def assert_memory_cleaned():
169172
# do a full round of compression/decompression
170173
def full_run():
171174
swizzle = astc_encoder.ASTCSwizzle()
175+
astc_image_data = b"\00" * 64
176+
astc_image_data_refs = sys.getrefcount(astc_image_data)
172177
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
174179
)
175180
config = astc_encoder.ASTCConfig(astc_encoder.ASTCProfile.LDR_SRGB, 4, 4)
176181
context = astc_encoder.ASTCContext(config)
177182
comp = context.compress(astc_image, swizzle)
178183
astc_image_new = astc_encoder.ASTCImage(astc_encoder.ASTCType.U8, 4, 4)
179184
decomp = context.decompress(comp, astc_image_new, swizzle)
180185

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+
181194
full_run()
182195
assert_memory_cleaned()
196+
assert total_obj_count >= len(gc.get_objects())
183197

184198

185199
if __name__ == "__main__":

0 commit comments

Comments
 (0)