Skip to content

Commit

Permalink
0.1.10 - fix decompress data refcount issue
Browse files Browse the repository at this point in the history
  • Loading branch information
K0lb3 committed Dec 7, 2024
1 parent a0d704a commit 4ba915f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion astc_encoder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
The exception is aarch64, which uses the neon encoder by default.
"""

__version__ = "0.1.9"
__version__ = "0.1.10"

from .enum import (
ASTCConfigFlags as ASTCConfigFlags,
Expand Down
1 change: 0 additions & 1 deletion src/pybind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,6 @@ PyObject *ASTCContext_method_decompress(ASTContextT *self, PyObject *args, PyObj
}

// create a python bytes object from the decompressed data
Py_IncRef(py_image_data);
Py_DecRef(py_image->data);
py_image->data = py_image_data;

Expand Down
20 changes: 17 additions & 3 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import gc
import os
import sys
from typing import Tuple
import gc
import psutil

import imagehash
import psutil
from PIL import Image

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

total_obj_count = len(gc.get_objects())

# create and destroy all object types
astc_encoder.ASTCSwizzle()
assert_memory_cleaned()
Expand All @@ -169,17 +172,28 @@ def assert_memory_cleaned():
# do a full round of compression/decompression
def full_run():
swizzle = astc_encoder.ASTCSwizzle()
astc_image_data = b"\00" * 64
astc_image_data_refs = sys.getrefcount(astc_image_data)
astc_image = astc_encoder.ASTCImage(
astc_encoder.ASTCType.U8, 4, 4, data=b"\00" * 64
astc_encoder.ASTCType.U8, 4, 4, 1, astc_image_data
)
config = astc_encoder.ASTCConfig(astc_encoder.ASTCProfile.LDR_SRGB, 4, 4)
context = astc_encoder.ASTCContext(config)
comp = context.compress(astc_image, swizzle)
astc_image_new = astc_encoder.ASTCImage(astc_encoder.ASTCType.U8, 4, 4)
decomp = context.decompress(comp, astc_image_new, swizzle)

assert sys.getrefcount(astc_image) == 2
assert sys.getrefcount(astc_image.data) == astc_image_data_refs + 2
del astc_image
assert sys.getrefcount(astc_image_data) == astc_image_data_refs

assert sys.getrefcount(astc_image_new) == 3 # inp, astc_image_new, decomp
assert sys.getrefcount(astc_image_new.data) == 3

full_run()
assert_memory_cleaned()
assert total_obj_count >= len(gc.get_objects())


if __name__ == "__main__":
Expand Down

0 comments on commit 4ba915f

Please sign in to comment.