diff --git a/encode.c b/encode.c index d821a54df..66ab1616c 100644 --- a/encode.c +++ b/encode.c @@ -483,9 +483,7 @@ PyImaging_ZipEncoderNew(PyObject* self, PyObject* args) &dictionary, &dictionary_size)) return NULL; - /* Copy to avoid referencing Python's memory, but there's no mechanism to - free this memory later, so this function (and several others here) - leaks. */ + /* Copy to avoid referencing Python's memory */ if (dictionary && dictionary_size > 0) { /* malloc check ok, size comes from PyArg_ParseTuple */ char* p = malloc(dictionary_size); @@ -504,6 +502,7 @@ PyImaging_ZipEncoderNew(PyObject* self, PyObject* args) return NULL; encoder->encode = ImagingZipEncode; + encoder->cleanup = ImagingZipEncodeCleanup; if (rawmode[0] == 'P') /* disable filtering */ @@ -638,6 +637,7 @@ PyImaging_JpegEncoderNew(PyObject* self, PyObject* args) if (get_packer(encoder, mode, rawmode) < 0) return NULL; + // Freed in JpegEncode, Case 5 qarrays = get_qtables_arrays(qtables, &qtablesLen); if (extra && extra_size > 0) { diff --git a/libImaging/Imaging.h b/libImaging/Imaging.h index bbef0440d..aa4009889 100644 --- a/libImaging/Imaging.h +++ b/libImaging/Imaging.h @@ -474,6 +474,7 @@ extern int ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8* buffer, int bytes); extern int ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8* buffer, int bytes); +extern int ImagingZipEncodeCleanup(ImagingCodecState state); #endif typedef void (*ImagingShuffler)(UINT8* out, const UINT8* in, int pixels); diff --git a/libImaging/ZipEncode.c b/libImaging/ZipEncode.c index 7f50be9ed..4a431fb33 100644 --- a/libImaging/ZipEncode.c +++ b/libImaging/ZipEncode.c @@ -348,6 +348,24 @@ ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) return -1; } +/* -------------------------------------------------------------------- */ +/* Cleanup */ +/* -------------------------------------------------------------------- */ + +int +ImagingZipEncodeCleanup(ImagingCodecState state) { + ZIPSTATE* context = (ZIPSTATE*) state->context; + + if (context->dictionary) { + free (context->dictionary); + context->dictionary = NULL; + } + + return -1; +} + + + const char* ImagingZipVersion(void) {