Fix memory leak when reusing DmtxEncode across multiple encode calls#64
Open
94xhn wants to merge 1 commit into
Open
Fix memory leak when reusing DmtxEncode across multiple encode calls#6494xhn wants to merge 1 commit into
94xhn wants to merge 1 commit into
Conversation
dmtxEncodeDataMatrix() unconditionally overwrote enc->message and enc->image on every call without freeing any previous values held by the same DmtxEncode struct. Reusing one DmtxEncode for multiple dmtxEncodeDataMatrix() calls (a supported and documented pattern) therefore leaked the prior message/image on each call. Free the existing enc->message and enc->image (mirroring the logic already used in dmtxEncodeDestroy()) immediately before allocating their replacements. This is a no-op on the first call, since both fields are NULL on a freshly created DmtxEncode. Fixes dmtx#40
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
dmtxEncodeDataMatrix()unconditionally does:without freeing whatever
enc->message/enc->imagealready held from aprevious call on the same
DmtxEncode. Reusing oneDmtxEncodeacrossmultiple
dmtxEncodeDataMatrix()calls (as shown in the repro below) leaksthe prior message and image on every call after the first.
Repro (see #40):
Fix
Free the existing
enc->message/enc->image(mirroring the logicalready used in
dmtxEncodeDestroy(), including the pixel-buffer free)immediately before allocating their replacements. Both fields are
NULLon a freshly created
DmtxEncode, anddmtxMessageDestroy()/dmtxImageDestroy()are already NULL-safe, so this is a no-op on thefirst call — no behavior change for the common single-use case.
Testing
dmtx.cwith GNU ld--wrap=malloc/calloc/free/reallocto count net (unfreed) allocations.
DmtxEncodeleaks ~6 allocations / ~30KB versus a single encode+destroybaseline (isolating library-internal allocations from libc/CRT
startup noise, which is identical in both cases).
(no residual leak), including under a 5-cycle reuse stress test.
DmtxEncodeto encode two differentstrings, decoded each resulting image independently, and confirmed both
decode back to their original input — the added early frees don't
corrupt or clobber the freshly created structures still in use.
Fixes #40