Repository navigation
Free the page a raw annotation handle is built on - #127
Merged
Merged
Conversation
Every handle from EPDFPage_GetAnnotRaw() or EPDFPage_CreateAnnotRaw() leaked one CPDF_Page (11 blocks, about 1.4 KB), on native and WASM, since 61181c3 (Aug 6 2025). The page was held by RawAnnotContext, a subclass of CPDF_AnnotContext. FPDFPage_CloseAnnot() deletes a handle as CPDF_AnnotContext, whose destructor is not virtual, so the subclass's member was never destroyed. Reported in embedpdf/embed-pdf-viewer#1124. CPDF_AnnotContext now owns the page itself, and is final: - CreateOwningPage() makes a handle that keeps the page it was built on. The two raw functions use it, and RawAnnotContext is gone. The page is the first member, so it is destroyed last, after the members that point at it. - CreateLinked() makes the handle FPDFAnnot_GetLinkedAnnot() returns. It shares a page its parent owns, so the two close in either order. A linked handle that only borrowed the page would dangle once its parent closed first; the leak hid that. - GetWidget() and FORM_SetFocusedAnnot() return early for a handle that owns its page. They failed for it already, since its page view is never valid, but only after making a view keyed by that page, which would now outlive the page. No return value changes. A handle for a loaded page borrows it, as before. Tests in epdf_annot_raw_embeddertest.cpp, all failing without the change: ClosingAHandleFreesItsPage, ALinkedHandleSharesThePage and FormFillCallsMakeNoViewForAHandleWithoutAPage. The first two are listed as layer-mode writers. A caller that worked around the leak by closing the page it read at handle + 0x40 must stop with this change: that releases the page a second time, and the offset no longer holds the page.
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.
Every handle from
EPDFPage_GetAnnotRaw()orEPDFPage_CreateAnnotRaw()leaked oneCPDF_Page: 11 blocks, about 1.4 KB, on native and WASM. It has done so since 61181c3 (Aug 6 2025), so in every 2.x and 3.x. The engine opens a raw handle for every annotation read. Reported in embedpdf/embed-pdf-viewer#1124.Cause
The raw functions build a
CPDF_Pagefrom the page dictionary alone, for a page nobody loaded. They gave it toRawAnnotContext, a subclass ofCPDF_AnnotContextwith one more member: the page.FPDFPage_CloseAnnot()deletes a handle asCPDF_AnnotContext, whose destructor is not virtual, so the subclass's member was never destroyed.Change
CPDF_AnnotContextowns the page itself, for the handles that made one. A handle for a loaded page borrows it, as before.CPDF_AnnotContext::CreateOwningPage()makes a handle that keeps the page it was built on. Both raw functions use it, andRawAnnotContextis gone. The page is the first member, so it is destroyed last, after the members that point at it.final, so the same mistake no longer compiles.CPDF_AnnotContext::CreateLinked()makes the handleFPDFAnnot_GetLinkedAnnot()returns. It shares a page its parent owns, so the two handles close in either order. A linked handle that only borrowed the page would dangle once its parent closed first; the leak hid that.GetWidget()andFORM_SetFocusedAnnot()infpdf_formfill.cppreturn early for a handle that owns its page. They failed for it already, because its page view is never valid, but only after making a view keyed by that page, which would now outlive the page. No return value changes.public/epdf_annot.hsays how such a handle is closed, that a linked handle may be closed before or after it, and that the three form-fill calls fail for it.epdf-api(a core helper that backs anEPDF*function), and the ledger's rule namesCreateOwningPage.A named factory, not a second constructor:
RetainPtr<T>converts toT*here, so an overload on the two is ambiguous.Not chosen:
fpdf_annot.cppand cannot share a page that only a subclass knows.FPDF_ClosePage(). That changes regular handles too, which is too wide for a fix that goes back to v2.Tests
Three in
epdf_annot_raw_embeddertest.cpp. All fail without the change.ClosingAHandleFreesItsPage: a created and a read raw handle each hold the only other reference to their page, and closing drops it.ALinkedHandleSharesThePage: a note and its popup; the parent closes first, the popup still works, and the page goes when both are closed. On a loaded page both handles borrow.FormFillCallsMakeNoViewForAHandleWithoutAPage: the three form-fill calls fail for a raw handle to a widget, and the form environment has no view for its page afterwards.The first two write, and are in
testing/embedpdf_layer_mode_writers.txt.Checked
macOS arm64, against the same tree without the change:
check-fork-tags.py: no untagged block.check-c-api.py: ok.MallocScribble): no crash, and only the baseline failures.leaks --atExiton a Node loop over the C API, 12,000 raw handles created, read and closed: 133,200 blocks (16.4 MB) on the released library (f2ea759), 0 with this build. With a linked handle closed after its parent: 139,199 blocks before, 0 after.Linux x64 in Docker, on f05c0fb (finished after the merge):
EPDFAnnotRawEmbedderTest.*: 8 pass, none failing only in layer mode, no unlisted writer.WASM was not measured; it is the same C++.
Left
FPDF_ClosePage()on the pointer read at handle + 0x40 must drop that in the same upgrade: it would release the page a second time, and the offset no longer holds the page.