Fix memory leak

Access checksum objects directly vs pointer reference. Structure size equals a 64 bit pointer (fits into a single register).
Allocated memory was never deallocated (my mistake).
This commit is contained in:
Kawe Mazidjatari 2022-06-05 03:01:10 +02:00
parent fb0a3db69e
commit a4783a755e
6 changed files with 27 additions and 31 deletions

View File

@ -161,7 +161,7 @@ extern "C" {
// Deinitializes a compressor, releasing all allocated memory.
// returns adler32 and crc32 of source data (valid only on success).
LZHAM_DLL_EXPORT lzham_compress_checksums* LZHAM_CDECL lzham_compress_deinit(lzham_compress_state_ptr pState);
LZHAM_DLL_EXPORT lzham_compress_checksums LZHAM_CDECL lzham_compress_deinit(lzham_compress_state_ptr pState);
// Compresses an arbitrarily sized block of data, writing as much available compressed data as possible to the output buffer.
// This method may be called as many times as needed, but for best perf. try not to call it with tiny buffers.
@ -282,7 +282,7 @@ extern "C" {
// Deinitializes a decompressor.
// returns adler32 of decompressed data if compute_adler32 was true, otherwise it returns the adler32 from the compressed stream.
LZHAM_DLL_EXPORT lzham_decompress_checksums* LZHAM_CDECL lzham_decompress_deinit(lzham_decompress_state_ptr pState);
LZHAM_DLL_EXPORT lzham_decompress_checksums LZHAM_CDECL lzham_decompress_deinit(lzham_decompress_state_ptr pState);
// Decompresses an arbitrarily sized block of compressed data, writing as much available decompressed data as possible to the output buffer.
// This method is implemented as a coroutine so it may be called as many times as needed. However, for best perf. try not to call it with tiny buffers.
@ -618,14 +618,14 @@ extern "C" {
typedef lzham_compress_state_ptr (LZHAM_CDECL *lzham_compress_init_func)(const lzham_compress_params *pParams);
typedef lzham_compress_state_ptr (LZHAM_CDECL *lzham_compress_reinit_func)(lzham_compress_state_ptr pState);
typedef lzham_compress_checksums* (LZHAM_CDECL *lzham_compress_deinit_func)(lzham_compress_state_ptr pState);
typedef lzham_compress_checksums (LZHAM_CDECL *lzham_compress_deinit_func)(lzham_compress_state_ptr pState);
typedef lzham_compress_status_t (LZHAM_CDECL *lzham_compress_func)(lzham_compress_state_ptr pState, const lzham_uint8 *pIn_buf, size_t *pIn_buf_size, lzham_uint8 *pOut_buf, size_t *pOut_buf_size, lzham_bool no_more_input_bytes_flag);
typedef lzham_compress_status_t (LZHAM_CDECL *lzham_compress2_func)(lzham_compress_state_ptr pState, const lzham_uint8 *pIn_buf, size_t *pIn_buf_size, lzham_uint8 *pOut_buf, size_t *pOut_buf_size, lzham_flush_t flush_type);
typedef lzham_compress_status_t (LZHAM_CDECL *lzham_compress_memory_func)(const lzham_compress_params *pParams, lzham_uint8* pDst_buf, size_t *pDst_len, const lzham_uint8* pSrc_buf, size_t src_len, lzham_uint32 *pAdler32, lzham_uint32* pCrc32);
typedef lzham_decompress_state_ptr (LZHAM_CDECL *lzham_decompress_init_func)(const lzham_decompress_params *pParams);
typedef lzham_decompress_state_ptr (LZHAM_CDECL *lzham_decompress_reinit_func)(lzham_compress_state_ptr pState, const lzham_decompress_params *pParams);
typedef lzham_decompress_checksums* (LZHAM_CDECL *lzham_decompress_deinit_func)(lzham_decompress_state_ptr pState);
typedef lzham_decompress_checksums (LZHAM_CDECL *lzham_decompress_deinit_func)(lzham_decompress_state_ptr pState);
typedef lzham_decompress_status_t (LZHAM_CDECL *lzham_decompress_func)(lzham_decompress_state_ptr pState, const lzham_uint8 *pIn_buf, size_t *pIn_buf_size, lzham_uint8 *pOut_buf, size_t *pOut_buf_size, lzham_bool no_more_input_bytes_flag);
typedef lzham_decompress_status_t (LZHAM_CDECL *lzham_decompress_memory_func)(const lzham_decompress_params *pParams, lzham_uint8* pDst_buf, size_t *pDst_len, const lzham_uint8* pSrc_buf, size_t src_len, lzham_uint32 *pAdler32, lzham_uint32 *pCrc32);

View File

@ -24,7 +24,7 @@ extern "C" LZHAM_DLL_EXPORT lzham_decompress_state_ptr lzham_decompress_reinit(l
return lzham::lzham_lib_decompress_reinit(p, pParams);
}
extern "C" LZHAM_DLL_EXPORT lzham_decompress_checksums* lzham_decompress_deinit(lzham_decompress_state_ptr p)
extern "C" LZHAM_DLL_EXPORT lzham_decompress_checksums lzham_decompress_deinit(lzham_decompress_state_ptr p)
{
return lzham::lzham_lib_decompress_deinit(p);
}
@ -53,7 +53,7 @@ extern "C" LZHAM_DLL_EXPORT lzham_compress_state_ptr lzham_compress_reinit(lzham
return lzham::lzham_lib_compress_reinit(p);
}
extern "C" LZHAM_DLL_EXPORT lzham_compress_checksums* lzham_compress_deinit(lzham_compress_state_ptr p)
extern "C" LZHAM_DLL_EXPORT lzham_compress_checksums lzham_compress_deinit(lzham_compress_state_ptr p)
{
return lzham::lzham_lib_compress_deinit(p);
}

View File

@ -9,7 +9,7 @@ namespace lzham
lzham_compress_state_ptr LZHAM_CDECL lzham_lib_compress_reinit(lzham_compress_state_ptr p);
lzham_compress_checksums* LZHAM_CDECL lzham_lib_compress_deinit(lzham_compress_state_ptr p);
lzham_compress_checksums LZHAM_CDECL lzham_lib_compress_deinit(lzham_compress_state_ptr p);
lzham_compress_status_t LZHAM_CDECL lzham_lib_compress(
lzham_compress_state_ptr p,

View File

@ -150,18 +150,15 @@ namespace lzham
return pState;
}
lzham_compress_checksums* LZHAM_CDECL lzham_lib_compress_deinit(lzham_compress_state_ptr p)
lzham_compress_checksums LZHAM_CDECL lzham_lib_compress_deinit(lzham_compress_state_ptr p)
{
lzham_compress_checksums checksums{};
lzham_compress_state *pState = static_cast<lzham_compress_state *>(p);
if (!pState)
return nullptr;
return checksums;
lzham_compress_checksums* checksums = new lzham_compress_checksums();
checksums->adler32 = pState->m_compressor.get_src_adler32();
checksums->crc32 = pState->m_compressor.get_src_crc32();
printf("checksums->adler32 %zX\n", checksums->adler32);
printf("checksums->crc32 %zX\n", checksums->crc32);
checksums.adler32 = pState->m_compressor.get_src_adler32();
checksums.crc32 = pState->m_compressor.get_src_crc32();
lzham_delete(pState);
return checksums;
@ -558,10 +555,10 @@ namespace lzham
lzham_compress_state_ptr pComp = (lzham_compress_state_ptr)pStream->state;
if (pComp)
{
lzham_compress_checksums* checksums = lzham_lib_compress_deinit(pComp);
lzham_compress_checksums checksums = lzham_lib_compress_deinit(pComp);
pStream->adler32 = checksums->adler32;
pStream->crc32 = checksums->crc32;
pStream->adler32 = checksums.adler32;
pStream->crc32 = checksums.crc32;
pStream->state = NULL;
}

View File

@ -11,7 +11,7 @@ namespace lzham
lzham_decompress_state_ptr LZHAM_CDECL lzham_lib_decompress_reinit(lzham_decompress_state_ptr pState, const lzham_decompress_params *pParams);
lzham_decompress_checksums* LZHAM_CDECL lzham_lib_decompress_deinit(lzham_decompress_state_ptr pState);
lzham_decompress_checksums LZHAM_CDECL lzham_lib_decompress_deinit(lzham_decompress_state_ptr pState);
lzham_decompress_status_t LZHAM_CDECL lzham_lib_decompress(
lzham_decompress_state_ptr pState,

View File

@ -1273,16 +1273,15 @@ namespace lzham
return pState;
}
lzham_decompress_checksums* LZHAM_CDECL lzham_lib_decompress_deinit(lzham_decompress_state_ptr p)
lzham_decompress_checksums LZHAM_CDECL lzham_lib_decompress_deinit(lzham_decompress_state_ptr p)
{
lzham_decompress_checksums checksums{};
lzham_decompressor *pState = static_cast<lzham_decompressor *>(p);
if (!pState)
return NULL;
return checksums;
lzham_decompress_checksums* checksums = new lzham_decompress_checksums();
checksums->adler32 = pState->m_decomp_adler32;
checksums->crc32 = pState->m_decomp_crc32;
checksums.adler32 = pState->m_decomp_adler32;
checksums.crc32 = pState->m_decomp_crc32;
lzham_free(pState->m_pRaw_decomp_buf);
lzham_delete(pState);
@ -1357,12 +1356,12 @@ namespace lzham
return LZHAM_DECOMP_STATUS_FAILED_INITIALIZING;
lzham_decompress_status_t status = lzham_lib_decompress(pState, pSrc_buf, &src_len, pDst_buf, pDst_len, true);
lzham_decompress_checksums* checksums = lzham_lib_decompress_deinit(pState);
lzham_decompress_checksums checksums = lzham_lib_decompress_deinit(pState);
if (pAdler32)
*pAdler32 = checksums->adler32;
*pAdler32 = checksums.adler32;
if (pCrc32)
*pCrc32 = checksums->crc32;
*pCrc32 = checksums.crc32;
return status;
@ -1511,10 +1510,10 @@ namespace lzham
lzham_decompress_state_ptr pState = static_cast<lzham_decompress_state_ptr>(pStream->state);
if (pState)
{
lzham_decompress_checksums* checksums = lzham_lib_decompress_deinit(pState);
lzham_decompress_checksums checksums = lzham_lib_decompress_deinit(pState);
pStream->adler32 = checksums->adler32;
pStream->crc32 = checksums->crc32;
pStream->adler32 = checksums.adler32;
pStream->crc32 = checksums.crc32;
pStream->state = NULL;
}