diff mbox series

[meta-oe,scarthgap,1/1] poppler: fix CVE-2025-52886

Message ID 20250711112842.3005393-1-yogita.urade@windriver.com
State New
Headers show
Series [meta-oe,scarthgap,1/1] poppler: fix CVE-2025-52886 | expand

Commit Message

yurade July 11, 2025, 11:28 a.m. UTC
From: Yogita Urade <yogita.urade@windriver.com>

Poppler is a PDF rendering library. Versions prior to 25.06.0
use `std::atomic_int` for reference counting. Because
`std::atomic_int` is only 32 bits, it is possible to overflow
the reference count and trigger a use-after-free. Version 25.06.0
patches the issue.

References:
https://nvd.nist.gov/vuln/detail/CVE-2025-52886
https://security-tracker.debian.org/tracker/CVE-2025-52886

Upstream patches:
https://gitlab.freedesktop.org/poppler/poppler/-/commit/3449a16d3b1389870eb3e20795e802c6ae8bc04f
https://gitlab.freedesktop.org/poppler/poppler/-/commit/ac36affcc8486de38e8905a8d6547a3464ff46e5

Signed-off-by: Yogita Urade <yogita.urade@windriver.com>
---
 .../poppler/poppler/CVE-2025-52886-0001.patch | 4325 +++++++++++++++++
 .../poppler/poppler/CVE-2025-52886-0002.patch |   58 +
 .../poppler/poppler_23.04.0.bb                |    2 +
 3 files changed, 4385 insertions(+)
 create mode 100644 meta-oe/recipes-support/poppler/poppler/CVE-2025-52886-0001.patch
 create mode 100644 meta-oe/recipes-support/poppler/poppler/CVE-2025-52886-0002.patch
diff mbox series

Patch

diff --git a/meta-oe/recipes-support/poppler/poppler/CVE-2025-52886-0001.patch b/meta-oe/recipes-support/poppler/poppler/CVE-2025-52886-0001.patch
new file mode 100644
index 0000000000..4f0dc61a8b
--- /dev/null
+++ b/meta-oe/recipes-support/poppler/poppler/CVE-2025-52886-0001.patch
@@ -0,0 +1,4325 @@ 
+From f2f933100eb18ade38ece640343007290c609e21 Mon Sep 17 00:00:00 2001
+From: Sune Vuorela <sune@vuorela.dk>
+Date: Wed, 2 Apr 2025 16:19:28 +0200
+Subject: [PATCH] Annot: Do refcount with shared_ptr
+
+Manage annots by shared_ptr rather than the manual ref/deref usage.
+
+Also do a bit more documentation of ownerships with unique ptr's
+
+CVE: CVE-2025-52886
+Upstream-Status: Backport [https://gitlab.freedesktop.org/poppler/poppler/-/commit/3449a16d3b1389870eb3e20795e802c6ae8bc04f]
+
+Signed-off-by: Yogita Urade <yogita.urade@windriver.com>
+---
+ glib/poppler-annot.cc                | 158 +++++------
+ glib/poppler-page.cc                 |   8 +-
+ glib/poppler-private.h               |  24 +-
+ poppler/Annot.cc                     | 143 ++++------
+ poppler/Annot.h                      |  34 +--
+ poppler/FontInfo.cc                  |   2 +-
+ poppler/Form.cc                      |  11 +-
+ poppler/Form.h                       |   6 +-
+ poppler/JSInfo.cc                    |   8 +-
+ poppler/Link.cc                      |  14 +-
+ poppler/Link.h                       |   4 +-
+ poppler/PDFDoc.cc                    |   2 +-
+ poppler/PSOutputDev.cc               |   2 +-
+ poppler/Page.cc                      |  39 ++-
+ poppler/Page.h                       |   8 +-
+ qt5/src/poppler-annotation-private.h |  10 +-
+ qt5/src/poppler-annotation.cc        | 393 +++++++++++++--------------
+ qt5/src/poppler-form.cc              |   4 +-
+ qt6/src/poppler-annotation-private.h |  10 +-
+ qt6/src/poppler-annotation.cc        | 393 +++++++++++++--------------
+ qt6/src/poppler-form.cc              |   4 +-
+ utils/HtmlOutputDev.cc               |   4 +-
+ utils/pdfdetach.cc                   |   4 +-
+ utils/pdfinfo.cc                     |   2 +-
+ 24 files changed, 582 insertions(+), 705 deletions(-)
+
+diff --git a/glib/poppler-annot.cc b/glib/poppler-annot.cc
+index b995f03..6fb7807 100644
+--- a/glib/poppler-annot.cc
++++ b/glib/poppler-annot.cc
+@@ -19,6 +19,7 @@
+  * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+  */
+
++#include "AnnotStampImageHelper.h"
+ #include "config.h"
+ #include "poppler.h"
+ #include "poppler-private.h"
+@@ -26,7 +27,7 @@
+ #define ZERO_CROPBOX(c) (!(c && (c->x1 > 0.01 || c->y1 > 0.01)))
+
+ const PDFRectangle *_poppler_annot_get_cropbox_and_page(PopplerAnnot *poppler_annot, Page **page_out);
+-AnnotStampImageHelper *_poppler_convert_cairo_image_to_stamp_image_helper(cairo_surface_t *image, PDFDoc *doc, GError **error);
++std::unique_ptr<AnnotStampImageHelper> _poppler_convert_cairo_image_to_stamp_image_helper(cairo_surface_t *image, PDFDoc *doc, GError **error);
+
+ /**
+  * SECTION:poppler-annot
+@@ -177,13 +178,12 @@ G_DEFINE_TYPE(PopplerAnnotCircle, poppler_annot_circle, POPPLER_TYPE_ANNOT_MARKU
+ G_DEFINE_TYPE(PopplerAnnotSquare, poppler_annot_square, POPPLER_TYPE_ANNOT_MARKUP)
+ G_DEFINE_TYPE(PopplerAnnotStamp, poppler_annot_stamp, POPPLER_TYPE_ANNOT)
+
+-static PopplerAnnot *_poppler_create_annot(GType annot_type, Annot *annot)
++static PopplerAnnot *_poppler_create_annot(GType annot_type, std::shared_ptr<Annot> annot)
+ {
+     PopplerAnnot *poppler_annot;
+
+     poppler_annot = POPPLER_ANNOT(g_object_new(annot_type, nullptr));
+-    poppler_annot->annot = annot;
+-    annot->incRefCnt();
++    poppler_annot->annot = std::move(annot);
+
+     return poppler_annot;
+ }
+@@ -193,8 +193,7 @@ static void poppler_annot_finalize(GObject *object)
+     PopplerAnnot *poppler_annot = POPPLER_ANNOT(object);
+
+     if (poppler_annot->annot) {
+-        poppler_annot->annot->decRefCnt();
+-        poppler_annot->annot = nullptr;
++        poppler_annot->annot.reset();
+     }
+
+     G_OBJECT_CLASS(poppler_annot_parent_class)->finalize(object);
+@@ -209,7 +208,7 @@ static void poppler_annot_class_init(PopplerAnnotClass *klass)
+     gobject_class->finalize = poppler_annot_finalize;
+ }
+
+-PopplerAnnot *_poppler_annot_new(Annot *annot)
++PopplerAnnot *_poppler_annot_new(const std::shared_ptr<Annot> &annot)
+ {
+     return _poppler_create_annot(POPPLER_TYPE_ANNOT, annot);
+ }
+@@ -222,7 +221,7 @@ static void poppler_annot_text_init(PopplerAnnotText *poppler_annot) { }
+
+ static void poppler_annot_text_class_init(PopplerAnnotTextClass *klass) { }
+
+-PopplerAnnot *_poppler_annot_text_new(Annot *annot)
++PopplerAnnot *_poppler_annot_text_new(const std::shared_ptr<Annot> &annot)
+ {
+     return _poppler_create_annot(POPPLER_TYPE_ANNOT_TEXT, annot);
+ }
+@@ -242,15 +241,14 @@ PopplerAnnot *_poppler_annot_text_new(Annot *annot)
+  */
+ PopplerAnnot *poppler_annot_text_new(PopplerDocument *doc, PopplerRectangle *rect)
+ {
+-    Annot *annot;
+     PDFRectangle pdf_rect(rect->x1, rect->y1, rect->x2, rect->y2);
+
+-    annot = new AnnotText(doc->doc, &pdf_rect);
++    auto annot = std::make_shared<AnnotText>(doc->doc, &pdf_rect);
+
+     return _poppler_annot_text_new(annot);
+ }
+
+-PopplerAnnot *_poppler_annot_text_markup_new(Annot *annot)
++PopplerAnnot *_poppler_annot_text_markup_new(const std::shared_ptr<Annot> &annot)
+ {
+     return _poppler_create_annot(POPPLER_TYPE_ANNOT_TEXT_MARKUP, annot);
+ }
+@@ -323,10 +321,9 @@ static void poppler_annot_text_markup_class_init(PopplerAnnotTextMarkupClass *kl
+ PopplerAnnot *poppler_annot_text_markup_new_highlight(PopplerDocument *doc, PopplerRectangle *rect, GArray *quadrilaterals)
+ {
+     PopplerAnnot *poppler_annot;
+-    AnnotTextMarkup *annot;
+     PDFRectangle pdf_rect(rect->x1, rect->y1, rect->x2, rect->y2);
+
+-    annot = new AnnotTextMarkup(doc->doc, &pdf_rect, Annot::typeHighlight);
++    auto annot = std::make_shared<AnnotTextMarkup>(doc->doc, &pdf_rect, Annot::typeHighlight);
+
+     poppler_annot = _poppler_annot_text_markup_new(annot);
+     poppler_annot_text_markup_set_quadrilaterals(POPPLER_ANNOT_TEXT_MARKUP(poppler_annot), quadrilaterals);
+@@ -350,12 +347,11 @@ PopplerAnnot *poppler_annot_text_markup_new_highlight(PopplerDocument *doc, Popp
+ PopplerAnnot *poppler_annot_text_markup_new_squiggly(PopplerDocument *doc, PopplerRectangle *rect, GArray *quadrilaterals)
+ {
+     PopplerAnnot *poppler_annot;
+-    AnnotTextMarkup *annot;
+     PDFRectangle pdf_rect(rect->x1, rect->y1, rect->x2, rect->y2);
+
+     g_return_val_if_fail(quadrilaterals != nullptr && quadrilaterals->len > 0, NULL);
+
+-    annot = new AnnotTextMarkup(doc->doc, &pdf_rect, Annot::typeSquiggly);
++    auto annot = std::make_shared<AnnotTextMarkup>(doc->doc, &pdf_rect, Annot::typeSquiggly);
+
+     poppler_annot = _poppler_annot_text_markup_new(annot);
+     poppler_annot_text_markup_set_quadrilaterals(POPPLER_ANNOT_TEXT_MARKUP(poppler_annot), quadrilaterals);
+@@ -379,12 +375,11 @@ PopplerAnnot *poppler_annot_text_markup_new_squiggly(PopplerDocument *doc, Poppl
+ PopplerAnnot *poppler_annot_text_markup_new_strikeout(PopplerDocument *doc, PopplerRectangle *rect, GArray *quadrilaterals)
+ {
+     PopplerAnnot *poppler_annot;
+-    AnnotTextMarkup *annot;
+     PDFRectangle pdf_rect(rect->x1, rect->y1, rect->x2, rect->y2);
+
+     g_return_val_if_fail(quadrilaterals != nullptr && quadrilaterals->len > 0, NULL);
+
+-    annot = new AnnotTextMarkup(doc->doc, &pdf_rect, Annot::typeStrikeOut);
++    auto annot = std::make_shared<AnnotTextMarkup>(doc->doc, &pdf_rect, Annot::typeStrikeOut);
+
+     poppler_annot = _poppler_annot_text_markup_new(annot);
+     poppler_annot_text_markup_set_quadrilaterals(POPPLER_ANNOT_TEXT_MARKUP(poppler_annot), quadrilaterals);
+@@ -408,12 +403,11 @@ PopplerAnnot *poppler_annot_text_markup_new_strikeout(PopplerDocument *doc, Popp
+ PopplerAnnot *poppler_annot_text_markup_new_underline(PopplerDocument *doc, PopplerRectangle *rect, GArray *quadrilaterals)
+ {
+     PopplerAnnot *poppler_annot;
+-    AnnotTextMarkup *annot;
+     PDFRectangle pdf_rect(rect->x1, rect->y1, rect->x2, rect->y2);
+
+     g_return_val_if_fail(quadrilaterals != nullptr && quadrilaterals->len > 0, NULL);
+
+-    annot = new AnnotTextMarkup(doc->doc, &pdf_rect, Annot::typeUnderline);
++    auto annot = std::make_shared<AnnotTextMarkup>(doc->doc, &pdf_rect, Annot::typeUnderline);
+
+     poppler_annot = _poppler_annot_text_markup_new(annot);
+     poppler_annot_text_markup_set_quadrilaterals(POPPLER_ANNOT_TEXT_MARKUP(poppler_annot), quadrilaterals);
+@@ -424,7 +418,7 @@ static void poppler_annot_free_text_init(PopplerAnnotFreeText *poppler_annot) {
+
+ static void poppler_annot_free_text_class_init(PopplerAnnotFreeTextClass *klass) { }
+
+-PopplerAnnot *_poppler_annot_free_text_new(Annot *annot)
++PopplerAnnot *_poppler_annot_free_text_new(const std::shared_ptr<Annot> &annot)
+ {
+     return _poppler_create_annot(POPPLER_TYPE_ANNOT_FREE_TEXT, annot);
+ }
+@@ -433,7 +427,7 @@ static void poppler_annot_file_attachment_init(PopplerAnnotFileAttachment *poppl
+
+ static void poppler_annot_file_attachment_class_init(PopplerAnnotFileAttachmentClass *klass) { }
+
+-PopplerAnnot *_poppler_annot_file_attachment_new(Annot *annot)
++PopplerAnnot *_poppler_annot_file_attachment_new(const std::shared_ptr<Annot> &annot)
+ {
+     return _poppler_create_annot(POPPLER_TYPE_ANNOT_FILE_ATTACHMENT, annot);
+ }
+@@ -459,13 +453,13 @@ static void poppler_annot_movie_class_init(PopplerAnnotMovieClass *klass)
+     gobject_class->finalize = poppler_annot_movie_finalize;
+ }
+
+-PopplerAnnot *_poppler_annot_movie_new(Annot *annot)
++PopplerAnnot *_poppler_annot_movie_new(const std::shared_ptr<Annot> &annot)
+ {
+     PopplerAnnot *poppler_annot;
+     AnnotMovie *annot_movie;
+
+     poppler_annot = _poppler_create_annot(POPPLER_TYPE_ANNOT_MOVIE, annot);
+-    annot_movie = static_cast<AnnotMovie *>(poppler_annot->annot);
++    annot_movie = static_cast<AnnotMovie *>(poppler_annot->annot.get());
+     POPPLER_ANNOT_MOVIE(poppler_annot)->movie = _poppler_movie_new(annot_movie->getMovie());
+
+     return poppler_annot;
+@@ -492,14 +486,14 @@ static void poppler_annot_screen_class_init(PopplerAnnotScreenClass *klass)
+     gobject_class->finalize = poppler_annot_screen_finalize;
+ }
+
+-PopplerAnnot *_poppler_annot_screen_new(PopplerDocument *doc, Annot *annot)
++PopplerAnnot *_poppler_annot_screen_new(PopplerDocument *doc, const std::shared_ptr<Annot> &annot)
+ {
+     PopplerAnnot *poppler_annot;
+     AnnotScreen *annot_screen;
+     LinkAction *action;
+
+     poppler_annot = _poppler_create_annot(POPPLER_TYPE_ANNOT_SCREEN, annot);
+-    annot_screen = static_cast<AnnotScreen *>(poppler_annot->annot);
++    annot_screen = static_cast<AnnotScreen *>(poppler_annot->annot.get());
+     action = annot_screen->getAction();
+     if (action) {
+         POPPLER_ANNOT_SCREEN(poppler_annot)->action = _poppler_action_new(doc, action, nullptr);
+@@ -508,7 +502,7 @@ PopplerAnnot *_poppler_annot_screen_new(PopplerDocument *doc, Annot *annot)
+     return poppler_annot;
+ }
+
+-PopplerAnnot *_poppler_annot_line_new(Annot *annot)
++PopplerAnnot *_poppler_annot_line_new(const std::shared_ptr<Annot> &annot)
+ {
+     return _poppler_create_annot(POPPLER_TYPE_ANNOT_LINE, annot);
+ }
+@@ -535,17 +529,16 @@ static void poppler_annot_line_class_init(PopplerAnnotLineClass *klass) { }
+ PopplerAnnot *poppler_annot_line_new(PopplerDocument *doc, PopplerRectangle *rect, PopplerPoint *start, PopplerPoint *end)
+ {
+     PopplerAnnot *poppler_annot;
+-    Annot *annot;
+     PDFRectangle pdf_rect(rect->x1, rect->y1, rect->x2, rect->y2);
+
+-    annot = new AnnotLine(doc->doc, &pdf_rect);
++    auto annot = std::make_shared<AnnotLine>(doc->doc, &pdf_rect);
+
+     poppler_annot = _poppler_annot_line_new(annot);
+     poppler_annot_line_set_vertices(POPPLER_ANNOT_LINE(poppler_annot), start, end);
+     return poppler_annot;
+ }
+
+-PopplerAnnot *_poppler_annot_circle_new(Annot *annot)
++PopplerAnnot *_poppler_annot_circle_new(const std::shared_ptr<Annot> &annot)
+ {
+     return _poppler_create_annot(POPPLER_TYPE_ANNOT_CIRCLE, annot);
+ }
+@@ -569,15 +562,14 @@ static void poppler_annot_circle_class_init(PopplerAnnotCircleClass *klass) { }
+  **/
+ PopplerAnnot *poppler_annot_circle_new(PopplerDocument *doc, PopplerRectangle *rect)
+ {
+-    Annot *annot;
+     PDFRectangle pdf_rect(rect->x1, rect->y1, rect->x2, rect->y2);
+
+-    annot = new AnnotGeometry(doc->doc, &pdf_rect, Annot::typeCircle);
++    auto annot = std::make_shared<AnnotGeometry>(doc->doc, &pdf_rect, Annot::typeCircle);
+
+     return _poppler_annot_circle_new(annot);
+ }
+
+-PopplerAnnot *_poppler_annot_square_new(Annot *annot)
++PopplerAnnot *_poppler_annot_square_new(const std::shared_ptr<Annot> &annot)
+ {
+     return _poppler_create_annot(POPPLER_TYPE_ANNOT_SQUARE, annot);
+ }
+@@ -601,10 +593,9 @@ static void poppler_annot_square_class_init(PopplerAnnotSquareClass *klass) { }
+  **/
+ PopplerAnnot *poppler_annot_square_new(PopplerDocument *doc, PopplerRectangle *rect)
+ {
+-    Annot *annot;
+     PDFRectangle pdf_rect(rect->x1, rect->y1, rect->x2, rect->y2);
+
+-    annot = new AnnotGeometry(doc->doc, &pdf_rect, Annot::typeSquare);
++    auto annot = std::make_shared<AnnotGeometry>(doc->doc, &pdf_rect, Annot::typeSquare);
+
+     return _poppler_annot_square_new(annot);
+ }
+@@ -623,7 +614,7 @@ static void poppler_annot_stamp_class_init(PopplerAnnotStampClass *klass)
+     gobject_class->finalize = poppler_annot_stamp_finalize;
+ }
+
+-PopplerAnnot *_poppler_annot_stamp_new(Annot *annot)
++PopplerAnnot *_poppler_annot_stamp_new(const std::shared_ptr<Annot> &annot)
+ {
+     PopplerAnnot *poppler_annot;
+
+@@ -647,10 +638,9 @@ PopplerAnnot *_poppler_annot_stamp_new(Annot *annot)
+  **/
+ PopplerAnnot *poppler_annot_stamp_new(PopplerDocument *doc, PopplerRectangle *rect)
+ {
+-    Annot *annot;
+     PDFRectangle pdf_rect(rect->x1, rect->y1, rect->x2, rect->y2);
+
+-    annot = new AnnotStamp(doc->doc, &pdf_rect);
++    auto annot = std::make_shared<AnnotStamp>(doc->doc, &pdf_rect);
+
+     return _poppler_annot_stamp_new(annot);
+ }
+@@ -699,9 +689,9 @@ static gboolean get_raw_data_from_cairo_image(cairo_surface_t *image, cairo_form
+     return FALSE;
+ }
+
+-AnnotStampImageHelper *_poppler_convert_cairo_image_to_stamp_image_helper(cairo_surface_t *image, PDFDoc *doc, GError **error)
++std::unique_ptr<AnnotStampImageHelper> _poppler_convert_cairo_image_to_stamp_image_helper(cairo_surface_t *image, PDFDoc *doc, GError **error)
+ {
+-    AnnotStampImageHelper *annotImg;
++    std::unique_ptr<AnnotStampImageHelper> annotImg;
+     GByteArray *data;
+     GByteArray *sMaskData;
+
+@@ -733,9 +723,9 @@ AnnotStampImageHelper *_poppler_convert_cairo_image_to_stamp_image_helper(cairo_
+
+     if (sMaskData->len > 0) {
+         AnnotStampImageHelper sMask(doc, width, height, ColorSpace::DeviceGray, 8, (char *)sMaskData->data, (int)sMaskData->len);
+-        annotImg = new AnnotStampImageHelper(doc, width, height, colorSpace, bitsPerComponent, (char *)data->data, (int)data->len, sMask.getRef());
++        annotImg = std::make_unique<AnnotStampImageHelper>(doc, width, height, colorSpace, bitsPerComponent, (char *)data->data, (int)data->len, sMask.getRef());
+     } else {
+-        annotImg = new AnnotStampImageHelper(doc, width, height, colorSpace, bitsPerComponent, (char *)data->data, (int)data->len);
++        annotImg = std::make_unique<AnnotStampImageHelper>(doc, width, height, colorSpace, bitsPerComponent, (char *)data->data, (int)data->len);
+     }
+
+     g_byte_array_unref(data);
+@@ -1128,7 +1118,7 @@ void poppler_annot_set_rectangle(PopplerAnnot *poppler_annot, PopplerRectangle *
+     if (page && SUPPORTED_ROTATION(page->getRotate())) {
+         /* annot is inside a rotated page, as core poppler rect must be saved
+          * un-rotated, let's proceed to un-rotate rect before saving */
+-        _unrotate_rect_for_annot_and_page(page, poppler_annot->annot, &x1, &y1, &x2, &y2);
++        _unrotate_rect_for_annot_and_page(page, poppler_annot->annot.get(), &x1, &y1, &x2, &y2);
+     }
+
+     poppler_annot->annot->setRect(x1 + crop_box->x1, y1 + crop_box->y1, x2 + crop_box->x1, y2 + crop_box->y1);
+@@ -1150,7 +1140,7 @@ gchar *poppler_annot_markup_get_label(PopplerAnnotMarkup *poppler_annot)
+
+     g_return_val_if_fail(POPPLER_IS_ANNOT_MARKUP(poppler_annot), NULL);
+
+-    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+
+     text = annot->getLabel();
+
+@@ -1174,7 +1164,7 @@ void poppler_annot_markup_set_label(PopplerAnnotMarkup *poppler_annot, const gch
+
+     g_return_if_fail(POPPLER_IS_ANNOT_MARKUP(poppler_annot));
+
+-    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+
+     tmp = label ? g_convert(label, -1, "UTF-16BE", "UTF-8", nullptr, &length, nullptr) : nullptr;
+     annot->setLabel(std::make_unique<GooString>(tmp, length));
+@@ -1197,7 +1187,7 @@ gboolean poppler_annot_markup_has_popup(PopplerAnnotMarkup *poppler_annot)
+
+     g_return_val_if_fail(POPPLER_IS_ANNOT_MARKUP(poppler_annot), FALSE);
+
+-    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+
+     return annot->getPopup() != nullptr;
+ }
+@@ -1219,8 +1209,8 @@ void poppler_annot_markup_set_popup(PopplerAnnotMarkup *poppler_annot, PopplerRe
+
+     g_return_if_fail(POPPLER_IS_ANNOT_MARKUP(poppler_annot));
+
+-    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot);
+-    annot->setPopup(std::make_unique<AnnotPopup>(annot->getDoc(), &pdf_rect));
++    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot.get());
++    annot->setPopup(std::make_shared<AnnotPopup>(annot->getDoc(), &pdf_rect));
+ }
+
+ /**
+@@ -1239,9 +1229,9 @@ gboolean poppler_annot_markup_get_popup_is_open(PopplerAnnotMarkup *poppler_anno
+
+     g_return_val_if_fail(POPPLER_IS_ANNOT_MARKUP(poppler_annot), FALSE);
+
+-    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+
+-    if ((annot_popup = annot->getPopup())) {
++    if ((annot_popup = annot->getPopup().get())) {
+         return annot_popup->getOpen();
+     }
+
+@@ -1264,9 +1254,9 @@ void poppler_annot_markup_set_popup_is_open(PopplerAnnotMarkup *poppler_annot, g
+
+     g_return_if_fail(POPPLER_IS_ANNOT_MARKUP(poppler_annot));
+
+-    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+
+-    annot_popup = annot->getPopup();
++    annot_popup = annot->getPopup().get();
+     if (!annot_popup) {
+         return;
+     }
+@@ -1295,8 +1285,8 @@ gboolean poppler_annot_markup_get_popup_rectangle(PopplerAnnotMarkup *poppler_an
+     g_return_val_if_fail(POPPLER_IS_ANNOT_MARKUP(poppler_annot), FALSE);
+     g_return_val_if_fail(poppler_rect != nullptr, FALSE);
+
+-    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot);
+-    annot_popup = annot->getPopup();
++    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot.get());
++    annot_popup = annot->getPopup().get();
+     if (!annot_popup) {
+         return FALSE;
+     }
+@@ -1330,8 +1320,8 @@ void poppler_annot_markup_set_popup_rectangle(PopplerAnnotMarkup *poppler_annot,
+     g_return_if_fail(POPPLER_IS_ANNOT_MARKUP(poppler_annot));
+     g_return_if_fail(poppler_rect != nullptr);
+
+-    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot);
+-    annot_popup = annot->getPopup();
++    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot.get());
++    annot_popup = annot->getPopup().get();
+     if (!annot_popup) {
+         return;
+     }
+@@ -1354,7 +1344,7 @@ gdouble poppler_annot_markup_get_opacity(PopplerAnnotMarkup *poppler_annot)
+
+     g_return_val_if_fail(POPPLER_IS_ANNOT_MARKUP(poppler_annot), 0);
+
+-    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+
+     return annot->getOpacity();
+ }
+@@ -1376,7 +1366,7 @@ void poppler_annot_markup_set_opacity(PopplerAnnotMarkup *poppler_annot, gdouble
+
+     g_return_if_fail(POPPLER_IS_ANNOT_MARKUP(poppler_annot));
+
+-    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+     annot->setOpacity(opacity);
+ }
+
+@@ -1397,7 +1387,7 @@ GDate *poppler_annot_markup_get_date(PopplerAnnotMarkup *poppler_annot)
+
+     g_return_val_if_fail(POPPLER_IS_ANNOT_MARKUP(poppler_annot), NULL);
+
+-    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+     annot_date = annot->getDate();
+     if (!annot_date) {
+         return nullptr;
+@@ -1430,7 +1420,7 @@ gchar *poppler_annot_markup_get_subject(PopplerAnnotMarkup *poppler_annot)
+
+     g_return_val_if_fail(POPPLER_IS_ANNOT_MARKUP(poppler_annot), NULL);
+
+-    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+
+     text = annot->getSubject();
+
+@@ -1451,7 +1441,7 @@ PopplerAnnotMarkupReplyType poppler_annot_markup_get_reply_to(PopplerAnnotMarkup
+
+     g_return_val_if_fail(POPPLER_IS_ANNOT_MARKUP(poppler_annot), POPPLER_ANNOT_MARKUP_REPLY_TYPE_R);
+
+-    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+
+     switch (annot->getReplyTo()) {
+     case AnnotMarkup::replyTypeR:
+@@ -1479,7 +1469,7 @@ PopplerAnnotExternalDataType poppler_annot_markup_get_external_data(PopplerAnnot
+
+     g_return_val_if_fail(POPPLER_IS_ANNOT_MARKUP(poppler_annot), POPPLER_ANNOT_EXTERNAL_DATA_MARKUP_UNKNOWN);
+
+-    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+
+     switch (annot->getExData()) {
+     case annotExternalDataMarkup3D:
+@@ -1509,7 +1499,7 @@ gboolean poppler_annot_text_get_is_open(PopplerAnnotText *poppler_annot)
+
+     g_return_val_if_fail(POPPLER_IS_ANNOT_TEXT(poppler_annot), FALSE);
+
+-    annot = static_cast<AnnotText *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotText *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+
+     return annot->getOpen();
+ }
+@@ -1529,7 +1519,7 @@ void poppler_annot_text_set_is_open(PopplerAnnotText *poppler_annot, gboolean is
+
+     g_return_if_fail(POPPLER_IS_ANNOT_TEXT(poppler_annot));
+
+-    annot = static_cast<AnnotText *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotText *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+     annot->setOpen(is_open);
+ }
+
+@@ -1548,7 +1538,7 @@ gchar *poppler_annot_text_get_icon(PopplerAnnotText *poppler_annot)
+
+     g_return_val_if_fail(POPPLER_IS_ANNOT_TEXT(poppler_annot), NULL);
+
+-    annot = static_cast<AnnotText *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotText *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+
+     text = annot->getIcon();
+
+@@ -1601,7 +1591,7 @@ void poppler_annot_text_set_icon(PopplerAnnotText *poppler_annot, const gchar *i
+
+     g_return_if_fail(POPPLER_IS_ANNOT_TEXT(poppler_annot));
+
+-    annot = static_cast<AnnotText *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotText *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+
+     text = new GooString(icon);
+     annot->setIcon(text);
+@@ -1622,7 +1612,7 @@ PopplerAnnotTextState poppler_annot_text_get_state(PopplerAnnotText *poppler_ann
+
+     g_return_val_if_fail(POPPLER_IS_ANNOT_TEXT(poppler_annot), POPPLER_ANNOT_TEXT_STATE_UNKNOWN);
+
+-    annot = static_cast<AnnotText *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotText *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+
+     switch (annot->getState()) {
+     case AnnotText::stateUnknown:
+@@ -1669,7 +1659,7 @@ void poppler_annot_text_markup_set_quadrilaterals(PopplerAnnotTextMarkup *popple
+     g_return_if_fail(POPPLER_IS_ANNOT_TEXT_MARKUP(poppler_annot));
+     g_return_if_fail(quadrilaterals != nullptr && quadrilaterals->len > 0);
+
+-    annot = static_cast<AnnotTextMarkup *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotTextMarkup *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+     crop_box = _poppler_annot_get_cropbox_and_page(POPPLER_ANNOT(poppler_annot), &page);
+     quads = create_annot_quads_from_poppler_quads(quadrilaterals);
+
+@@ -1708,7 +1698,7 @@ GArray *poppler_annot_text_markup_get_quadrilaterals(PopplerAnnotTextMarkup *pop
+
+     g_return_val_if_fail(POPPLER_IS_ANNOT_TEXT_MARKUP(poppler_annot), NULL);
+
+-    annot = static_cast<AnnotTextMarkup *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotTextMarkup *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+     crop_box = _poppler_annot_get_cropbox(POPPLER_ANNOT(poppler_annot));
+     AnnotQuadrilaterals *quads = annot->getQuadrilaterals();
+
+@@ -1730,7 +1720,7 @@ PopplerAnnotFreeTextQuadding poppler_annot_free_text_get_quadding(PopplerAnnotFr
+
+     g_return_val_if_fail(POPPLER_IS_ANNOT_FREE_TEXT(poppler_annot), POPPLER_ANNOT_FREE_TEXT_QUADDING_LEFT_JUSTIFIED);
+
+-    annot = static_cast<AnnotFreeText *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotFreeText *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+
+     switch (annot->getQuadding()) {
+     case VariableTextQuadding::leftJustified:
+@@ -1764,7 +1754,7 @@ PopplerAnnotCalloutLine *poppler_annot_free_text_get_callout_line(PopplerAnnotFr
+
+     g_return_val_if_fail(POPPLER_IS_ANNOT_FREE_TEXT(poppler_annot), NULL);
+
+-    annot = static_cast<AnnotFreeText *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotFreeText *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+
+     if ((line = annot->getCalloutLine())) {
+         AnnotCalloutMultiLine *multiline;
+@@ -1808,11 +1798,10 @@ PopplerAttachment *poppler_annot_file_attachment_get_attachment(PopplerAnnotFile
+
+     g_return_val_if_fail(POPPLER_IS_ANNOT_FILE_ATTACHMENT(poppler_annot), NULL);
+
+-    annot = static_cast<AnnotFileAttachment *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotFileAttachment *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+
+-    FileSpec *file = new FileSpec(annot->getFile());
+-    attachment = _poppler_attachment_new(file);
+-    delete file;
++    FileSpec file { annot->getFile() };
++    attachment = _poppler_attachment_new(&file);
+
+     return attachment;
+ }
+@@ -1834,7 +1823,7 @@ gchar *poppler_annot_file_attachment_get_name(PopplerAnnotFileAttachment *popple
+
+     g_return_val_if_fail(POPPLER_IS_ANNOT_FILE_ATTACHMENT(poppler_annot), NULL);
+
+-    annot = static_cast<AnnotFileAttachment *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotFileAttachment *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+     name = annot->getName();
+
+     return name ? _poppler_goo_string_to_utf8(name) : nullptr;
+@@ -1906,7 +1895,7 @@ gchar *poppler_annot_movie_get_title(PopplerAnnotMovie *poppler_annot)
+
+     g_return_val_if_fail(POPPLER_IS_ANNOT_MOVIE(poppler_annot), NULL);
+
+-    annot = static_cast<AnnotMovie *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotMovie *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+
+     title = annot->getTitle();
+
+@@ -1965,7 +1954,7 @@ void poppler_annot_line_set_vertices(PopplerAnnotLine *poppler_annot, PopplerPoi
+     g_return_if_fail(start != nullptr);
+     g_return_if_fail(end != nullptr);
+
+-    annot = static_cast<AnnotLine *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotLine *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+     annot->setVertices(start->x, start->y, end->x, end->y);
+ }
+
+@@ -1974,7 +1963,7 @@ static PopplerColor *poppler_annot_geometry_get_interior_color(PopplerAnnot *pop
+ {
+     AnnotGeometry *annot;
+
+-    annot = static_cast<AnnotGeometry *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotGeometry *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+
+     return create_poppler_color_from_annot_color(annot->getInteriorColor());
+ }
+@@ -1983,7 +1972,7 @@ static void poppler_annot_geometry_set_interior_color(PopplerAnnot *poppler_anno
+ {
+     AnnotGeometry *annot;
+
+-    annot = static_cast<AnnotGeometry *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotGeometry *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+
+     annot->setInteriorColor(create_annot_color_from_poppler_color(poppler_color));
+ }
+@@ -2073,7 +2062,7 @@ PopplerAnnotStampIcon poppler_annot_stamp_get_icon(PopplerAnnotStamp *poppler_an
+
+     g_return_val_if_fail(POPPLER_IS_ANNOT_STAMP(poppler_annot), POPPLER_ANNOT_STAMP_ICON_UNKNOWN);
+
+-    annot = static_cast<AnnotStamp *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotStamp *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+
+     text = annot->getIcon();
+
+@@ -2129,7 +2118,7 @@ void poppler_annot_stamp_set_icon(PopplerAnnotStamp *poppler_annot, PopplerAnnot
+
+     g_return_if_fail(POPPLER_IS_ANNOT_STAMP(poppler_annot));
+
+-    annot = static_cast<AnnotStamp *>(POPPLER_ANNOT(poppler_annot)->annot);
++    annot = static_cast<AnnotStamp *>(POPPLER_ANNOT(poppler_annot)->annot.get());
+
+     if (icon == POPPLER_ANNOT_STAMP_ICON_NONE) {
+         annot->setIcon(nullptr);
+@@ -2186,16 +2175,15 @@ void poppler_annot_stamp_set_icon(PopplerAnnotStamp *poppler_annot, PopplerAnnot
+ gboolean poppler_annot_stamp_set_custom_image(PopplerAnnotStamp *poppler_annot, cairo_surface_t *image, GError **error)
+ {
+     AnnotStamp *annot;
+-    AnnotStampImageHelper *annot_image_helper;
+
+     g_return_val_if_fail(POPPLER_IS_ANNOT_STAMP(poppler_annot), FALSE);
+
+-    annot = static_cast<AnnotStamp *>(POPPLER_ANNOT(poppler_annot)->annot);
+-    annot_image_helper = _poppler_convert_cairo_image_to_stamp_image_helper(image, annot->getDoc(), error);
++    annot = static_cast<AnnotStamp *>(POPPLER_ANNOT(poppler_annot)->annot.get());
++    std::unique_ptr<AnnotStampImageHelper> annot_image_helper = _poppler_convert_cairo_image_to_stamp_image_helper(image, annot->getDoc(), error);
+     if (!annot_image_helper) {
+         return FALSE;
+     }
+-    annot->setCustomImage(annot_image_helper);
++    annot->setCustomImage(std::move(annot_image_helper));
+
+     return TRUE;
+ }
+diff --git a/glib/poppler-page.cc b/glib/poppler-page.cc
+index 7f47cd5..1ccc423 100644
+--- a/glib/poppler-page.cc
++++ b/glib/poppler-page.cc
+@@ -1132,7 +1132,7 @@ GList *poppler_page_get_link_mapping(PopplerPage *page)
+
+     poppler_page_get_size(page, &width, &height);
+
+-    for (AnnotLink *link : links->getLinks()) {
++    for (const std::shared_ptr<AnnotLink> &link : links->getLinks()) {
+         PopplerLinkMapping *mapping;
+         PopplerRectangle rect;
+         LinkAction *link_action;
+@@ -1295,7 +1295,7 @@ GList *poppler_page_get_annot_mapping(PopplerPage *page)
+     poppler_page_get_size(page, &width, &height);
+     crop_box = page->page->getCropBox();
+
+-    for (Annot *annot : annots->getAnnots()) {
++    for (const std::shared_ptr<Annot> &annot : annots->getAnnots()) {
+         PopplerAnnotMapping *mapping;
+         PopplerRectangle rect;
+         gboolean flag_no_rotate;
+@@ -1572,12 +1572,12 @@ void poppler_page_add_annot(PopplerPage *page, PopplerAnnot *annot)
+     if (page_is_rotated) {
+         /* annot is inside a rotated page, as core poppler rect must be saved
+          * un-rotated, let's proceed to un-rotate rect before saving */
+-        _unrotate_rect_for_annot_and_page(page->page, annot->annot, &x1, &y1, &x2, &y2);
++        _unrotate_rect_for_annot_and_page(page->page, annot->annot.get(), &x1, &y1, &x2, &y2);
+     }
+
+     annot->annot->setRect(x1 + page_crop_box->x1, y1 + page_crop_box->y1, x2 + page_crop_box->x1, y2 + page_crop_box->y1);
+
+-    AnnotTextMarkup *annot_markup = dynamic_cast<AnnotTextMarkup *>(annot->annot);
++    AnnotTextMarkup *annot_markup = dynamic_cast<AnnotTextMarkup *>(annot->annot.get());
+     if (annot_markup) {
+         AnnotQuadrilaterals *quads;
+         crop_box = _poppler_annot_get_cropbox(annot);
+diff --git a/glib/poppler-private.h b/glib/poppler-private.h
+index 758c702..1b51b76 100644
+--- a/glib/poppler-private.h
++++ b/glib/poppler-private.h
+@@ -85,7 +85,7 @@ struct _PopplerFormField
+ struct _PopplerAnnot
+ {
+     GObject parent_instance;
+-    Annot *annot;
++    std::shared_ptr<Annot> annot;
+ };
+
+ typedef struct _Layer
+@@ -146,17 +146,17 @@ PopplerFormField *_poppler_form_field_new(PopplerDocument *document, FormWidget
+ PopplerAttachment *_poppler_attachment_new(FileSpec *file);
+ PopplerMovie *_poppler_movie_new(const Movie *movie);
+ PopplerMedia *_poppler_media_new(const MediaRendition *media);
+-PopplerAnnot *_poppler_annot_new(Annot *annot);
+-PopplerAnnot *_poppler_annot_text_new(Annot *annot);
+-PopplerAnnot *_poppler_annot_free_text_new(Annot *annot);
+-PopplerAnnot *_poppler_annot_text_markup_new(Annot *annot);
+-PopplerAnnot *_poppler_annot_file_attachment_new(Annot *annot);
+-PopplerAnnot *_poppler_annot_movie_new(Annot *annot);
+-PopplerAnnot *_poppler_annot_screen_new(PopplerDocument *doc, Annot *annot);
+-PopplerAnnot *_poppler_annot_line_new(Annot *annot);
+-PopplerAnnot *_poppler_annot_circle_new(Annot *annot);
+-PopplerAnnot *_poppler_annot_square_new(Annot *annot);
+-PopplerAnnot *_poppler_annot_stamp_new(Annot *annot);
++PopplerAnnot *_poppler_annot_new(const std::shared_ptr<Annot> &annot);
++PopplerAnnot *_poppler_annot_text_new(const std::shared_ptr<Annot> &annot);
++PopplerAnnot *_poppler_annot_free_text_new(const std::shared_ptr<Annot> &annot);
++PopplerAnnot *_poppler_annot_text_markup_new(const std::shared_ptr<Annot> &annot);
++PopplerAnnot *_poppler_annot_file_attachment_new(const std::shared_ptr<Annot> &annot);
++PopplerAnnot *_poppler_annot_movie_new(const std::shared_ptr<Annot> &annot);
++PopplerAnnot *_poppler_annot_screen_new(PopplerDocument *doc, const std::shared_ptr<Annot> &annot);
++PopplerAnnot *_poppler_annot_line_new(const std::shared_ptr<Annot> &annot);
++PopplerAnnot *_poppler_annot_circle_new(const std::shared_ptr<Annot> &annot);
++PopplerAnnot *_poppler_annot_square_new(const std::shared_ptr<Annot> &annot);
++PopplerAnnot *_poppler_annot_stamp_new(const std::shared_ptr<Annot> &annot);
+
+ const PDFRectangle *_poppler_annot_get_cropbox(PopplerAnnot *poppler_annot);
+
+diff --git a/poppler/Annot.cc b/poppler/Annot.cc
+index 5716ea5..b98df5d 100644
+--- a/poppler/Annot.cc
++++ b/poppler/Annot.cc
+@@ -1064,7 +1064,7 @@ void AnnotAppearance::removeStream(Ref refToStream)
+             continue;
+         }
+         Annots *annots = page->getAnnots();
+-        for (Annot *annot : annots->getAnnots()) {
++        for (const std::shared_ptr<Annot> &annot : annots->getAnnots()) {
+             AnnotAppearance *annotAp = annot->getAppearStreams();
+             if (annotAp && annotAp != this && annotAp->referencesStream(refToStream)) {
+                 return; // Another annotation points to the stream -> Don't delete it
+@@ -1263,7 +1263,6 @@ double AnnotAppearanceBBox::getPageYMax() const
+ Annot::Annot(PDFDoc *docA, PDFRectangle *rectA)
+ {
+
+-    refCnt = 1;
+     flags = flagUnknown;
+     type = typeUnknown;
+
+@@ -1284,7 +1283,6 @@ Annot::Annot(PDFDoc *docA, PDFRectangle *rectA)
+
+ Annot::Annot(PDFDoc *docA, Object &&dictObject)
+ {
+-    refCnt = 1;
+     hasRef = false;
+     flags = flagUnknown;
+     type = typeUnknown;
+@@ -1294,7 +1292,6 @@ Annot::Annot(PDFDoc *docA, Object &&dictObject)
+
+ Annot::Annot(PDFDoc *docA, Object &&dictObject, const Object *obj)
+ {
+-    refCnt = 1;
+     if (obj->isRef()) {
+         hasRef = true;
+         ref = obj->getRef();
+@@ -1670,18 +1667,6 @@ void Annot::removeReferencedObjects()
+     invalidateAppearance();
+ }
+
+-void Annot::incRefCnt()
+-{
+-    refCnt++;
+-}
+-
+-void Annot::decRefCnt()
+-{
+-    if (--refCnt == 0) {
+-        delete this;
+-    }
+-}
+-
+ Annot::~Annot() { }
+
+ void AnnotAppearanceBuilder::setDrawColor(const AnnotColor *drawColor, bool fill)
+@@ -2218,7 +2203,7 @@ void AnnotMarkup::setLabel(std::unique_ptr<GooString> &&new_label)
+     update("T", Object(label->copy()));
+ }
+
+-void AnnotMarkup::setPopup(std::unique_ptr<AnnotPopup> &&new_popup)
++void AnnotMarkup::setPopup(std::shared_ptr<AnnotPopup> new_popup)
+ {
+     // If there exists an old popup annotation that is already
+     // associated with a page, then we need to remove that
+@@ -2227,7 +2212,7 @@ void AnnotMarkup::setPopup(std::unique_ptr<AnnotPopup> &&new_popup)
+     if (popup && popup->getPageNum() != 0) {
+         Page *pageobj = doc->getPage(popup->getPageNum());
+         if (pageobj) {
+-            pageobj->removeAnnot(popup.get());
++            pageobj->removeAnnot(popup);
+         }
+     }
+
+@@ -2244,7 +2229,7 @@ void AnnotMarkup::setPopup(std::unique_ptr<AnnotPopup> &&new_popup)
+             Page *pageobj = doc->getPage(page);
+             assert(pageobj != nullptr); // pageobj should exist in doc (see setPage())
+
+-            pageobj->addAnnot(popup.get());
++            pageobj->addAnnot(popup);
+         }
+     } else {
+         popup = nullptr;
+@@ -2276,7 +2261,7 @@ void AnnotMarkup::removeReferencedObjects()
+
+     // Remove popup
+     if (popup) {
+-        pageobj->removeAnnot(popup.get());
++        pageobj->removeAnnot(popup);
+     }
+
+     Annot::removeReferencedObjects();
+@@ -2943,7 +2928,7 @@ void AnnotFreeText::setStyleString(GooString *new_string)
+     update("DS", Object(styleString->copy()));
+ }
+
+-void AnnotFreeText::setCalloutLine(AnnotCalloutLine *line)
++void AnnotFreeText::setCalloutLine(std::unique_ptr<AnnotCalloutLine> &&line)
+ {
+     Object obj1;
+     if (line == nullptr) {
+@@ -2958,15 +2943,13 @@ void AnnotFreeText::setCalloutLine(AnnotCalloutLine *line)
+         obj1.arrayAdd(Object(x2));
+         obj1.arrayAdd(Object(y2));
+
+-        AnnotCalloutMultiLine *mline = dynamic_cast<AnnotCalloutMultiLine *>(line);
++        AnnotCalloutMultiLine *mline = dynamic_cast<AnnotCalloutMultiLine *>(line.get());
+         if (mline) {
+             double x3 = mline->getX3(), y3 = mline->getY3();
+             obj1.arrayAdd(Object(x3));
+             obj1.arrayAdd(Object(y3));
+-            calloutLine = std::make_unique<AnnotCalloutMultiLine>(x1, y1, x2, y2, x3, y3);
+-        } else {
+-            calloutLine = std::make_unique<AnnotCalloutLine>(x1, y1, x2, y2);
+         }
++        calloutLine = std::move(line);
+     }
+
+     update("CL", std::move(obj1));
+@@ -5715,10 +5698,7 @@ AnnotStamp::AnnotStamp(PDFDoc *docA, Object &&dictObject, const Object *obj) : A
+     initialize(docA, annotObj.getDict());
+ }
+
+-AnnotStamp::~AnnotStamp()
+-{
+-    delete stampImageHelper;
+-}
++AnnotStamp::~AnnotStamp() = default;
+
+ void AnnotStamp::initialize(PDFDoc *docA, Dict *dict)
+ {
+@@ -5729,7 +5709,6 @@ void AnnotStamp::initialize(PDFDoc *docA, Dict *dict)
+         icon = std::make_unique<GooString>("Draft");
+     }
+
+-    stampImageHelper = nullptr;
+     updatedAppearanceStream = Ref::INVALID();
+ }
+
+@@ -5887,16 +5866,18 @@ void AnnotStamp::setIcon(GooString *new_icon)
+     invalidateAppearance();
+ }
+
+-void AnnotStamp::setCustomImage(AnnotStampImageHelper *stampImageHelperA)
++void AnnotStamp::setCustomImage(std::unique_ptr<AnnotStampImageHelper> &&stampImageHelperA)
+ {
+     if (!stampImageHelperA) {
+         return;
+     }
+
+     annotLocker();
+-    clearCustomImage();
++    if (stampImageHelper) {
++        stampImageHelper->removeAnnotStampImageObject();
++    }
+
+-    stampImageHelper = stampImageHelperA;
++    stampImageHelper = std::move(stampImageHelperA);
+     generateStampCustomAppearance();
+
+     if (updatedAppearanceStream == Ref::INVALID()) {
+@@ -5911,16 +5892,6 @@ void AnnotStamp::setCustomImage(AnnotStampImageHelper *stampImageHelperA)
+     update("AP", std::move(obj1));
+ }
+
+-void AnnotStamp::clearCustomImage()
+-{
+-    if (stampImageHelper != nullptr) {
+-        stampImageHelper->removeAnnotStampImageObject();
+-        delete stampImageHelper;
+-        stampImageHelper = nullptr;
+-        invalidateAppearance();
+-    }
+-}
+-
+ //------------------------------------------------------------------------
+ // AnnotGeometry
+ //------------------------------------------------------------------------
+@@ -7467,96 +7438,90 @@ const GooString *AnnotRichMedia::Params::getFlashVars() const
+
+ Annots::Annots(PDFDoc *docA, int page, Object *annotsObj)
+ {
+-    Annot *annot;
+-    int i;
+-
+     doc = docA;
+
+     if (annotsObj->isArray()) {
+-        for (i = 0; i < annotsObj->arrayGetLength(); ++i) {
++        for (int i = 0; i < annotsObj->arrayGetLength(); ++i) {
+             // get the Ref to this annot and pass it to Annot constructor
+             // this way, it'll be possible for the annot to retrieve the corresponding
+             // form widget
+             Object obj1 = annotsObj->arrayGet(i);
+             if (obj1.isDict()) {
+                 const Object &obj2 = annotsObj->arrayGetNF(i);
+-                annot = createAnnot(std::move(obj1), &obj2);
++                std::shared_ptr<Annot> annot = createAnnot(std::move(obj1), &obj2);
+                 if (annot) {
+                     if (annot->isOk()) {
+                         annot->setPage(page, false); // Don't change /P
+                         appendAnnot(annot);
+                     }
+-                    annot->decRefCnt();
+                 }
+             }
+         }
+     }
+ }
+
+-void Annots::appendAnnot(Annot *annot)
++void Annots::appendAnnot(std::shared_ptr<Annot> annot)
+ {
+     if (annot && annot->isOk()) {
+-        annots.push_back(annot);
+-        annot->incRefCnt();
++        annots.push_back(std::move(annot));
+     }
+ }
+
+-bool Annots::removeAnnot(Annot *annot)
++bool Annots::removeAnnot(const std::shared_ptr<Annot> &annot)
+ {
+     auto idx = std::find(annots.begin(), annots.end(), annot);
+
+     if (idx == annots.end()) {
+         return false;
+     } else {
+-        annot->decRefCnt();
+         annots.erase(idx);
+         return true;
+     }
+ }
+
+-Annot *Annots::createAnnot(Object &&dictObject, const Object *obj)
++std::shared_ptr<Annot> Annots::createAnnot(Object &&dictObject, const Object *obj)
+ {
+-    Annot *annot = nullptr;
++    std::shared_ptr<Annot> annot = nullptr;
+     Object obj1 = dictObject.dictLookup("Subtype");
+     if (obj1.isName()) {
+         const char *typeName = obj1.getName();
+
+         if (!strcmp(typeName, "Text")) {
+-            annot = new AnnotText(doc, std::move(dictObject), obj);
++            annot = std::make_shared<AnnotText>(doc, std::move(dictObject), obj);
+         } else if (!strcmp(typeName, "Link")) {
+-            annot = new AnnotLink(doc, std::move(dictObject), obj);
++            annot = std::make_shared<AnnotLink>(doc, std::move(dictObject), obj);
+         } else if (!strcmp(typeName, "FreeText")) {
+-            annot = new AnnotFreeText(doc, std::move(dictObject), obj);
++            annot = std::make_shared<AnnotFreeText>(doc, std::move(dictObject), obj);
+         } else if (!strcmp(typeName, "Line")) {
+-            annot = new AnnotLine(doc, std::move(dictObject), obj);
++            annot = std::make_shared<AnnotLine>(doc, std::move(dictObject), obj);
+         } else if (!strcmp(typeName, "Square")) {
+-            annot = new AnnotGeometry(doc, std::move(dictObject), obj);
++            annot = std::make_shared<AnnotGeometry>(doc, std::move(dictObject), obj);
+         } else if (!strcmp(typeName, "Circle")) {
+-            annot = new AnnotGeometry(doc, std::move(dictObject), obj);
++            annot = std::make_shared<AnnotGeometry>(doc, std::move(dictObject), obj);
+         } else if (!strcmp(typeName, "Polygon")) {
+-            annot = new AnnotPolygon(doc, std::move(dictObject), obj);
++            annot = std::make_shared<AnnotPolygon>(doc, std::move(dictObject), obj);
+         } else if (!strcmp(typeName, "PolyLine")) {
+-            annot = new AnnotPolygon(doc, std::move(dictObject), obj);
++            annot = std::make_shared<AnnotPolygon>(doc, std::move(dictObject), obj);
+         } else if (!strcmp(typeName, "Highlight")) {
+-            annot = new AnnotTextMarkup(doc, std::move(dictObject), obj);
++            annot = std::make_shared<AnnotTextMarkup>(doc, std::move(dictObject), obj);
+         } else if (!strcmp(typeName, "Underline")) {
+-            annot = new AnnotTextMarkup(doc, std::move(dictObject), obj);
++            annot = std::make_shared<AnnotTextMarkup>(doc, std::move(dictObject), obj);
+         } else if (!strcmp(typeName, "Squiggly")) {
+-            annot = new AnnotTextMarkup(doc, std::move(dictObject), obj);
++            annot = std::make_shared<AnnotTextMarkup>(doc, std::move(dictObject), obj);
+         } else if (!strcmp(typeName, "StrikeOut")) {
+-            annot = new AnnotTextMarkup(doc, std::move(dictObject), obj);
++            annot = std::make_shared<AnnotTextMarkup>(doc, std::move(dictObject), obj);
+         } else if (!strcmp(typeName, "Stamp")) {
+-            annot = new AnnotStamp(doc, std::move(dictObject), obj);
++            annot = std::make_shared<AnnotStamp>(doc, std::move(dictObject), obj);
+         } else if (!strcmp(typeName, "Caret")) {
+-            annot = new AnnotCaret(doc, std::move(dictObject), obj);
++            annot = std::make_shared<AnnotCaret>(doc, std::move(dictObject), obj);
+         } else if (!strcmp(typeName, "Ink")) {
+-            annot = new AnnotInk(doc, std::move(dictObject), obj);
++            annot = std::make_shared<AnnotInk>(doc, std::move(dictObject), obj);
+         } else if (!strcmp(typeName, "FileAttachment")) {
+-            annot = new AnnotFileAttachment(doc, std::move(dictObject), obj);
++            annot = std::make_shared<AnnotFileAttachment>(doc, std::move(dictObject), obj);
+         } else if (!strcmp(typeName, "Sound")) {
+-            annot = new AnnotSound(doc, std::move(dictObject), obj);
++            annot = std::make_shared<AnnotSound>(doc, std::move(dictObject), obj);
+         } else if (!strcmp(typeName, "Movie")) {
+-            annot = new AnnotMovie(doc, std::move(dictObject), obj);
++            annot = std::make_shared<AnnotMovie>(doc, std::move(dictObject), obj);
+         } else if (!strcmp(typeName, "Widget")) {
+             // Find the annot in forms
+             if (obj->isRef()) {
+@@ -7565,25 +7530,24 @@ Annot *Annots::createAnnot(Object &&dictObject, const Object *obj)
+                     FormWidget *widget = form->findWidgetByRef(obj->getRef());
+                     if (widget) {
+                         annot = widget->getWidgetAnnotation();
+-                        annot->incRefCnt();
+                     }
+                 }
+             }
+             if (!annot) {
+-                annot = new AnnotWidget(doc, std::move(dictObject), obj);
++                annot = std::make_shared<AnnotWidget>(doc, std::move(dictObject), obj);
+             }
+         } else if (!strcmp(typeName, "Screen")) {
+-            annot = new AnnotScreen(doc, std::move(dictObject), obj);
++            annot = std::make_shared<AnnotScreen>(doc, std::move(dictObject), obj);
+         } else if (!strcmp(typeName, "PrinterMark")) {
+-            annot = new Annot(doc, std::move(dictObject), obj);
++            annot = std::make_shared<Annot>(doc, std::move(dictObject), obj);
+         } else if (!strcmp(typeName, "TrapNet")) {
+-            annot = new Annot(doc, std::move(dictObject), obj);
++            annot = std::make_shared<Annot>(doc, std::move(dictObject), obj);
+         } else if (!strcmp(typeName, "Watermark")) {
+-            annot = new Annot(doc, std::move(dictObject), obj);
++            annot = std::make_shared<Annot>(doc, std::move(dictObject), obj);
+         } else if (!strcmp(typeName, "3D")) {
+-            annot = new Annot3D(doc, std::move(dictObject), obj);
++            annot = std::make_shared<Annot3D>(doc, std::move(dictObject), obj);
+         } else if (!strcmp(typeName, "RichMedia")) {
+-            annot = new AnnotRichMedia(doc, std::move(dictObject), obj);
++            annot = std::make_shared<AnnotRichMedia>(doc, std::move(dictObject), obj);
+         } else if (!strcmp(typeName, "Popup")) {
+             /* Popup annots are already handled by markup annots
+              * Here we only care about popup annots without a
+@@ -7591,21 +7555,21 @@ Annot *Annots::createAnnot(Object &&dictObject, const Object *obj)
+              */
+             Object obj2 = dictObject.dictLookup("Parent");
+             if (obj2.isNull()) {
+-                annot = new AnnotPopup(doc, std::move(dictObject), obj);
++                annot = std::make_shared<AnnotPopup>(doc, std::move(dictObject), obj);
+             } else {
+                 annot = nullptr;
+             }
+         } else {
+-            annot = new Annot(doc, std::move(dictObject), obj);
++            annot = std::make_shared<Annot>(doc, std::move(dictObject), obj);
+         }
+     }
+
+     return annot;
+ }
+
+-Annot *Annots::findAnnot(Ref *ref)
++std::shared_ptr<Annot> Annots::findAnnot(Ref *ref)
+ {
+-    for (auto *annot : annots) {
++    for (const auto &annot : annots) {
+         if (annot->match(ref)) {
+             return annot;
+         }
+@@ -7613,12 +7577,7 @@ Annot *Annots::findAnnot(Ref *ref)
+     return nullptr;
+ }
+
+-Annots::~Annots()
+-{
+-    for (auto *annot : annots) {
+-        annot->decRefCnt();
+-    }
+-}
++Annots::~Annots() = default;
+
+ //------------------------------------------------------------------------
+ // AnnotAppearanceBuilder
+diff --git a/poppler/Annot.h b/poppler/Annot.h
+index 819877d..ad40d5e 100644
+--- a/poppler/Annot.h
++++ b/poppler/Annot.h
+@@ -716,9 +716,6 @@ public:
+     Annot(PDFDoc *docA, Object &&dictObject, const Object *obj);
+     bool isOk() { return ok; }
+
+-    void incRefCnt();
+-    void decRefCnt();
+-
+     virtual void draw(Gfx *gfx, bool printing);
+     // Get the resource dict of the appearance stream
+     virtual Object getAppearanceResDict();
+@@ -774,6 +771,8 @@ public:
+     // If newFontNeeded is not null, it will contain whether the given font has glyphs to represent the needed text
+     static void layoutText(const GooString *text, GooString *outBuf, int *i, const GfxFont &font, double *width, double widthLimit, int *charCount, bool noReencode, bool *newFontNeeded = nullptr);
+
++    virtual ~Annot();
++
+ private:
+     void readArrayNum(Object *pdfArray, int key, double *value);
+     // write vStr[i:j[ in appearBuf
+@@ -782,7 +781,6 @@ private:
+     void setPage(int pageIndex, bool updateP); // Called by Page::addAnnot and Annots ctor
+
+ protected:
+-    virtual ~Annot();
+     virtual void removeReferencedObjects(); // Called by Page::removeAnnot
+     Object createForm(const GooString *appearBuf, const double *bbox, bool transparencyGroup, Dict *resDict);
+     Object createForm(const GooString *appearBuf, const double *bbox, bool transparencyGroup, Object &&resDictObject); // overload to support incRef/decRef
+@@ -799,8 +797,6 @@ protected:
+
+     Object annotObj;
+
+-    std::atomic_int refCnt;
+-
+     // required data
+     AnnotSubtype type; // Annotation type
+     std::unique_ptr<PDFRectangle> rect; // Rect
+@@ -873,7 +869,7 @@ public:
+
+     // getters
+     const GooString *getLabel() const { return label.get(); }
+-    AnnotPopup *getPopup() const { return popup.get(); }
++    std::shared_ptr<AnnotPopup> getPopup() const { return popup; }
+     double getOpacity() const { return opacity; }
+     // getRC
+     const GooString *getDate() const { return date.get(); }
+@@ -884,7 +880,7 @@ public:
+     AnnotExternalDataType getExData() const { return exData; }
+
+     // The annotation takes the ownership of new_popup
+-    void setPopup(std::unique_ptr<AnnotPopup> &&new_popup);
++    void setPopup(std::shared_ptr<AnnotPopup> new_popup);
+     void setLabel(std::unique_ptr<GooString> &&new_label);
+     void setOpacity(double opacityA);
+     void setDate(GooString *new_date);
+@@ -893,7 +889,7 @@ protected:
+     void removeReferencedObjects() override;
+
+     std::unique_ptr<GooString> label; // T            (Default author)
+-    std::unique_ptr<AnnotPopup> popup; // Popup
++    std::shared_ptr<AnnotPopup> popup; // Popup
+     double opacity; // CA           (Default 1.0)
+     // RC
+     std::unique_ptr<GooString> date; // CreationDate
+@@ -1068,7 +1064,7 @@ public:
+     void setDefaultAppearance(const DefaultAppearance &da);
+     void setQuadding(VariableTextQuadding new_quadding);
+     void setStyleString(GooString *new_string);
+-    void setCalloutLine(AnnotCalloutLine *line);
++    void setCalloutLine(std::unique_ptr<AnnotCalloutLine> &&line);
+     void setIntent(AnnotFreeTextIntent new_intent);
+
+     // getters
+@@ -1224,9 +1220,7 @@ public:
+
+     void setIcon(GooString *new_icon);
+
+-    void setCustomImage(AnnotStampImageHelper *stampImageHelperA);
+-
+-    void clearCustomImage();
++    void setCustomImage(std::unique_ptr<AnnotStampImageHelper> &&stampImageHelperA);
+
+     // getters
+     const GooString *getIcon() const { return icon.get(); }
+@@ -1237,7 +1231,7 @@ private:
+     void generateStampCustomAppearance();
+
+     std::unique_ptr<GooString> icon; // Name       (Default Draft)
+-    AnnotStampImageHelper *stampImageHelper;
++    std::unique_ptr<AnnotStampImageHelper> stampImageHelper;
+     Ref updatedAppearanceStream;
+ };
+
+@@ -1768,17 +1762,17 @@ public:
+     Annots(const Annots &) = delete;
+     Annots &operator=(const Annots &) = delete;
+
+-    const std::vector<Annot *> &getAnnots() { return annots; }
++    const std::vector<std::shared_ptr<Annot>> &getAnnots() { return annots; }
+
+-    void appendAnnot(Annot *annot);
+-    bool removeAnnot(Annot *annot);
++    void appendAnnot(std::shared_ptr<Annot> annot);
++    bool removeAnnot(const std::shared_ptr<Annot> &annot);
+
+ private:
+-    Annot *createAnnot(Object &&dictObject, const Object *obj);
+-    Annot *findAnnot(Ref *ref);
++    std::shared_ptr<Annot> createAnnot(Object &&dictObject, const Object *obj);
++    std::shared_ptr<Annot> findAnnot(Ref *ref);
+
+     PDFDoc *doc;
+-    std::vector<Annot *> annots;
++    std::vector<std::shared_ptr<Annot>> annots;
+ };
+
+ #endif
+diff --git a/poppler/FontInfo.cc b/poppler/FontInfo.cc
+index 309ec6d..c8c1bdf 100644
+--- a/poppler/FontInfo.cc
++++ b/poppler/FontInfo.cc
+@@ -82,7 +82,7 @@ std::vector<FontInfo *> FontInfoScanner::scan(int nPages)
+             delete resDict;
+         }
+         annots = page->getAnnots();
+-        for (Annot *annot : annots->getAnnots()) {
++        for (const std::shared_ptr<Annot> &annot : annots->getAnnots()) {
+             Object obj1 = annot->getAppearanceResDict();
+             if (obj1.isDict()) {
+                 scanFonts(xrefA.get(), obj1.getDict(), &result);
+diff --git a/poppler/Form.cc b/poppler/Form.cc
+index 2c0c35a..ca73a35 100644
+--- a/poppler/Form.cc
++++ b/poppler/Form.cc
+@@ -123,12 +123,7 @@ FormWidget::FormWidget(PDFDoc *docA, Object *aobj, unsigned num, Ref aref, FormF
+     widget = nullptr;
+ }
+
+-FormWidget::~FormWidget()
+-{
+-    if (widget) {
+-        widget->decRefCnt();
+-    }
+-}
++FormWidget::~FormWidget() = default;
+
+ void FormWidget::print(int indent)
+ {
+@@ -142,7 +137,7 @@ void FormWidget::createWidgetAnnotation()
+     }
+
+     Object obj1(ref);
+-    widget = new AnnotWidget(doc, &obj, &obj1, field);
++    widget = std::make_shared<AnnotWidget>(doc, &obj, &obj1, field);
+ }
+
+ bool FormWidget::inRect(double x, double y) const
+@@ -3114,7 +3109,7 @@ FormPageWidgets::FormPageWidgets(Annots *annots, unsigned int page, Form *form)
+
+         /* For each entry in the page 'Annots' dict, try to find
+            a matching form field */
+-        for (Annot *annot : annots->getAnnots()) {
++        for (const std::shared_ptr<Annot> &annot : annots->getAnnots()) {
+
+             if (annot->getType() != Annot::typeWidget) {
+                 continue;
+diff --git a/poppler/Form.h b/poppler/Form.h
+index 80be38e..5a0af10 100644
+--- a/poppler/Form.h
++++ b/poppler/Form.h
+@@ -146,8 +146,8 @@ public:
+     static void decodeID(unsigned id, unsigned *pageNum, unsigned *fieldNum);
+
+     void createWidgetAnnotation();
+-    AnnotWidget *getWidgetAnnotation() const { return widget; }
+-    void setWidgetAnnotation(AnnotWidget *_widget) { widget = _widget; }
++    std::shared_ptr<AnnotWidget> getWidgetAnnotation() const { return widget; }
++    void setWidgetAnnotation(std::shared_ptr<AnnotWidget> _widget) { widget = std::move(widget); }
+
+     virtual void updateWidgetAppearance() = 0;
+
+@@ -156,7 +156,7 @@ public:
+ protected:
+     FormWidget(PDFDoc *docA, Object *aobj, unsigned num, Ref aref, FormField *fieldA);
+
+-    AnnotWidget *widget;
++    std::shared_ptr<AnnotWidget> widget;
+     FormField *field;
+     FormFieldType type;
+     Object obj;
+diff --git a/poppler/JSInfo.cc b/poppler/JSInfo.cc
+index eaef33e..e4c4f37 100644
+--- a/poppler/JSInfo.cc
++++ b/poppler/JSInfo.cc
+@@ -191,15 +191,15 @@ void JSInfo::scan(int nPages)
+         }
+         // annotation actions (links, screen, widget)
+         annots = page->getAnnots();
+-        for (Annot *a : annots->getAnnots()) {
++        for (const std::shared_ptr<Annot> &a : annots->getAnnots()) {
+             if (a->getType() == Annot::typeLink) {
+-                AnnotLink *annot = static_cast<AnnotLink *>(a);
++                AnnotLink *annot = static_cast<AnnotLink *>(a.get());
+                 scanLinkAction(annot->getAction(), "Link Annotation Activated");
+                 if (onlyFirstJS && hasJS) {
+                     return;
+                 }
+             } else if (a->getType() == Annot::typeScreen) {
+-                AnnotScreen *annot = static_cast<AnnotScreen *>(a);
++                AnnotScreen *annot = static_cast<AnnotScreen *>(a.get());
+                 scanLinkAction(annot->getAction(), "Screen Annotation Activated");
+                 scanLinkAction(annot->getAdditionalAction(Annot::actionCursorEntering).get(), "Screen Annotation Cursor Enter");
+                 scanLinkAction(annot->getAdditionalAction(Annot::actionCursorLeaving).get(), "Screen Annotation Cursor Leave");
+@@ -216,7 +216,7 @@ void JSInfo::scan(int nPages)
+                     return;
+                 }
+             } else if (a->getType() == Annot::typeWidget) {
+-                AnnotWidget *annot = static_cast<AnnotWidget *>(a);
++                AnnotWidget *annot = static_cast<AnnotWidget *>(a.get());
+                 scanLinkAction(annot->getAction(), "Widget Annotation Activated");
+                 scanLinkAction(annot->getAdditionalAction(Annot::actionCursorEntering).get(), "Widget Annotation Cursor Enter");
+                 scanLinkAction(annot->getAdditionalAction(Annot::actionCursorLeaving).get(), "Widget Annotation Cursor Leave");
+diff --git a/poppler/Link.cc b/poppler/Link.cc
+index 8aca0b9..c952887 100644
+--- a/poppler/Link.cc
++++ b/poppler/Link.cc
+@@ -35,6 +35,7 @@
+
+ #include <cstddef>
+ #include <cstring>
++#include <memory>
+ #include "goo/gmem.h"
+ #include "goo/GooString.h"
+ #include "Error.h"
+@@ -890,20 +891,13 @@ Links::Links(Annots *annots)
+         return;
+     }
+
+-    for (Annot *annot : annots->getAnnots()) {
++    for (const std::shared_ptr<Annot> &annot : annots->getAnnots()) {
+
+         if (annot->getType() != Annot::typeLink) {
+             continue;
+         }
+-
+-        annot->incRefCnt();
+-        links.push_back(static_cast<AnnotLink *>(annot));
++        links.push_back(std::static_pointer_cast<AnnotLink>(annot));
+     }
+ }
+
+-Links::~Links()
+-{
+-    for (AnnotLink *link : links) {
+-        link->decRefCnt();
+-    }
+-}
++Links::~Links() = default;
+diff --git a/poppler/Link.h b/poppler/Link.h
+index 204207b..9bd01f0 100644
+--- a/poppler/Link.h
++++ b/poppler/Link.h
+@@ -555,10 +555,10 @@ public:
+     Links(const Links &) = delete;
+     Links &operator=(const Links &) = delete;
+
+-    const std::vector<AnnotLink *> &getLinks() const { return links; }
++    const std::vector<std::shared_ptr<AnnotLink>> &getLinks() const { return links; }
+
+ private:
+-    std::vector<AnnotLink *> links;
++    std::vector<std::shared_ptr<AnnotLink>> links;
+ };
+
+ #endif
+diff --git a/poppler/PDFDoc.cc b/poppler/PDFDoc.cc
+index f1b9bfc..872841c 100644
+--- a/poppler/PDFDoc.cc
++++ b/poppler/PDFDoc.cc
+@@ -2225,7 +2225,7 @@ bool PDFDoc::sign(const std::string &saveFilename, const std::string &certNickna
+     field->setImageResource(imageResourceRef);
+
+     Object refObj(ref);
+-    AnnotWidget *signatureAnnot = new AnnotWidget(this, field->getObj(), &refObj, field.get());
++    auto signatureAnnot = std::make_shared<AnnotWidget>(this, field->getObj(), &refObj, field.get());
+     signatureAnnot->setFlags(signatureAnnot->getFlags() | Annot::flagPrint | Annot::flagLocked | Annot::flagNoRotate);
+     Dict dummy(getXRef());
+     auto appearCharacs = std::make_unique<AnnotAppearanceCharacs>(&dummy);
+diff --git a/poppler/PSOutputDev.cc b/poppler/PSOutputDev.cc
+index 23e3dcf..ac03f27 100644
+--- a/poppler/PSOutputDev.cc
++++ b/poppler/PSOutputDev.cc
+@@ -1738,7 +1738,7 @@ void PSOutputDev::writeDocSetup(Catalog *catalog, const std::vector<int> &pageLi
+             setupResources(resDict);
+         }
+         annots = page->getAnnots();
+-        for (Annot *annot : annots->getAnnots()) {
++        for (const std::shared_ptr<Annot> &annot : annots->getAnnots()) {
+             Object obj1 = annot->getAppearanceResDict();
+             if (obj1.isDict()) {
+                 setupResources(obj1.getDict());
+diff --git a/poppler/Page.cc b/poppler/Page.cc
+index 9d5a4ff..234f124 100644
+--- a/poppler/Page.cc
++++ b/poppler/Page.cc
+@@ -318,14 +318,7 @@ err1:
+     ok = false;
+ }
+
+-Page::~Page()
+-{
+-    delete attrs;
+-    delete annots;
+-    for (auto frmField : standaloneFields) {
+-        delete frmField;
+-    }
+-}
++Page::~Page() = default;
+
+ Dict *Page::getResourceDict()
+ {
+@@ -364,11 +357,11 @@ void Page::replaceXRef(XRef *xrefA)
+ }
+
+ /* Loads standalone fields into Page, should be called once per page only */
+-void Page::loadStandaloneFields(Annots *annotations, Form *form)
++void Page::loadStandaloneFields(Form *form)
+ {
+     /* Look for standalone annots, identified by being: 1) of type Widget
+      * 2) not referenced from the Catalog's Form Field array */
+-    for (Annot *annot : annots->getAnnots()) {
++    for (const std::shared_ptr<Annot> &annot : annots->getAnnots()) {
+
+         if (annot->getType() != Annot::typeWidget || !annot->getHasRef()) {
+             continue;
+@@ -384,7 +377,7 @@ void Page::loadStandaloneFields(Annots *annotations, Form *form)
+
+         if (field && field->getNumWidgets() == 1) {
+
+-            static_cast<AnnotWidget *>(annot)->setField(field);
++            std::static_pointer_cast<AnnotWidget>(annot)->setField(field);
+
+             field->setStandAlone(true);
+             FormWidget *formWidget = field->getWidget(0);
+@@ -405,15 +398,15 @@ Annots *Page::getAnnots(XRef *xrefA)
+ {
+     if (!annots) {
+         Object obj = getAnnotsObject(xrefA);
+-        annots = new Annots(doc, num, &obj);
++        annots = std::make_unique<Annots>(doc, num, &obj);
+         // Load standalone fields once for the page
+-        loadStandaloneFields(annots, doc->getCatalog()->getForm());
++        loadStandaloneFields(doc->getCatalog()->getForm());
+     }
+
+-    return annots;
++    return annots.get();
+ }
+
+-bool Page::addAnnot(Annot *annot)
++bool Page::addAnnot(const std::shared_ptr<Annot> &annot)
+ {
+     if (unlikely(xref->getEntry(pageRef.num)->type == xrefEntryFree)) {
+         // something very wrong happened if we're here
+@@ -456,14 +449,14 @@ bool Page::addAnnot(Annot *annot)
+     // Popup annots are already handled by markup annots,
+     // so add to the list only Popup annots without a
+     // markup annotation associated.
+-    if (annot->getType() != Annot::typePopup || !static_cast<AnnotPopup *>(annot)->hasParent()) {
++    if (annot->getType() != Annot::typePopup || !static_cast<AnnotPopup *>(annot.get())->hasParent()) {
+         annots->appendAnnot(annot);
+     }
+     annot->setPage(num, true);
+
+-    AnnotMarkup *annotMarkup = dynamic_cast<AnnotMarkup *>(annot);
++    AnnotMarkup *annotMarkup = dynamic_cast<AnnotMarkup *>(annot.get());
+     if (annotMarkup) {
+-        AnnotPopup *annotPopup = annotMarkup->getPopup();
++        std::shared_ptr<AnnotPopup> annotPopup = annotMarkup->getPopup();
+         if (annotPopup) {
+             addAnnot(annotPopup);
+         }
+@@ -472,7 +465,7 @@ bool Page::addAnnot(Annot *annot)
+     return true;
+ }
+
+-void Page::removeAnnot(Annot *annot)
++void Page::removeAnnot(const std::shared_ptr<Annot> &annot)
+ {
+     Ref annotRef = annot->getRef();
+
+@@ -596,8 +589,8 @@ void Page::displaySlice(OutputDev *out, double hDPI, double vDPI, int rotate, bo
+         if (globalParams->getPrintCommands()) {
+             printf("***** Annotations\n");
+         }
+-        for (Annot *annot : annots->getAnnots()) {
+-            if ((annotDisplayDecideCbk && (*annotDisplayDecideCbk)(annot, annotDisplayDecideCbkData)) || !annotDisplayDecideCbk) {
++        for (const std::shared_ptr<Annot> &annot : annots->getAnnots()) {
++            if ((annotDisplayDecideCbk && (*annotDisplayDecideCbk)(annot.get(), annotDisplayDecideCbkData)) || !annotDisplayDecideCbk) {
+                 annot->draw(gfx, printing);
+             }
+         }
+@@ -787,8 +780,8 @@ void Page::makeBox(double hDPI, double vDPI, int rotate, bool useMediaBox, bool
+ void Page::processLinks(OutputDev *out)
+ {
+     std::unique_ptr<Links> links = getLinks();
+-    for (AnnotLink *link : links->getLinks()) {
+-        out->processLink(link);
++    for (const std::shared_ptr<AnnotLink> &link : links->getLinks()) {
++        out->processLink(link.get());
+     }
+ }
+
+diff --git a/poppler/Page.h b/poppler/Page.h
+index 3fe86be..56951a2 100644
+--- a/poppler/Page.h
++++ b/poppler/Page.h
+@@ -181,9 +181,9 @@ public:
+     // Get annotations array.
+     Object getAnnotsObject(XRef *xrefA = nullptr) { return annotsObj.fetch(xrefA ? xrefA : xref); }
+     // Add a new annotation to the page
+-    bool addAnnot(Annot *annot);
++    bool addAnnot(const std::shared_ptr<Annot> &annot);
+     // Remove an existing annotation from the page
+-    void removeAnnot(Annot *annot);
++    void removeAnnot(const std::shared_ptr<Annot> &annot);
+
+     // Return a list of links.
+     std::unique_ptr<Links> getLinks();
+@@ -252,7 +252,7 @@ private:
+     const Ref pageRef; // page reference
+     int num; // page number
+     PageAttrs *attrs; // page attributes
+-    Annots *annots; // annotations
++    std::unique_ptr<Annots> annots; // annotations
+     Object annotsObj; // annotations array
+     Object contents; // page contents
+     Object thumb; // page thumbnail
+@@ -267,7 +267,7 @@ private:
+     // create standalone FormFields to contain those special FormWidgets, as
+     // they are 'de facto' being used to implement tooltips. See #34
+     std::vector<FormField *> standaloneFields;
+-    void loadStandaloneFields(Annots *annotations, Form *form);
++    void loadStandaloneFields(Form *form);
+ };
+
+ #endif
+diff --git a/qt5/src/poppler-annotation-private.h b/qt5/src/poppler-annotation-private.h
+index 1f8d756..8970f49 100644
+--- a/qt5/src/poppler-annotation-private.h
++++ b/qt5/src/poppler-annotation-private.h
+@@ -57,7 +57,7 @@ public:
+
+     /* Returns an Annotation of the right subclass whose d_ptr points to
+      * this AnnotationPrivate */
+-    virtual Annotation *makeAlias() = 0;
++    virtual std::unique_ptr<Annotation> makeAlias() = 0;
+
+     /* properties: contents related */
+     QString author;
+@@ -77,18 +77,18 @@ public:
+     /* revisions */
+     Annotation::RevScope revisionScope;
+     Annotation::RevType revisionType;
+-    QList<Annotation *> revisions;
++    std::vector<std::unique_ptr<Annotation>> revisions;
+
+     /* After this call, the Annotation object will behave like a wrapper for
+      * the specified Annot object. All cached values are discarded */
+-    void tieToNativeAnnot(Annot *ann, ::Page *page, DocumentData *doc);
++    void tieToNativeAnnot(std::shared_ptr<Annot> ann, ::Page *page, DocumentData *doc);
+
+     /* Creates a new Annot object on the specified page, flushes current
+      * values to that object and ties this Annotation to that object */
+-    virtual Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) = 0;
++    virtual std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) = 0;
+
+     /* Inited to 0 (i.e. untied annotation) */
+-    Annot *pdfAnnot;
++    std::shared_ptr<Annot> pdfAnnot;
+     ::Page *pdfPage;
+     DocumentData *parentDoc;
+
+diff --git a/qt5/src/poppler-annotation.cc b/qt5/src/poppler-annotation.cc
+index e15523c..f148671 100644
+--- a/qt5/src/poppler-annotation.cc
++++ b/qt5/src/poppler-annotation.cc
+@@ -65,6 +65,12 @@
+  * static data set at creation time by findAnnotations
+  */
+
++template<typename T, typename U>
++static std::unique_ptr<T> static_pointer_cast(std::unique_ptr<U> &&in)
++{
++    return std::unique_ptr<T>(static_cast<std::add_pointer_t<T>>(in.release()));
++}
++
+ namespace Poppler {
+
+ // BEGIN AnnotationUtils implementation
+@@ -199,36 +205,25 @@ void getRawDataFromQImage(const QImage &qimg, int bitsPerPixel, QByteArray *data
+ void AnnotationPrivate::addRevision(Annotation *ann, Annotation::RevScope scope, Annotation::RevType type)
+ {
+     /* Since ownership stays with the caller, create an alias of ann */
+-    revisions.append(ann->d_ptr->makeAlias());
++    revisions.push_back(ann->d_ptr->makeAlias());
+
+     /* Set revision properties */
+     revisionScope = scope;
+     revisionType = type;
+ }
+
+-AnnotationPrivate::~AnnotationPrivate()
+-{
+-    // Delete all children revisions
+-    qDeleteAll(revisions);
+-
+-    // Release Annot object
+-    if (pdfAnnot) {
+-        pdfAnnot->decRefCnt();
+-    }
+-}
++AnnotationPrivate::~AnnotationPrivate() = default;
+
+-void AnnotationPrivate::tieToNativeAnnot(Annot *ann, ::Page *page, Poppler::DocumentData *doc)
++void AnnotationPrivate::tieToNativeAnnot(std::shared_ptr<Annot> *ann, ::Page *page, Poppler::DocumentData *doc)
+ {
+     if (pdfAnnot) {
+         error(errIO, -1, "Annotation is already tied");
+         return;
+     }
+
+-    pdfAnnot = ann;
++    pdfAnnot = std::move(ann);
+     pdfPage = page;
+     parentDoc = doc;
+-
+-    pdfAnnot->incRefCnt();
+ }
+
+ /* This method is called when a new annotation is created, after pdfAnnot and
+@@ -237,7 +232,7 @@ void AnnotationPrivate::flushBaseAnnotationProperties()
+ {
+     Q_ASSERT(pdfPage);
+
+-    Annotation *q = makeAlias(); // Setters are defined in the public class
++    std::unique_ptr<Annotation> q = makeAlias(); // Setters are defined in the public class
+
+     // Since pdfAnnot has been set, this calls will write in the Annot object
+     q->setAuthor(author);
+@@ -250,13 +245,7 @@ void AnnotationPrivate::flushBaseAnnotationProperties()
+     q->setStyle(style);
+     q->setPopup(popup);
+
+-    // Flush revisions
+-    foreach (Annotation *r, revisions) {
+-        // TODO: Flush revision
+-        delete r; // Object is no longer needed
+-    }
+-
+-    delete q;
++    revisions.clear();
+
+     // Clear some members to save memory
+     author.clear();
+@@ -455,14 +444,14 @@ QList<Annotation *> AnnotationPrivate::findAnnotations(::Page *pdfPage, Document
+
+     // Create Annotation objects and tie to their native Annot
+     QList<Annotation *> res;
+-    for (Annot *ann : annots->getAnnots()) {
++    for (const std::shared_ptr<Annot> &ann : annots->getAnnots()) {
+         if (!ann) {
+             error(errInternal, -1, "Annot is null");
+             continue;
+         }
+
+         // Check parent annotation
+-        AnnotMarkup *markupann = dynamic_cast<AnnotMarkup *>(ann);
++        AnnotMarkup *markupann = dynamic_cast<AnnotMarkup *>(ann.get());
+         if (!markupann) {
+             // Assume it's a root annotation, and skip if user didn't request it
+             if (parentID != -1) {
+@@ -536,7 +525,7 @@ QList<Annotation *> AnnotationPrivate::findAnnotations(::Page *pdfPage, Document
+                 continue;
+             }
+             // parse Link params
+-            AnnotLink *linkann = static_cast<AnnotLink *>(ann);
++            AnnotLink *linkann = static_cast<AnnotLink *>(ann.get());
+             LinkAnnotation *l = new LinkAnnotation();
+             annotation = l;
+
+@@ -566,7 +555,7 @@ QList<Annotation *> AnnotationPrivate::findAnnotations(::Page *pdfPage, Document
+             if (!wantFileAttachmentAnnotations) {
+                 continue;
+             }
+-            AnnotFileAttachment *attachann = static_cast<AnnotFileAttachment *>(ann);
++            AnnotFileAttachment *attachann = static_cast<AnnotFileAttachment *>(ann.get());
+             FileAttachmentAnnotation *f = new FileAttachmentAnnotation();
+             annotation = f;
+             // -> fileIcon
+@@ -581,7 +570,7 @@ QList<Annotation *> AnnotationPrivate::findAnnotations(::Page *pdfPage, Document
+             if (!wantSoundAnnotations) {
+                 continue;
+             }
+-            AnnotSound *soundann = static_cast<AnnotSound *>(ann);
++            AnnotSound *soundann = static_cast<AnnotSound *>(ann.get());
+             SoundAnnotation *s = new SoundAnnotation();
+             annotation = s;
+
+@@ -596,7 +585,7 @@ QList<Annotation *> AnnotationPrivate::findAnnotations(::Page *pdfPage, Document
+             if (!wantMovieAnnotations) {
+                 continue;
+             }
+-            AnnotMovie *movieann = static_cast<AnnotMovie *>(ann);
++            AnnotMovie *movieann = static_cast<AnnotMovie *>(ann.get());
+             MovieAnnotation *m = new MovieAnnotation();
+             annotation = m;
+
+@@ -614,7 +603,7 @@ QList<Annotation *> AnnotationPrivate::findAnnotations(::Page *pdfPage, Document
+             if (!wantScreenAnnotations) {
+                 continue;
+             }
+-            AnnotScreen *screenann = static_cast<AnnotScreen *>(ann);
++            AnnotScreen *screenann = static_cast<AnnotScreen *>(ann.get());
+             // TODO Support other link types than Link::Rendition in ScreenAnnotation
+             if (!screenann->getAction() || screenann->getAction()->getKind() != actionRendition) {
+                 continue;
+@@ -644,7 +633,7 @@ QList<Annotation *> AnnotationPrivate::findAnnotations(::Page *pdfPage, Document
+             annotation = new WidgetAnnotation();
+             break;
+         case Annot::typeRichMedia: {
+-            const AnnotRichMedia *annotRichMedia = static_cast<AnnotRichMedia *>(ann);
++            const AnnotRichMedia *annotRichMedia = static_cast<AnnotRichMedia *>(ann.get());
+
+             RichMediaAnnotation *richMediaAnnotation = new RichMediaAnnotation;
+
+@@ -852,9 +841,9 @@ Link *AnnotationPrivate::additionalAction(Annotation::AdditionalActionType type)
+
+     std::unique_ptr<::LinkAction> linkAction = nullptr;
+     if (pdfAnnot->getType() == Annot::typeScreen) {
+-        linkAction = static_cast<AnnotScreen *>(pdfAnnot)->getAdditionalAction(actionType);
++        linkAction = static_cast<AnnotScreen *>(pdfAnnot.get())->getAdditionalAction(actionType);
+     } else {
+-        linkAction = static_cast<AnnotWidget *>(pdfAnnot)->getAdditionalAction(actionType);
++        linkAction = static_cast<AnnotWidget *>(pdfAnnot.get())->getAdditionalAction(actionType);
+     }
+
+     Link *link = nullptr;
+@@ -875,7 +864,7 @@ void AnnotationPrivate::addAnnotationToPage(::Page *pdfPage, DocumentData *doc,
+
+     // Unimplemented annotations can't be created by the user because their ctor
+     // is private. Therefore, createNativeAnnot will never return 0
+-    Annot *nativeAnnot = ann->d_ptr->createNativeAnnot(pdfPage, doc);
++    std::shared_ptr<Annot> nativeAnnot = ann->d_ptr->createNativeAnnot(pdfPage, doc);
+     Q_ASSERT(nativeAnnot);
+
+     if (ann->d_ptr->annotationAppearance.isStream()) {
+@@ -908,8 +897,8 @@ class TextAnnotationPrivate : public AnnotationPrivate
+ {
+ public:
+     TextAnnotationPrivate();
+-    Annotation *makeAlias() override;
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
++    std::unique_ptr<Annotation> makeAlias() override;
++    std::unique_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
+     void setDefaultAppearanceToNative();
+     std::unique_ptr<DefaultAppearance> getDefaultAppearanceFromNative() const;
+
+@@ -1419,7 +1408,7 @@ QString Annotation::author() const
+         return d->author;
+     }
+
+-    const AnnotMarkup *markupann = dynamic_cast<const AnnotMarkup *>(d->pdfAnnot);
++    const AnnotMarkup *markupann = dynamic_cast<const AnnotMarkup *>(d->pdfAnnot.get());
+     return markupann ? UnicodeParsedString(markupann->getLabel()) : QString();
+ }
+
+@@ -1432,7 +1421,7 @@ void Annotation::setAuthor(const QString &author)
+         return;
+     }
+
+-    AnnotMarkup *markupann = dynamic_cast<AnnotMarkup *>(d->pdfAnnot);
++    AnnotMarkup *markupann = dynamic_cast<AnnotMarkup *>(d->pdfAnnot.get());
+     if (markupann) {
+         markupann->setLabel(std::unique_ptr<GooString>(QStringToUnicodeGooString(author)));
+     }
+@@ -1535,7 +1524,7 @@ QDateTime Annotation::creationDate() const
+         return d->creationDate;
+     }
+
+-    const AnnotMarkup *markupann = dynamic_cast<const AnnotMarkup *>(d->pdfAnnot);
++    const AnnotMarkup *markupann = dynamic_cast<const AnnotMarkup *>(d->pdfAnnot.get());
+
+     if (markupann && markupann->getDate()) {
+         return convertDate(markupann->getDate()->c_str());
+@@ -1553,7 +1542,7 @@ void Annotation::setCreationDate(const QDateTime &date)
+         return;
+     }
+
+-    AnnotMarkup *markupann = dynamic_cast<AnnotMarkup *>(d->pdfAnnot);
++    AnnotMarkup *markupann = dynamic_cast<AnnotMarkup *>(d->pdfAnnot.get());
+     if (markupann) {
+         if (date.isValid()) {
+             const time_t t = date.toSecsSinceEpoch();
+@@ -1686,7 +1675,7 @@ Annotation::Style Annotation::style() const
+     Style s;
+     s.setColor(convertAnnotColor(d->pdfAnnot->getColor()));
+
+-    const AnnotMarkup *markupann = dynamic_cast<const AnnotMarkup *>(d->pdfAnnot);
++    const AnnotMarkup *markupann = dynamic_cast<const AnnotMarkup *>(d->pdfAnnot.get());
+     if (markupann) {
+         s.setOpacity(markupann->getOpacity());
+     }
+@@ -1713,11 +1702,11 @@ Annotation::Style Annotation::style() const
+     AnnotBorderEffect *border_effect;
+     switch (d->pdfAnnot->getType()) {
+     case Annot::typeFreeText:
+-        border_effect = static_cast<AnnotFreeText *>(d->pdfAnnot)->getBorderEffect();
++        border_effect = static_cast<AnnotFreeText *>(d->pdfAnnot.get())->getBorderEffect();
+         break;
+     case Annot::typeSquare:
+     case Annot::typeCircle:
+-        border_effect = static_cast<AnnotGeometry *>(d->pdfAnnot)->getBorderEffect();
++        border_effect = static_cast<AnnotGeometry *>(d->pdfAnnot.get())->getBorderEffect();
+         break;
+     default:
+         border_effect = nullptr;
+@@ -1741,7 +1730,7 @@ void Annotation::setStyle(const Annotation::Style &style)
+
+     d->pdfAnnot->setColor(convertQColor(style.color()));
+
+-    AnnotMarkup *markupann = dynamic_cast<AnnotMarkup *>(d->pdfAnnot);
++    AnnotMarkup *markupann = dynamic_cast<AnnotMarkup *>(d->pdfAnnot.get());
+     if (markupann) {
+         markupann->setOpacity(style.opacity());
+     }
+@@ -1765,9 +1754,9 @@ Annotation::Popup Annotation::popup() const
+     AnnotPopup *popup = nullptr;
+     int flags = -1; // Not initialized
+
+-    const AnnotMarkup *markupann = dynamic_cast<const AnnotMarkup *>(d->pdfAnnot);
++    const AnnotMarkup *markupann = dynamic_cast<const AnnotMarkup *>(d->pdfAnnot.get());
+     if (markupann) {
+-        popup = markupann->getPopup();
++        popup = markupann->getPopup().get();
+         w.setSummary(UnicodeParsedString(markupann->getSubject()));
+     }
+
+@@ -1783,7 +1772,7 @@ Annotation::Popup Annotation::popup() const
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeText) {
+-        const AnnotText *textann = static_cast<const AnnotText *>(d->pdfAnnot);
++        const AnnotText *textann = static_cast<const AnnotText *>(d->pdfAnnot.get());
+
+         // Text annotations default to same rect as annotation
+         if (flags == -1) {
+@@ -1839,7 +1828,7 @@ Annotation::RevScope Annotation::revisionScope() const
+         return d->revisionScope;
+     }
+
+-    const AnnotMarkup *markupann = dynamic_cast<const AnnotMarkup *>(d->pdfAnnot);
++    const AnnotMarkup *markupann = dynamic_cast<const AnnotMarkup *>(d->pdfAnnot.get());
+
+     if (markupann && markupann->isInReplyTo()) {
+         switch (markupann->getReplyTo()) {
+@@ -1861,7 +1850,7 @@ Annotation::RevType Annotation::revisionType() const
+         return d->revisionType;
+     }
+
+-    const AnnotText *textann = dynamic_cast<const AnnotText *>(d->pdfAnnot);
++    const AnnotText *textann = dynamic_cast<const AnnotText *>(d->pdfAnnot.get());
+
+     if (textann && textann->isInReplyTo()) {
+         switch (textann->getState()) {
+@@ -1892,8 +1881,9 @@ QList<Annotation *> Annotation::revisions() const
+     if (!d->pdfAnnot) {
+         /* Return aliases, whose ownership goes to the caller */
+         QList<Annotation *> res;
+-        foreach (Annotation *rev, d->revisions)
+-            res.append(rev->d_ptr->makeAlias());
++        for (const std::unique_ptr<Annotation> &rev : d->revisions) {
++            res.append(rev->d_ptr->makeAlias().release());
++        }
+         return res;
+     }
+
+@@ -1910,7 +1900,7 @@ std::unique_ptr<AnnotationAppearance> Annotation::annotationAppearance() const
+ {
+     Q_D(const Annotation);
+
+-    return std::make_unique<AnnotationAppearance>(new AnnotationAppearancePrivate(d->pdfAnnot));
++    return std::make_unique<AnnotationAppearance>(new AnnotationAppearancePrivate(d->pdfAnnot.get()));
+ }
+
+ void Annotation::setAnnotationAppearance(const AnnotationAppearance &annotationAppearance)
+@@ -1934,15 +1924,15 @@ void Annotation::setAnnotationAppearance(const AnnotationAppearance &annotationA
+
+ TextAnnotationPrivate::TextAnnotationPrivate() : AnnotationPrivate(), textType(TextAnnotation::Linked), textIcon(QStringLiteral("Note")), inplaceAlign(0), inplaceIntent(TextAnnotation::Unknown) { }
+
+-Annotation *TextAnnotationPrivate::makeAlias()
++std::unique_ptr<Annotation> TextAnnotationPrivate::makeAlias()
+ {
+-    return new TextAnnotation(*this);
++    return std::unique_ptr<Annotation>(new TextAnnotation(*this);
+ }
+
+-Annot *TextAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
++std::shared_ptr<Annot> *TextAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
+ {
+-    // Setters are defined in the public class
+-    TextAnnotation *q = static_cast<TextAnnotation *>(makeAlias());
++    // Setters are defined in the public clas
++    std::unique_ptr<TextAnnotation> q = static_pointer_cast<TextAnnotation *>(makeAlias());
+
+     // Set page and contents
+     pdfPage = destPage;
+@@ -1951,13 +1941,13 @@ Annot *TextAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *
+     // Set pdfAnnot
+     PDFRectangle rect = boundaryToPdfRectangle(boundary, flags);
+     if (textType == TextAnnotation::Linked) {
+-        pdfAnnot = new AnnotText { destPage->getDoc(), &rect };
++        pdfAnnot = std::make_shared<AnnotText>(destPage->getDoc(), &rect);
+     } else {
+         const double pointSize = textFont ? textFont->pointSizeF() : AnnotFreeText::undefinedFontPtSize;
+         if (pointSize < 0) {
+             qWarning() << "TextAnnotationPrivate::createNativeAnnot: font pointSize < 0";
+         }
+-        pdfAnnot = new AnnotFreeText { destPage->getDoc(), &rect };
++        pdfAnnot = std::make_shared<AnnotFreeText>(destPage->getDoc(), &rect);
+     }
+
+     // Set properties
+@@ -1967,8 +1957,6 @@ Annot *TextAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *
+     q->setCalloutPoints(inplaceCallout);
+     q->setInplaceIntent(inplaceIntent);
+
+-    delete q;
+-
+     inplaceCallout.clear(); // Free up memory
+
+     setDefaultAppearanceToNative();
+@@ -1979,7 +1967,7 @@ Annot *TextAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *
+ void TextAnnotationPrivate::setDefaultAppearanceToNative()
+ {
+     if (pdfAnnot && pdfAnnot->getType() == Annot::typeFreeText) {
+-        AnnotFreeText *ftextann = static_cast<AnnotFreeText *>(pdfAnnot);
++        AnnotFreeText *ftextann = static_cast<AnnotFreeText *>(pdfAnnot.get());
+         const double pointSize = textFont ? textFont->pointSizeF() : AnnotFreeText::undefinedFontPtSize;
+         if (pointSize < 0) {
+             qWarning() << "TextAnnotationPrivate::createNativeAnnot: font pointSize < 0";
+@@ -2008,7 +1996,7 @@ void TextAnnotationPrivate::setDefaultAppearanceToNative()
+ std::unique_ptr<DefaultAppearance> TextAnnotationPrivate::getDefaultAppearanceFromNative() const
+ {
+     if (pdfAnnot && pdfAnnot->getType() == Annot::typeFreeText) {
+-        AnnotFreeText *ftextann = static_cast<AnnotFreeText *>(pdfAnnot);
++        AnnotFreeText *ftextann = static_cast<AnnotFreeText *>(pdfAnnot.get());
+         return ftextann->getDefaultAppearance();
+     } else {
+         return {};
+@@ -2165,7 +2153,7 @@ QString TextAnnotation::textIcon() const
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeText) {
+-        const AnnotText *textann = static_cast<const AnnotText *>(d->pdfAnnot);
++        const AnnotText *textann = static_cast<const AnnotText *>(d->pdfAnnot.get());
+         return QString::fromLatin1(textann->getIcon()->c_str());
+     }
+
+@@ -2182,7 +2170,7 @@ void TextAnnotation::setTextIcon(const QString &icon)
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeText) {
+-        AnnotText *textann = static_cast<AnnotText *>(d->pdfAnnot);
++        AnnotText *textann = static_cast<AnnotText *>(d->pdfAnnot.get());
+         QByteArray encoded = icon.toLatin1();
+         GooString s(encoded.constData());
+         textann->setIcon(&s);
+@@ -2256,7 +2244,7 @@ int TextAnnotation::inplaceAlign() const
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeFreeText) {
+-        const AnnotFreeText *ftextann = static_cast<const AnnotFreeText *>(d->pdfAnnot);
++        const AnnotFreeText *ftextann = static_cast<const AnnotFreeText *>(d->pdfAnnot.get());
+         return static_cast<int>(ftextann->getQuadding());
+     }
+
+@@ -2273,7 +2261,7 @@ void TextAnnotation::setInplaceAlign(int align)
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeFreeText) {
+-        AnnotFreeText *ftextann = static_cast<AnnotFreeText *>(d->pdfAnnot);
++        AnnotFreeText *ftextann = static_cast<AnnotFreeText *>(d->pdfAnnot.get());
+         ftextann->setQuadding((VariableTextQuadding)align);
+     }
+ }
+@@ -2300,7 +2288,7 @@ QVector<QPointF> TextAnnotation::calloutPoints() const
+         return QVector<QPointF>();
+     }
+
+-    const AnnotFreeText *ftextann = static_cast<const AnnotFreeText *>(d->pdfAnnot);
++    const AnnotFreeText *ftextann = static_cast<const AnnotFreeText *>(d->pdfAnnot.get());
+     const AnnotCalloutLine *callout = ftextann->getCalloutLine();
+
+     if (!callout) {
+@@ -2332,7 +2320,7 @@ void TextAnnotation::setCalloutPoints(const QVector<QPointF> &points)
+         return;
+     }
+
+-    AnnotFreeText *ftextann = static_cast<AnnotFreeText *>(d->pdfAnnot);
++    AnnotFreeText *ftextann = static_cast<AnnotFreeText *>(d->pdfAnnot.get());
+     const int count = points.size();
+
+     if (count == 0) {
+@@ -2345,7 +2333,7 @@ void TextAnnotation::setCalloutPoints(const QVector<QPointF> &points)
+         return;
+     }
+
+-    AnnotCalloutLine *callout;
++    std::unique_ptr<AnnotCalloutLine> callout;
+     double x1, y1, x2, y2;
+     double MTX[6];
+     d->fillTransformationMTX(MTX);
+@@ -2355,12 +2343,12 @@ void TextAnnotation::setCalloutPoints(const QVector<QPointF> &points)
+     if (count == 3) {
+         double x3, y3;
+         XPDFReader::invTransform(MTX, points[2], x3, y3);
+-        callout = new AnnotCalloutMultiLine(x1, y1, x2, y2, x3, y3);
++        callout = std::make_unique<AnnotCalloutMultiLine>(x1, y1, x2, y2, x3, y3);
+     } else {
+-        callout = new AnnotCalloutLine(x1, y1, x2, y2);
++        callout = std::make_unique<AnnotCalloutLine>(x1, y1, x2, y2);
+     }
+
+-    ftextann->setCalloutLine(callout);
++    ftextann->setCalloutLine(std::move(callout));
+     delete callout;
+ }
+
+@@ -2373,7 +2361,7 @@ TextAnnotation::InplaceIntent TextAnnotation::inplaceIntent() const
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeFreeText) {
+-        const AnnotFreeText *ftextann = static_cast<const AnnotFreeText *>(d->pdfAnnot);
++        const AnnotFreeText *ftextann = static_cast<const AnnotFreeText *>(d->pdfAnnot.get());
+         return (TextAnnotation::InplaceIntent)ftextann->getIntent();
+     }
+
+@@ -2390,7 +2378,7 @@ void TextAnnotation::setInplaceIntent(TextAnnotation::InplaceIntent intent)
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeFreeText) {
+-        AnnotFreeText *ftextann = static_cast<AnnotFreeText *>(d->pdfAnnot);
++        AnnotFreeText *ftextann = static_cast<AnnotFreeText *>(d->pdfAnnot.get());
+         ftextann->setIntent((AnnotFreeText::AnnotFreeTextIntent)intent);
+     }
+ }
+@@ -2400,8 +2388,8 @@ class LineAnnotationPrivate : public AnnotationPrivate
+ {
+ public:
+     LineAnnotationPrivate();
+-    Annotation *makeAlias() override;
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
++    std::unique_ptr<Annotation> makeAlias() override;
++    std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
+
+     // data fields (note uses border for rendering style)
+     QLinkedList<QPointF> linePoints;
+@@ -2421,15 +2409,15 @@ LineAnnotationPrivate::LineAnnotationPrivate()
+ {
+ }
+
+-Annotation *LineAnnotationPrivate::makeAlias()
++std::unique_ptr<Annotation> LineAnnotationPrivate::makeAlias()
+ {
+-    return new LineAnnotation(*this);
++    return std::unique_ptr<LineAnnotation>(new LineAnnotation(*this));
+ }
+
+-Annot *LineAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
++std::shared_ptr<Annot> LineAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
+ {
+     // Setters are defined in the public class
+-    LineAnnotation *q = static_cast<LineAnnotation *>(makeAlias());
++    std::unique_ptr<LineAnnotation> q = static_pointer_cast<LineAnnotation *>(makeAlias());
+
+     // Set page and document
+     pdfPage = destPage;
+@@ -2438,9 +2426,9 @@ Annot *LineAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *
+     // Set pdfAnnot
+     PDFRectangle rect = boundaryToPdfRectangle(boundary, flags);
+     if (lineType == LineAnnotation::StraightLine) {
+-        pdfAnnot = new AnnotLine(doc->doc, &rect);
++        pdfAnnot = std::make_shared<AnnotLine>(doc->doc, &rect);
+     } else {
+-        pdfAnnot = new AnnotPolygon(doc->doc, &rect, lineClosed ? Annot::typePolygon : Annot::typePolyLine);
++        pdfAnnot = std::make_shared<AnnotPolygon>(doc->doc, &rect, lineClosed ? Annot::typePolygon : Annot::typePolyLine);
+     }
+
+     // Set properties
+@@ -2454,8 +2442,6 @@ Annot *LineAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *
+     q->setLineShowCaption(lineShowCaption);
+     q->setLineIntent(lineIntent);
+
+-    delete q;
+-
+     linePoints.clear(); // Free up memory
+
+     return pdfAnnot;
+@@ -2621,14 +2607,14 @@ QLinkedList<QPointF> LineAnnotation::linePoints() const
+
+     QLinkedList<QPointF> res;
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot);
++        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot.get());
+         QPointF p;
+         XPDFReader::transform(MTX, lineann->getX1(), lineann->getY1(), p);
+         res.append(p);
+         XPDFReader::transform(MTX, lineann->getX2(), lineann->getY2(), p);
+         res.append(p);
+     } else {
+-        const AnnotPolygon *polyann = static_cast<const AnnotPolygon *>(d->pdfAnnot);
++        const AnnotPolygon *polyann = static_cast<const AnnotPolygon *>(d->pdfAnnot.get());
+         const AnnotPath *vertices = polyann->getVertices();
+
+         for (int i = 0; i < vertices->getCoordsLength(); ++i) {
+@@ -2645,13 +2631,14 @@ void LineAnnotation::setLinePoints(const QLinkedList<QPointF> &points)
+ {
+     Q_D(LineAnnotation);
+
++    if (!d->pdfAnnot) {
+     if (!d->pdfAnnot) {
+         d->linePoints = points;
+         return;
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot);
++        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot.get());
+         if (points.size() != 2) {
+             error(errSyntaxError, -1, "Expected two points for a straight line");
+             return;
+@@ -2663,7 +2650,7 @@ void LineAnnotation::setLinePoints(const QLinkedList<QPointF> &points)
+         XPDFReader::invTransform(MTX, points.last(), x2, y2);
+         lineann->setVertices(x1, y1, x2, y2);
+     } else {
+-        AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot);
++        AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot.get());
+         AnnotPath *p = d->toAnnotPath(points);
+         polyann->setVertices(p);
+         delete p;
+@@ -2679,10 +2666,10 @@ LineAnnotation::TermStyle LineAnnotation::lineStartStyle() const
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot);
++        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot.get());
+         return (LineAnnotation::TermStyle)lineann->getStartStyle();
+     } else {
+-        const AnnotPolygon *polyann = static_cast<const AnnotPolygon *>(d->pdfAnnot);
++        const AnnotPolygon *polyann = static_cast<const AnnotPolygon *>(d->pdfAnnot.get());
+         return (LineAnnotation::TermStyle)polyann->getStartStyle();
+     }
+ }
+@@ -2697,10 +2684,10 @@ void LineAnnotation::setLineStartStyle(LineAnnotation::TermStyle style)
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot);
++        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot.get());
+         lineann->setStartEndStyle((AnnotLineEndingStyle)style, lineann->getEndStyle());
+     } else {
+-        AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot);
++        AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot.get());
+         polyann->setStartEndStyle((AnnotLineEndingStyle)style, polyann->getEndStyle());
+     }
+ }
+@@ -2714,10 +2701,10 @@ LineAnnotation::TermStyle LineAnnotation::lineEndStyle() const
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot);
++        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot.get());
+         return (LineAnnotation::TermStyle)lineann->getEndStyle();
+     } else {
+-        const AnnotPolygon *polyann = static_cast<const AnnotPolygon *>(d->pdfAnnot);
++        const AnnotPolygon *polyann = static_cast<const AnnotPolygon *>(d->pdfAnnot.get());
+         return (LineAnnotation::TermStyle)polyann->getEndStyle();
+     }
+ }
+@@ -2732,10 +2719,10 @@ void LineAnnotation::setLineEndStyle(LineAnnotation::TermStyle style)
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot);
++        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot.get());
+         lineann->setStartEndStyle(lineann->getStartStyle(), (AnnotLineEndingStyle)style);
+     } else {
+-        AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot);
++        AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot.get());
+         polyann->setStartEndStyle(polyann->getStartStyle(), (AnnotLineEndingStyle)style);
+     }
+ }
+@@ -2761,7 +2748,7 @@ void LineAnnotation::setLineClosed(bool closed)
+     }
+
+     if (d->pdfAnnot->getType() != Annot::typeLine) {
+-        AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot);
++        AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot.get());
+
+         // Set new subtype and switch intent if necessary
+         if (closed) {
+@@ -2789,10 +2776,10 @@ QColor LineAnnotation::lineInnerColor() const
+     AnnotColor *c;
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot);
++        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot.get());
+         c = lineann->getInteriorColor();
+     } else {
+-        const AnnotPolygon *polyann = static_cast<const AnnotPolygon *>(d->pdfAnnot);
++        const AnnotPolygon *polyann = static_cast<const AnnotPolygon *>(d->pdfAnnot.get());
+         c = polyann->getInteriorColor();
+     }
+
+@@ -2811,10 +2798,10 @@ void LineAnnotation::setLineInnerColor(const QColor &color)
+     auto c = convertQColor(color);
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot);
++        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot.get());
+         lineann->setInteriorColor(std::move(c));
+     } else {
+-        AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot);
++        AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot.get());
+         polyann->setInteriorColor(std::move(c));
+     }
+ }
+@@ -2828,7 +2815,7 @@ double LineAnnotation::lineLeadingForwardPoint() const
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot);
++        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot.get());
+         return lineann->getLeaderLineLength();
+     }
+
+@@ -2845,7 +2832,7 @@ void LineAnnotation::setLineLeadingForwardPoint(double point)
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot);
++        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot.get());
+         lineann->setLeaderLineLength(point);
+     }
+ }
+@@ -2859,7 +2846,7 @@ double LineAnnotation::lineLeadingBackPoint() const
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot);
++        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot.get());
+         return lineann->getLeaderLineExtension();
+     }
+
+@@ -2876,7 +2863,7 @@ void LineAnnotation::setLineLeadingBackPoint(double point)
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot);
++        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot.get());
+         lineann->setLeaderLineExtension(point);
+     }
+ }
+@@ -2890,7 +2877,7 @@ bool LineAnnotation::lineShowCaption() const
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot);
++        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot.get());
+         return lineann->getCaption();
+     }
+
+@@ -2907,7 +2894,7 @@ void LineAnnotation::setLineShowCaption(bool show)
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot);
++        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot.get());
+         lineann->setCaption(show);
+     }
+ }
+@@ -2921,10 +2908,10 @@ LineAnnotation::LineIntent LineAnnotation::lineIntent() const
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot);
++        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot.get());
+         return (LineAnnotation::LineIntent)(lineann->getIntent() + 1);
+     } else {
+-        const AnnotPolygon *polyann = static_cast<const AnnotPolygon *>(d->pdfAnnot);
++        const AnnotPolygon *polyann = static_cast<const AnnotPolygon *>(d->pdfAnnot.get());
+         if (polyann->getIntent() == AnnotPolygon::polygonCloud) {
+             return LineAnnotation::PolygonCloud;
+         } else { // AnnotPolygon::polylineDimension, AnnotPolygon::polygonDimension
+@@ -2947,10 +2934,10 @@ void LineAnnotation::setLineIntent(LineAnnotation::LineIntent intent)
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot);
++        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot.get());
+         lineann->setIntent((AnnotLine::AnnotLineIntent)(intent - 1));
+     } else {
+-        AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot);
++        AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot.get());
+         if (intent == LineAnnotation::PolygonCloud) {
+             polyann->setIntent(AnnotPolygon::polygonCloud);
+         } else // LineAnnotation::Dimension
+@@ -2969,8 +2956,8 @@ class GeomAnnotationPrivate : public AnnotationPrivate
+ {
+ public:
+     GeomAnnotationPrivate();
+-    Annotation *makeAlias() override;
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
++    std::unique_ptr<Annotation> makeAlias() override;
++    std::unique_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
+
+     // data fields (note uses border for rendering style)
+     GeomAnnotation::GeomType geomType;
+@@ -2979,15 +2966,15 @@ public:
+
+ GeomAnnotationPrivate::GeomAnnotationPrivate() : AnnotationPrivate(), geomType(GeomAnnotation::InscribedSquare) { }
+
+-Annotation *GeomAnnotationPrivate::makeAlias()
++std::unique_ptr<Annotation> GeomAnnotationPrivate::makeAlias()
+ {
+-    return new GeomAnnotation(*this);
++    return std::unique_ptr<GeomAnnotation>(new GeomAnnotation(*this));
+ }
+
+-Annot *GeomAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
++std::shared_ptr<Annot> GeomAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
+ {
+     // Setters are defined in the public class
+-    GeomAnnotation *q = static_cast<GeomAnnotation *>(makeAlias());
++    std::unique_ptr<GeomAnnotation> q = static_pointer_cast<GeomAnnotation *>(makeAlias());
+
+     // Set page and document
+     pdfPage = destPage;
+@@ -3002,13 +2989,12 @@ Annot *GeomAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *
+
+     // Set pdfAnnot
+     PDFRectangle rect = boundaryToPdfRectangle(boundary, flags);
+-    pdfAnnot = new AnnotGeometry(destPage->getDoc(), &rect, type);
++    pdfAnnot = std::make_shared<AnnotGeometry>(destPage->getDoc(), &rect, type);
+
+     // Set properties
+     flushBaseAnnotationProperties();
+     q->setGeomInnerColor(geomInnerColor);
+
+-    delete q;
+     return pdfAnnot;
+ }
+
+@@ -3089,7 +3075,7 @@ void GeomAnnotation::setGeomType(GeomAnnotation::GeomType type)
+         return;
+     }
+
+-    AnnotGeometry *geomann = static_cast<AnnotGeometry *>(d->pdfAnnot);
++    AnnotGeometry *geomann = static_cast<AnnotGeometry *>(d->pdfAnnot.get());
+     if (type == GeomAnnotation::InscribedSquare) {
+         geomann->setType(Annot::typeSquare);
+     } else { // GeomAnnotation::InscribedCircle
+@@ -3105,7 +3091,7 @@ QColor GeomAnnotation::geomInnerColor() const
+         return d->geomInnerColor;
+     }
+
+-    const AnnotGeometry *geomann = static_cast<const AnnotGeometry *>(d->pdfAnnot);
++    const AnnotGeometry *geomann = static_cast<const AnnotGeometry *>(d->pdfAnnot.get());
+     return convertAnnotColor(geomann->getInteriorColor());
+ }
+
+@@ -3118,7 +3104,7 @@ void GeomAnnotation::setGeomInnerColor(const QColor &color)
+         return;
+     }
+
+-    AnnotGeometry *geomann = static_cast<AnnotGeometry *>(d->pdfAnnot);
++    AnnotGeometry *geomann = static_cast<AnnotGeometry *>(d->pdfAnnot.get());
+     geomann->setInteriorColor(convertQColor(color));
+ }
+
+@@ -3127,8 +3113,8 @@ class HighlightAnnotationPrivate : public AnnotationPrivate
+ {
+ public:
+     HighlightAnnotationPrivate();
+-    Annotation *makeAlias() override;
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
++    std::unique_ptr<Annotation> makeAlias() override;
++    std::shared_ptr<Annot> *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
+
+     // data fields
+     HighlightAnnotation::HighlightType highlightType;
+@@ -3142,9 +3128,9 @@ public:
+
+ HighlightAnnotationPrivate::HighlightAnnotationPrivate() : AnnotationPrivate(), highlightType(HighlightAnnotation::Highlight) { }
+
+-Annotation *HighlightAnnotationPrivate::makeAlias()
++std::unique_ptr<Annotation> HighlightAnnotationPrivate::makeAlias()
+ {
+-    return new HighlightAnnotation(*this);
++    return std::unique_ptr<HighlightAnnotation>(new HighlightAnnotation(*this));
+ }
+
+ Annot::AnnotSubtype HighlightAnnotationPrivate::toAnnotSubType(HighlightAnnotation::HighlightType type)
+@@ -3217,10 +3203,10 @@ AnnotQuadrilaterals *HighlightAnnotationPrivate::toQuadrilaterals(const QList<Hi
+     return new AnnotQuadrilaterals(std::move(ac), count);
+ }
+
+-Annot *HighlightAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
++std::shared_ptr<Annot> HighlightAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
+ {
+     // Setters are defined in the public class
+-    HighlightAnnotation *q = static_cast<HighlightAnnotation *>(makeAlias());
++    std::unique_ptr<HighlightAnnotation> q = static_pointer_cast<HighlightAnnotation *>(makeAlias());
+
+     // Set page and document
+     pdfPage = destPage;
+@@ -3228,7 +3214,7 @@ Annot *HighlightAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentD
+
+     // Set pdfAnnot
+     PDFRectangle rect = boundaryToPdfRectangle(boundary, flags);
+-    pdfAnnot = new AnnotTextMarkup(destPage->getDoc(), &rect, toAnnotSubType(highlightType));
++    pdfAnnot = std::make_shared<AnnotTextMarkup>(destPage->getDoc(), &rect, toAnnotSubType(highlightType));
+
+     // Set properties
+     flushBaseAnnotationProperties();
+@@ -3236,8 +3222,6 @@ Annot *HighlightAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentD
+
+     highlightQuads.clear(); // Free up memory
+
+-    delete q;
+-
+     return pdfAnnot;
+ }
+
+@@ -3370,7 +3354,7 @@ void HighlightAnnotation::setHighlightType(HighlightAnnotation::HighlightType ty
+         return;
+     }
+
+-    AnnotTextMarkup *hlann = static_cast<AnnotTextMarkup *>(d->pdfAnnot);
++    AnnotTextMarkup *hlann = static_cast<AnnotTextMarkup *>(d->pdfAnnot.get());
+     hlann->setType(HighlightAnnotationPrivate::toAnnotSubType(type));
+ }
+
+@@ -3382,7 +3366,7 @@ QList<HighlightAnnotation::Quad> HighlightAnnotation::highlightQuads() const
+         return d->highlightQuads;
+     }
+
+-    const AnnotTextMarkup *hlann = static_cast<AnnotTextMarkup *>(d->pdfAnnot);
++    const AnnotTextMarkup *hlann = static_cast<AnnotTextMarkup *>(d->pdfAnnot.get());
+     return d->fromQuadrilaterals(hlann->getQuadrilaterals());
+ }
+
+@@ -3395,7 +3379,7 @@ void HighlightAnnotation::setHighlightQuads(const QList<HighlightAnnotation::Qua
+         return;
+     }
+
+-    AnnotTextMarkup *hlann = static_cast<AnnotTextMarkup *>(d->pdfAnnot);
++    AnnotTextMarkup *hlann = static_cast<AnnotTextMarkup *>(d->pdfAnnot.get());
+     AnnotQuadrilaterals *quadrilaterals = d->toQuadrilaterals(quads);
+     hlann->setQuadrilaterals(quadrilaterals);
+     delete quadrilaterals;
+@@ -3406,10 +3390,10 @@ class StampAnnotationPrivate : public AnnotationPrivate
+ {
+ public:
+     StampAnnotationPrivate();
+-    Annotation *makeAlias() override;
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
++    std::unique_ptr<Annotation> makeAlias() override;
++    std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
+
+-    AnnotStampImageHelper *convertQImageToAnnotStampImageHelper(const QImage &qimg);
++    std::unique_ptr<AnnotStampImageHelper> convertQImageToAnnotStampImageHelper(const QImage &qimg);
+
+     // data fields
+     QString stampIconName;
+@@ -3418,14 +3402,14 @@ public:
+
+ StampAnnotationPrivate::StampAnnotationPrivate() : AnnotationPrivate(), stampIconName(QStringLiteral("Draft")) { }
+
+-Annotation *StampAnnotationPrivate::makeAlias()
++std::unique_ptr<Annotation> StampAnnotationPrivate::makeAlias()
+ {
+-    return new StampAnnotation(*this);
++    return std::unique_ptr<StampAnnotation>(new StampAnnotation(*this));
+ }
+
+-Annot *StampAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
++std::shared_ptr<Annot> StampAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
+ {
+-    StampAnnotation *q = static_cast<StampAnnotation *>(makeAlias());
++    std::unique_ptr<StampAnnotation> q = static_pointer_cast<StampAnnotation *>(makeAlias());
+
+     // Set page and document
+     pdfPage = destPage;
+@@ -3433,21 +3417,19 @@ Annot *StampAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData
+
+     // Set pdfAnnot
+     PDFRectangle rect = boundaryToPdfRectangle(boundary, flags);
+-    pdfAnnot = new AnnotStamp(destPage->getDoc(), &rect);
++    pdfAnnot = std::make_shared<AnnotStamp>(destPage->getDoc(), &rect);
+
+     // Set properties
+     flushBaseAnnotationProperties();
+     q->setStampIconName(stampIconName);
+     q->setStampCustomImage(stampCustomImage);
+
+-    delete q;
+-
+     stampIconName.clear(); // Free up memory
+
+     return pdfAnnot;
+ }
+
+-AnnotStampImageHelper *StampAnnotationPrivate::convertQImageToAnnotStampImageHelper(const QImage &qimg)
++std::unique_ptr<AnnotStampImageHelper> StampAnnotationPrivate::convertQImageToAnnotStampImageHelper(const QImage &qimg)
+ {
+     QImage convertedQImage = qimg;
+
+@@ -3528,13 +3510,13 @@ AnnotStampImageHelper *StampAnnotationPrivate::convertQImageToAnnotStampImageHel
+
+     getRawDataFromQImage(convertedQImage, convertedQImage.depth(), &data, &sMaskData);
+
+-    AnnotStampImageHelper *annotImg;
++    std::unique_ptr<AnnotStampImageHelper> annotImg;
+
+     if (sMaskData.count() > 0) {
+         AnnotStampImageHelper sMask(parentDoc->doc, width, height, ColorSpace::DeviceGray, 8, sMaskData.data(), sMaskData.count());
+-        annotImg = new AnnotStampImageHelper(parentDoc->doc, width, height, colorSpace, bitsPerComponent, data.data(), data.count(), sMask.getRef());
++        annotImg = std::make_unique<AnnotStampImageHelper>(parentDoc->doc, width, height, colorSpace, bitsPerComponent, data.data(), data.count(), sMask.getRef());
+     } else {
+-        annotImg = new AnnotStampImageHelper(parentDoc->doc, width, height, colorSpace, bitsPerComponent, data.data(), data.count());
++        annotImg = std::make_unique<AnnotStampImageHelper>(parentDoc->doc, width, height, colorSpace, bitsPerComponent, data.data(), data.count());
+     }
+
+     return annotImg;
+@@ -3595,7 +3577,7 @@ QString StampAnnotation::stampIconName() const
+         return d->stampIconName;
+     }
+
+-    const AnnotStamp *stampann = static_cast<const AnnotStamp *>(d->pdfAnnot);
++    const AnnotStamp *stampann = static_cast<const AnnotStamp *>(d->pdfAnnot.get());
+     return QString::fromLatin1(stampann->getIcon()->c_str());
+ }
+
+@@ -3608,7 +3590,7 @@ void StampAnnotation::setStampIconName(const QString &name)
+         return;
+     }
+
+-    AnnotStamp *stampann = static_cast<AnnotStamp *>(d->pdfAnnot);
++    AnnotStamp *stampann = static_cast<AnnotStamp *>(d->pdfAnnot.get());
+     QByteArray encoded = name.toLatin1();
+     GooString s(encoded.constData());
+     stampann->setIcon(&s);
+@@ -3627,9 +3609,9 @@ void StampAnnotation::setStampCustomImage(const QImage &image)
+         return;
+     }
+
+-    AnnotStamp *stampann = static_cast<AnnotStamp *>(d->pdfAnnot);
+-    AnnotStampImageHelper *annotCustomImage = d->convertQImageToAnnotStampImageHelper(image);
+-    stampann->setCustomImage(annotCustomImage);
++    AnnotStamp *stampann = static_cast<AnnotStamp *>(d->pdfAnnot.get());
++    std::unique_ptr<AnnotStampImageHelper> annotCustomImage = d->convertQImageToAnnotStampImageHelper(image);
++    stampann->setCustomImage(std::move(annotCustomImage));
+ }
+
+ /** InkAnnotation [Annotation] */
+@@ -3637,8 +3619,8 @@ class InkAnnotationPrivate : public AnnotationPrivate
+ {
+ public:
+     InkAnnotationPrivate();
+-    Annotation *makeAlias() override;
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
++    std::unique_ptr<Annotation> makeAlias() override;
++    std::shared_ptr<Annot> *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
+
+     // data fields
+     QList<QLinkedList<QPointF>> inkPaths;
+@@ -3649,9 +3631,9 @@ public:
+
+ InkAnnotationPrivate::InkAnnotationPrivate() : AnnotationPrivate() { }
+
+-Annotation *InkAnnotationPrivate::makeAlias()
++std::unique_ptr<Annotation> *InkAnnotationPrivate::makeAlias()
+ {
+-    return new InkAnnotation(*this);
++    return std::unique_ptr<InkAnnotation>(new InkAnnotation(*this));
+ }
+
+ // Note: Caller is required to delete array elements and the array itself after use
+@@ -3665,10 +3647,10 @@ AnnotPath **InkAnnotationPrivate::toAnnotPaths(const QList<QLinkedList<QPointF>>
+     return res;
+ }
+
+-Annot *InkAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
++std::shared_ptr<Annot> *InkAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
+ {
+     // Setters are defined in the public class
+-    InkAnnotation *q = static_cast<InkAnnotation *>(makeAlias());
++    std::shared_ptr<InkAnnotation> q = static_pointer_cast<InkAnnotation *>(makeAlias());
+
+     // Set page and document
+     pdfPage = destPage;
+@@ -3676,7 +3658,7 @@ Annot *InkAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *d
+
+     // Set pdfAnnot
+     PDFRectangle rect = boundaryToPdfRectangle(boundary, flags);
+-    pdfAnnot = new AnnotInk(destPage->getDoc(), &rect);
++    pdfAnnot = std::make_shared<AnnotInk>(destPage->getDoc(), &rect);
+
+     // Set properties
+     flushBaseAnnotationProperties();
+@@ -3684,8 +3666,6 @@ Annot *InkAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *d
+
+     inkPaths.clear(); // Free up memory
+
+-    delete q;
+-
+     return pdfAnnot;
+ }
+
+@@ -3787,7 +3767,7 @@ QList<QLinkedList<QPointF>> InkAnnotation::inkPaths() const
+         return d->inkPaths;
+     }
+
+-    const AnnotInk *inkann = static_cast<const AnnotInk *>(d->pdfAnnot);
++    const AnnotInk *inkann = static_cast<const AnnotInk *>(d->pdfAnnot.get());
+
+     const AnnotPath *const *paths = inkann->getInkList();
+     if (!paths || !inkann->getInkListLength()) {
+@@ -3825,7 +3805,7 @@ void InkAnnotation::setInkPaths(const QList<QLinkedList<QPointF>> &paths)
+         return;
+     }
+
+-    AnnotInk *inkann = static_cast<AnnotInk *>(d->pdfAnnot);
++    AnnotInk *inkann = static_cast<AnnotInk *>(d->pdfAnnot.get());
+     AnnotPath **annotpaths = d->toAnnotPaths(paths);
+     const int pathsNumber = paths.size();
+     inkann->setInkList(annotpaths, pathsNumber);
+@@ -3842,8 +3822,8 @@ class LinkAnnotationPrivate : public AnnotationPrivate
+ public:
+     LinkAnnotationPrivate();
+     ~LinkAnnotationPrivate() override;
+-    Annotation *makeAlias() override;
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
++    std::unique_ptr<Annotation> makeAlias() override;
++    std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
+
+     // data fields
+     Link *linkDestination;
+@@ -3858,12 +3838,12 @@ LinkAnnotationPrivate::~LinkAnnotationPrivate()
+     delete linkDestination;
+ }
+
+-Annotation *LinkAnnotationPrivate::makeAlias()
++std::unique_ptr<Annotation> LinkAnnotationPrivate::makeAlias()
+ {
+-    return new LinkAnnotation(*this);
++    return std::unique_ptr<LinkAnnotation>(new LinkAnnotation(*this));
+ }
+
+-Annot *LinkAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
++std::shared_ptr<Annot> LinkAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
+ {
+     return nullptr; // Not implemented
+ }
+@@ -4144,8 +4124,8 @@ class CaretAnnotationPrivate : public AnnotationPrivate
+ {
+ public:
+     CaretAnnotationPrivate();
+-    Annotation *makeAlias() override;
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
++    std::unique_ptr<Annotation> makeAlias() override;
++    std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
+
+     // data fields
+     CaretAnnotation::CaretSymbol symbol;
+@@ -4174,15 +4154,15 @@ static CaretAnnotation::CaretSymbol caretSymbolFromString(const QString &symbol)
+
+ CaretAnnotationPrivate::CaretAnnotationPrivate() : AnnotationPrivate(), symbol(CaretAnnotation::None) { }
+
+-Annotation *CaretAnnotationPrivate::makeAlias()
++std::unique_ptr<Annotation> CaretAnnotationPrivate::makeAlias()
+ {
+-    return new CaretAnnotation(*this);
++    return std::unique_ptr<CaretAnnotation>(new CaretAnnotation(*this));
+ }
+
+-Annot *CaretAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
++std::shared_ptr<Annot> CaretAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
+ {
+     // Setters are defined in the public class
+-    CaretAnnotation *q = static_cast<CaretAnnotation *>(makeAlias());
++    std::unique_ptr<CaretAnnotation> q = static_pointer_cast<CaretAnnotation *>(makeAlias());
+
+     // Set page and document
+     pdfPage = destPage;
+@@ -4190,13 +4170,12 @@ Annot *CaretAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData
+
+     // Set pdfAnnot
+     PDFRectangle rect = boundaryToPdfRectangle(boundary, flags);
+-    pdfAnnot = new AnnotCaret(destPage->getDoc(), &rect);
++    pdfAnnot = std::make_shared<AnnotCaret>(destPage->getDoc(), &rect);
+
+     // Set properties
+     flushBaseAnnotationProperties();
+     q->setCaretSymbol(symbol);
+
+-    delete q;
+     return pdfAnnot;
+ }
+
+@@ -4255,7 +4234,7 @@ CaretAnnotation::CaretSymbol CaretAnnotation::caretSymbol() const
+         return d->symbol;
+     }
+
+-    const AnnotCaret *caretann = static_cast<const AnnotCaret *>(d->pdfAnnot);
++    const AnnotCaret *caretann = static_cast<const AnnotCaret *>(d->pdfAnnot.get());
+     return (CaretAnnotation::CaretSymbol)caretann->getSymbol();
+ }
+
+@@ -4268,7 +4247,7 @@ void CaretAnnotation::setCaretSymbol(CaretAnnotation::CaretSymbol symbol)
+         return;
+     }
+
+-    AnnotCaret *caretann = static_cast<AnnotCaret *>(d->pdfAnnot);
++    AnnotCaret *caretann = static_cast<AnnotCaret *>(d->pdfAnnot.get());
+     caretann->setSymbol((AnnotCaret::AnnotCaretSymbol)symbol);
+ }
+
+@@ -4278,8 +4257,8 @@ class FileAttachmentAnnotationPrivate : public AnnotationPrivate
+ public:
+     FileAttachmentAnnotationPrivate();
+     ~FileAttachmentAnnotationPrivate() override;
+-    Annotation *makeAlias() override;
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
++    std::unique_ptr<Annotation> makeAlias() override;
++    std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
+
+     // data fields
+     QString icon;
+@@ -4293,12 +4272,12 @@ FileAttachmentAnnotationPrivate::~FileAttachmentAnnotationPrivate()
+     delete embfile;
+ }
+
+-Annotation *FileAttachmentAnnotationPrivate::makeAlias()
++std::unique_ptr<Annotation> FileAttachmentAnnotationPrivate::makeAlias()
+ {
+-    return new FileAttachmentAnnotation(*this);
++    return std::unique_ptr<FileAttachmentAnnotation>(new FileAttachmentAnnotation(*this));
+ }
+
+-Annot *FileAttachmentAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
++std::shared_ptr<Annot> *FileAttachmentAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
+ {
+     return nullptr; // Not implemented
+ }
+@@ -4370,8 +4349,8 @@ class SoundAnnotationPrivate : public AnnotationPrivate
+ public:
+     SoundAnnotationPrivate();
+     ~SoundAnnotationPrivate() override;
+-    Annotation *makeAlias() override;
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
++    std::unique_ptr<Annotation> makeAlias() override;
++    std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
+
+     // data fields
+     QString icon;
+@@ -4385,12 +4364,12 @@ SoundAnnotationPrivate::~SoundAnnotationPrivate()
+     delete sound;
+ }
+
+-Annotation *SoundAnnotationPrivate::makeAlias()
++std::unique_ptr<Annotation> SoundAnnotationPrivate::makeAlias()
+ {
+-    return new SoundAnnotation(*this);
++    return std::unique_ptr<SoundAnnotation>(new SoundAnnotation(*this));
+ }
+
+-Annot *SoundAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
++std::shared_ptr<Annot> SoundAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
+ {
+     return nullptr; // Not implemented
+ }
+@@ -4462,8 +4441,8 @@ class MovieAnnotationPrivate : public AnnotationPrivate
+ public:
+     MovieAnnotationPrivate();
+     ~MovieAnnotationPrivate() override;
+-    Annotation *makeAlias() override;
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
++    std::unique_ptr<Annotation> makeAlias() override;
++    std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
+
+     // data fields
+     MovieObject *movie;
+@@ -4477,12 +4456,12 @@ MovieAnnotationPrivate::~MovieAnnotationPrivate()
+     delete movie;
+ }
+
+-Annotation *MovieAnnotationPrivate::makeAlias()
++std::unique_ptr<Annotation> MovieAnnotationPrivate::makeAlias()
+ {
+-    return new MovieAnnotation(*this);
++    return std::unique_ptr<Annotation>(new MovieAnnotation(*this));
+ }
+
+-Annot *MovieAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
++std::shared_ptr<Annot> MovieAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
+ {
+     return nullptr; // Not implemented
+ }
+@@ -4554,8 +4533,8 @@ class ScreenAnnotationPrivate : public AnnotationPrivate
+ public:
+     ScreenAnnotationPrivate();
+     ~ScreenAnnotationPrivate() override;
+-    Annotation *makeAlias() override;
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
++    std::unique_ptr<Annotation> makeAlias() override;
++    std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
+
+     // data fields
+     LinkRendition *action;
+@@ -4571,12 +4550,12 @@ ScreenAnnotationPrivate::~ScreenAnnotationPrivate()
+
+ ScreenAnnotation::ScreenAnnotation(ScreenAnnotationPrivate &dd) : Annotation(dd) { }
+
+-Annotation *ScreenAnnotationPrivate::makeAlias()
++std::unique_ptr<Annotation> ScreenAnnotationPrivate::makeAlias()
+ {
+-    return new ScreenAnnotation(*this);
++    return std::unique_ptr<ScreenAnnotation>(new ScreenAnnotation(*this));
+ }
+
+-Annot *ScreenAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
++std::shared_ptr<Annot> ScreenAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
+ {
+     return nullptr; // Not implemented
+ }
+@@ -4634,16 +4613,16 @@ Link *ScreenAnnotation::additionalAction(AdditionalActionType type) const
+ class WidgetAnnotationPrivate : public AnnotationPrivate
+ {
+ public:
+-    Annotation *makeAlias() override;
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
++    std::unique_ptr<Annotation> makeAlias() override;
++    std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
+ };
+
+-Annotation *WidgetAnnotationPrivate::makeAlias()
++std::unique_ptr<Annotation> WidgetAnnotationPrivate::makeAlias()
+ {
+-    return new WidgetAnnotation(*this);
++    return std::unique_ptr<WidgetAnnotation>(new WidgetAnnotation(*this));
+ }
+
+-Annot *WidgetAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
++std::shared_ptr<Annot> WidgetAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
+ {
+     return nullptr; // Not implemented
+ }
+@@ -4967,9 +4946,9 @@ public:
+
+     ~RichMediaAnnotationPrivate() override;
+
+-    Annotation *makeAlias() override { return new RichMediaAnnotation(*this); }
++    std::unique_ptr<Annotation> makeAlias() override { return std::unique_ptr<RichMediaAnnotation>(new RichMediaAnnotation(*this)); }
+
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override
++    std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override
+     {
+         Q_UNUSED(destPage);
+         Q_UNUSED(doc);
+diff --git a/qt5/src/poppler-form.cc b/qt5/src/poppler-form.cc
+index dfdcd39..760a891 100644
+--- a/qt5/src/poppler-form.cc
++++ b/qt5/src/poppler-form.cc
+@@ -269,7 +269,7 @@ Link *FormField::additionalAction(AdditionalActionType type) const
+
+ Link *FormField::additionalAction(Annotation::AdditionalActionType type) const
+ {
+-    ::AnnotWidget *w = m_formData->fm->getWidgetAnnotation();
++    std::shared_ptr<AnnotWidget> w = m_formData->fm->getWidgetAnnotation();
+     if (!w) {
+         return nullptr;
+     }
+@@ -350,7 +350,7 @@ void FormFieldButton::setIcon(const FormFieldIcon &icon)
+
+     FormWidgetButton *fwb = static_cast<FormWidgetButton *>(m_formData->fm);
+     if (fwb->getButtonType() == formButtonPush) {
+-        ::AnnotWidget *w = m_formData->fm->getWidgetAnnotation();
++        ::AnnotWidget *w = m_formData->fm->getWidgetAnnotation().get();
+         FormFieldIconData *data = FormFieldIconData::getData(icon);
+         if (data->icon != nullptr) {
+             w->setNewAppearance(data->icon->lookup("AP"));
+diff --git a/qt6/src/poppler-annotation-private.h b/qt6/src/poppler-annotation-private.h
+index 35e2676..11cda48 100644
+--- a/qt6/src/poppler-annotation-private.h
++++ b/qt6/src/poppler-annotation-private.h
+@@ -59,7 +59,7 @@ public:
+
+     /* Returns an Annotation of the right subclass whose d_ptr points to
+      * this AnnotationPrivate */
+-    virtual Annotation *makeAlias() = 0;
++    virtual std::unique_ptr<Annotation> makeAlias() = 0;
+
+     /* properties: contents related */
+     QString author;
+@@ -79,18 +79,18 @@ public:
+     /* revisions */
+     Annotation::RevScope revisionScope;
+     Annotation::RevType revisionType;
+-    QList<Annotation *> revisions;
++    std::vector<std::unique_ptr<Annotation>> revisions;
+
+     /* After this call, the Annotation object will behave like a wrapper for
+      * the specified Annot object. All cached values are discarded */
+-    void tieToNativeAnnot(Annot *ann, ::Page *page, DocumentData *doc);
++    void tieToNativeAnnot(std::shared_ptr<Annot> ann, ::Page *page, DocumentData *doc);
+
+     /* Creates a new Annot object on the specified page, flushes current
+      * values to that object and ties this Annotation to that object */
+-    virtual Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) = 0;
++    virtual std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) = 0;
+
+     /* Inited to 0 (i.e. untied annotation) */
+-    Annot *pdfAnnot;
++    std::shared_ptr<Annot> pdfAnnot;
+     ::Page *pdfPage;
+     DocumentData *parentDoc;
+
+diff --git a/qt6/src/poppler-annotation.cc b/qt6/src/poppler-annotation.cc
+index 1a3c813..c1fcdb6 100644
+--- a/qt6/src/poppler-annotation.cc
++++ b/qt6/src/poppler-annotation.cc
+@@ -57,6 +57,12 @@
+ #include <Link.h>
+ #include <DateInfo.h>
+
++template<typename T, typename U>
++static std::unique_ptr<T> static_pointer_cast(std::unique_ptr<U> &&in)
++{
++    return std::unique_ptr<T>(static_cast<std::add_pointer_t<T>>(in.release()));
++}
++
+ /* Almost all getters directly query the underlying poppler annotation, with
+  * the exceptions of link, file attachment, sound, movie and screen annotations,
+  * Whose data retrieval logic has not been moved yet. Their getters return
+@@ -129,36 +135,25 @@ void getRawDataFromQImage(const QImage &qimg, int bitsPerPixel, QByteArray *data
+ void AnnotationPrivate::addRevision(Annotation *ann, Annotation::RevScope scope, Annotation::RevType type)
+ {
+     /* Since ownership stays with the caller, create an alias of ann */
+-    revisions.append(ann->d_ptr->makeAlias());
++    revisions.push_back(ann->d_ptr->makeAlias());
+
+     /* Set revision properties */
+     revisionScope = scope;
+     revisionType = type;
+ }
+
+-AnnotationPrivate::~AnnotationPrivate()
+-{
+-    // Delete all children revisions
+-    qDeleteAll(revisions);
+-
+-    // Release Annot object
+-    if (pdfAnnot) {
+-        pdfAnnot->decRefCnt();
+-    }
+-}
++AnnotationPrivate::~AnnotationPrivate() = default;
+
+-void AnnotationPrivate::tieToNativeAnnot(Annot *ann, ::Page *page, Poppler::DocumentData *doc)
++void AnnotationPrivate::tieToNativeAnnot(std::shared_ptr<Annot> ann, ::Page *page, Poppler::DocumentData *doc)
+ {
+     if (pdfAnnot) {
+         error(errIO, -1, "Annotation is already tied");
+         return;
+     }
+
+-    pdfAnnot = ann;
++    pdfAnnot = std::move(ann);
+     pdfPage = page;
+     parentDoc = doc;
+-
+-    pdfAnnot->incRefCnt();
+ }
+
+ /* This method is called when a new annotation is created, after pdfAnnot and
+@@ -167,7 +162,7 @@ void AnnotationPrivate::flushBaseAnnotationProperties()
+ {
+     Q_ASSERT(pdfPage);
+
+-    Annotation *q = makeAlias(); // Setters are defined in the public class
++    std::unique_ptr<Annotation> q = makeAlias(); // Setters are defined in the public class
+
+     // Since pdfAnnot has been set, this calls will write in the Annot object
+     q->setAuthor(author);
+@@ -180,14 +175,6 @@ void AnnotationPrivate::flushBaseAnnotationProperties()
+     q->setStyle(style);
+     q->setPopup(popup);
+
+-    // Flush revisions
+-    foreach (Annotation *r, revisions) {
+-        // TODO: Flush revision
+-        delete r; // Object is no longer needed
+-    }
+-
+-    delete q;
+-
+     // Clear some members to save memory
+     author.clear();
+     contents.clear();
+@@ -377,14 +364,14 @@ std::vector<std::unique_ptr<Annotation>> AnnotationPrivate::findAnnotations(::Pa
+
+     // Create Annotation objects and tie to their native Annot
+     std::vector<std::unique_ptr<Annotation>> res;
+-    for (Annot *ann : annots->getAnnots()) {
++    for (const std::shared_ptr<Annot> &ann : annots->getAnnots()) {
+         if (!ann) {
+             error(errInternal, -1, "Annot is null");
+             continue;
+         }
+
+         // Check parent annotation
+-        AnnotMarkup *markupann = dynamic_cast<AnnotMarkup *>(ann);
++        AnnotMarkup *markupann = dynamic_cast<AnnotMarkup *>(ann.get());
+         if (!markupann) {
+             // Assume it's a root annotation, and skip if user didn't request it
+             if (parentID != -1) {
+@@ -458,7 +445,7 @@ std::vector<std::unique_ptr<Annotation>> AnnotationPrivate::findAnnotations(::Pa
+                 continue;
+             }
+             // parse Link params
+-            AnnotLink *linkann = static_cast<AnnotLink *>(ann);
++            AnnotLink *linkann = static_cast<AnnotLink *>(ann.get());
+             LinkAnnotation *l = new LinkAnnotation();
+
+             // -> hlMode
+@@ -488,7 +475,7 @@ std::vector<std::unique_ptr<Annotation>> AnnotationPrivate::findAnnotations(::Pa
+             if (!wantFileAttachmentAnnotations) {
+                 continue;
+             }
+-            AnnotFileAttachment *attachann = static_cast<AnnotFileAttachment *>(ann);
++            AnnotFileAttachment *attachann = static_cast<AnnotFileAttachment *>(ann.get());
+             FileAttachmentAnnotation *f = new FileAttachmentAnnotation();
+             // -> fileIcon
+             f->setFileIconName(QString::fromLatin1(attachann->getName()->c_str()));
+@@ -503,7 +490,7 @@ std::vector<std::unique_ptr<Annotation>> AnnotationPrivate::findAnnotations(::Pa
+             if (!wantSoundAnnotations) {
+                 continue;
+             }
+-            AnnotSound *soundann = static_cast<AnnotSound *>(ann);
++            AnnotSound *soundann = static_cast<AnnotSound *>(ann.get());
+             SoundAnnotation *s = new SoundAnnotation();
+
+             // -> soundIcon
+@@ -518,7 +505,7 @@ std::vector<std::unique_ptr<Annotation>> AnnotationPrivate::findAnnotations(::Pa
+             if (!wantMovieAnnotations) {
+                 continue;
+             }
+-            AnnotMovie *movieann = static_cast<AnnotMovie *>(ann);
++            AnnotMovie *movieann = static_cast<AnnotMovie *>(ann.get());
+             MovieAnnotation *m = new MovieAnnotation();
+
+             // -> movie
+@@ -536,7 +523,7 @@ std::vector<std::unique_ptr<Annotation>> AnnotationPrivate::findAnnotations(::Pa
+             if (!wantScreenAnnotations) {
+                 continue;
+             }
+-            AnnotScreen *screenann = static_cast<AnnotScreen *>(ann);
++            AnnotScreen *screenann = static_cast<AnnotScreen *>(ann.get());
+             // TODO Support other link types than Link::Rendition in ScreenAnnotation
+             if (!screenann->getAction() || screenann->getAction()->getKind() != actionRendition) {
+                 continue;
+@@ -566,7 +553,7 @@ std::vector<std::unique_ptr<Annotation>> AnnotationPrivate::findAnnotations(::Pa
+             annotation.reset(new WidgetAnnotation());
+             break;
+         case Annot::typeRichMedia: {
+-            const AnnotRichMedia *annotRichMedia = static_cast<AnnotRichMedia *>(ann);
++            const AnnotRichMedia *annotRichMedia = static_cast<AnnotRichMedia *>(ann.get());
+
+             RichMediaAnnotation *richMediaAnnotation = new RichMediaAnnotation;
+
+@@ -774,9 +761,9 @@ std::unique_ptr<Link> AnnotationPrivate::additionalAction(Annotation::Additional
+
+     std::unique_ptr<::LinkAction> linkAction;
+     if (pdfAnnot->getType() == Annot::typeScreen) {
+-        linkAction = static_cast<AnnotScreen *>(pdfAnnot)->getAdditionalAction(actionType);
++        linkAction = static_cast<AnnotScreen *>(pdfAnnot.get())->getAdditionalAction(actionType);
+     } else {
+-        linkAction = static_cast<AnnotWidget *>(pdfAnnot)->getAdditionalAction(actionType);
++        linkAction = static_cast<AnnotWidget *>(pdfAnnot.get())->getAdditionalAction(actionType);
+     }
+
+     if (linkAction) {
+@@ -795,7 +782,7 @@ void AnnotationPrivate::addAnnotationToPage(::Page *pdfPage, DocumentData *doc,
+
+     // Unimplemented annotations can't be created by the user because their ctor
+     // is private. Therefore, createNativeAnnot will never return 0
+-    Annot *nativeAnnot = ann->d_ptr->createNativeAnnot(pdfPage, doc);
++    std::shared_ptr<Annot> nativeAnnot = ann->d_ptr->createNativeAnnot(pdfPage, doc);
+     Q_ASSERT(nativeAnnot);
+
+     if (ann->d_ptr->annotationAppearance.isStream()) {
+@@ -828,8 +815,8 @@ class TextAnnotationPrivate : public AnnotationPrivate
+ {
+ public:
+     TextAnnotationPrivate();
+-    Annotation *makeAlias() override;
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
++    std::unique_ptr<Annotation> makeAlias() override;
++    std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
+     void setDefaultAppearanceToNative();
+     std::unique_ptr<DefaultAppearance> getDefaultAppearanceFromNative() const;
+
+@@ -1057,7 +1044,7 @@ QString Annotation::author() const
+         return d->author;
+     }
+
+-    const AnnotMarkup *markupann = dynamic_cast<const AnnotMarkup *>(d->pdfAnnot);
++    const AnnotMarkup *markupann = dynamic_cast<const AnnotMarkup *>(d->pdfAnnot.get());
+     return markupann ? UnicodeParsedString(markupann->getLabel()) : QString();
+ }
+
+@@ -1070,7 +1057,7 @@ void Annotation::setAuthor(const QString &author)
+         return;
+     }
+
+-    AnnotMarkup *markupann = dynamic_cast<AnnotMarkup *>(d->pdfAnnot);
++    AnnotMarkup *markupann = dynamic_cast<AnnotMarkup *>(d->pdfAnnot.get());
+     if (markupann) {
+         markupann->setLabel(std::unique_ptr<GooString>(QStringToUnicodeGooString(author)));
+     }
+@@ -1173,7 +1160,7 @@ QDateTime Annotation::creationDate() const
+         return d->creationDate;
+     }
+
+-    const AnnotMarkup *markupann = dynamic_cast<const AnnotMarkup *>(d->pdfAnnot);
++    const AnnotMarkup *markupann = dynamic_cast<const AnnotMarkup *>(d->pdfAnnot.get());
+
+     if (markupann && markupann->getDate()) {
+         return convertDate(markupann->getDate()->c_str());
+@@ -1191,7 +1178,7 @@ void Annotation::setCreationDate(const QDateTime &date)
+         return;
+     }
+
+-    AnnotMarkup *markupann = dynamic_cast<AnnotMarkup *>(d->pdfAnnot);
++    AnnotMarkup *markupann = dynamic_cast<AnnotMarkup *>(d->pdfAnnot.get());
+     if (markupann) {
+         if (date.isValid()) {
+             const time_t t = date.toSecsSinceEpoch();
+@@ -1325,7 +1312,7 @@ Annotation::Style Annotation::style() const
+     Style s;
+     s.setColor(convertAnnotColor(d->pdfAnnot->getColor()));
+
+-    const AnnotMarkup *markupann = dynamic_cast<const AnnotMarkup *>(d->pdfAnnot);
++    const AnnotMarkup *markupann = dynamic_cast<const AnnotMarkup *>(d->pdfAnnot.get());
+     if (markupann) {
+         s.setOpacity(markupann->getOpacity());
+     }
+@@ -1348,11 +1335,11 @@ Annotation::Style Annotation::style() const
+     AnnotBorderEffect *border_effect;
+     switch (d->pdfAnnot->getType()) {
+     case Annot::typeFreeText:
+-        border_effect = static_cast<AnnotFreeText *>(d->pdfAnnot)->getBorderEffect();
++        border_effect = static_cast<AnnotFreeText *>(d->pdfAnnot.get())->getBorderEffect();
+         break;
+     case Annot::typeSquare:
+     case Annot::typeCircle:
+-        border_effect = static_cast<AnnotGeometry *>(d->pdfAnnot)->getBorderEffect();
++        border_effect = static_cast<AnnotGeometry *>(d->pdfAnnot.get())->getBorderEffect();
+         break;
+     default:
+         border_effect = nullptr;
+@@ -1376,7 +1363,7 @@ void Annotation::setStyle(const Annotation::Style &style)
+
+     d->pdfAnnot->setColor(convertQColor(style.color()));
+
+-    AnnotMarkup *markupann = dynamic_cast<AnnotMarkup *>(d->pdfAnnot);
++    AnnotMarkup *markupann = dynamic_cast<AnnotMarkup *>(d->pdfAnnot.get());
+     if (markupann) {
+         markupann->setOpacity(style.opacity());
+     }
+@@ -1397,10 +1384,10 @@ Annotation::Popup Annotation::popup() const
+     }
+
+     Popup w;
+-    AnnotPopup *popup = nullptr;
++    std::shared_ptr<AnnotPopup> popup = nullptr;
+     int flags = -1; // Not initialized
+
+-    const AnnotMarkup *markupann = dynamic_cast<const AnnotMarkup *>(d->pdfAnnot);
++    const AnnotMarkup *markupann = dynamic_cast<const AnnotMarkup *>(d->pdfAnnot.get());
+     if (markupann) {
+         popup = markupann->getPopup();
+         w.setSummary(UnicodeParsedString(markupann->getSubject()));
+@@ -1418,7 +1405,7 @@ Annotation::Popup Annotation::popup() const
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeText) {
+-        const AnnotText *textann = static_cast<const AnnotText *>(d->pdfAnnot);
++        const AnnotText *textann = static_cast<const AnnotText *>(d->pdfAnnot.get());
+
+         // Text annotations default to same rect as annotation
+         if (flags == -1) {
+@@ -1474,7 +1461,7 @@ Annotation::RevScope Annotation::revisionScope() const
+         return d->revisionScope;
+     }
+
+-    const AnnotMarkup *markupann = dynamic_cast<const AnnotMarkup *>(d->pdfAnnot);
++    const AnnotMarkup *markupann = dynamic_cast<const AnnotMarkup *>(d->pdfAnnot.get());
+
+     if (markupann && markupann->isInReplyTo()) {
+         switch (markupann->getReplyTo()) {
+@@ -1496,7 +1483,7 @@ Annotation::RevType Annotation::revisionType() const
+         return d->revisionType;
+     }
+
+-    const AnnotText *textann = dynamic_cast<const AnnotText *>(d->pdfAnnot);
++    const AnnotText *textann = dynamic_cast<const AnnotText *>(d->pdfAnnot.get());
+
+     if (textann && textann->isInReplyTo()) {
+         switch (textann->getState()) {
+@@ -1527,8 +1514,10 @@ std::vector<std::unique_ptr<Annotation>> Annotation::revisions() const
+     if (!d->pdfAnnot) {
+         /* Return aliases, whose ownership goes to the caller */
+         std::vector<std::unique_ptr<Annotation>> res;
+-        foreach (Annotation *rev, d->revisions)
+-            res.push_back(std::unique_ptr<Annotation>(rev->d_ptr->makeAlias()));
++        res.reserve(d->revisions.size());
++        for (const std::unique_ptr<Annotation> &rev : d->revisions) {
++            res.push_back(rev->d_ptr->makeAlias());
++        }
+         return res;
+     }
+
+@@ -1545,7 +1534,7 @@ std::unique_ptr<AnnotationAppearance> Annotation::annotationAppearance() const
+ {
+     Q_D(const Annotation);
+
+-    return std::make_unique<AnnotationAppearance>(new AnnotationAppearancePrivate(d->pdfAnnot));
++    return std::make_unique<AnnotationAppearance>(new AnnotationAppearancePrivate(d->pdfAnnot.get()));
+ }
+
+ void Annotation::setAnnotationAppearance(const AnnotationAppearance &annotationAppearance)
+@@ -1568,15 +1557,15 @@ void Annotation::setAnnotationAppearance(const AnnotationAppearance &annotationA
+ /** TextAnnotation [Annotation] */
+ TextAnnotationPrivate::TextAnnotationPrivate() : AnnotationPrivate(), textType(TextAnnotation::Linked), textIcon(QStringLiteral("Note")), inplaceAlign(TextAnnotation::InplaceAlignLeft), inplaceIntent(TextAnnotation::Unknown) { }
+
+-Annotation *TextAnnotationPrivate::makeAlias()
++std::unique_ptr<Annotation> TextAnnotationPrivate::makeAlias()
+ {
+-    return new TextAnnotation(*this);
++    return std::unique_ptr<Annotation>(new TextAnnotation(*this));
+ }
+
+-Annot *TextAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
++std::shared_ptr<Annot> TextAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
+ {
+     // Setters are defined in the public class
+-    TextAnnotation *q = static_cast<TextAnnotation *>(makeAlias());
++    std::unique_ptr<TextAnnotation> q = static_pointer_cast<TextAnnotation>(makeAlias());
+
+     // Set page and contents
+     pdfPage = destPage;
+@@ -1585,13 +1574,13 @@ Annot *TextAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *
+     // Set pdfAnnot
+     PDFRectangle rect = boundaryToPdfRectangle(boundary, flags);
+     if (textType == TextAnnotation::Linked) {
+-        pdfAnnot = new AnnotText { destPage->getDoc(), &rect };
++        pdfAnnot = std::make_shared<AnnotText>(destPage->getDoc(), &rect);
+     } else {
+         const double pointSize = textFont ? textFont->pointSizeF() : AnnotFreeText::undefinedFontPtSize;
+         if (pointSize < 0) {
+             qWarning() << "TextAnnotationPrivate::createNativeAnnot: font pointSize < 0";
+         }
+-        pdfAnnot = new AnnotFreeText { destPage->getDoc(), &rect };
++        pdfAnnot = std::make_shared<AnnotFreeText>(destPage->getDoc(), &rect);
+     }
+
+     // Set properties
+@@ -1601,8 +1590,6 @@ Annot *TextAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *
+     q->setCalloutPoints(inplaceCallout);
+     q->setInplaceIntent(inplaceIntent);
+
+-    delete q;
+-
+     inplaceCallout.clear(); // Free up memory
+
+     setDefaultAppearanceToNative();
+@@ -1613,7 +1600,7 @@ Annot *TextAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *
+ void TextAnnotationPrivate::setDefaultAppearanceToNative()
+ {
+     if (pdfAnnot && pdfAnnot->getType() == Annot::typeFreeText) {
+-        AnnotFreeText *ftextann = static_cast<AnnotFreeText *>(pdfAnnot);
++        AnnotFreeText *ftextann = static_cast<AnnotFreeText *>(pdfAnnot.get());
+         const double pointSize = textFont ? textFont->pointSizeF() : AnnotFreeText::undefinedFontPtSize;
+         if (pointSize < 0) {
+             qWarning() << "TextAnnotationPrivate::createNativeAnnot: font pointSize < 0";
+@@ -1642,7 +1629,7 @@ void TextAnnotationPrivate::setDefaultAppearanceToNative()
+ std::unique_ptr<DefaultAppearance> TextAnnotationPrivate::getDefaultAppearanceFromNative() const
+ {
+     if (pdfAnnot && pdfAnnot->getType() == Annot::typeFreeText) {
+-        AnnotFreeText *ftextann = static_cast<AnnotFreeText *>(pdfAnnot);
++        AnnotFreeText *ftextann = static_cast<AnnotFreeText *>(pdfAnnot.get());
+         return ftextann->getDefaultAppearance();
+     } else {
+         return {};
+@@ -1696,7 +1683,7 @@ QString TextAnnotation::textIcon() const
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeText) {
+-        const AnnotText *textann = static_cast<const AnnotText *>(d->pdfAnnot);
++        const AnnotText *textann = static_cast<const AnnotText *>(d->pdfAnnot.get());
+         return QString::fromLatin1(textann->getIcon()->c_str());
+     }
+
+@@ -1713,7 +1700,7 @@ void TextAnnotation::setTextIcon(const QString &icon)
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeText) {
+-        AnnotText *textann = static_cast<AnnotText *>(d->pdfAnnot);
++        AnnotText *textann = static_cast<AnnotText *>(d->pdfAnnot.get());
+         QByteArray encoded = icon.toLatin1();
+         GooString s(encoded.constData());
+         textann->setIcon(&s);
+@@ -1787,7 +1774,7 @@ TextAnnotation::InplaceAlignPosition TextAnnotation::inplaceAlign() const
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeFreeText) {
+-        const AnnotFreeText *ftextann = static_cast<const AnnotFreeText *>(d->pdfAnnot);
++        const AnnotFreeText *ftextann = static_cast<const AnnotFreeText *>(d->pdfAnnot.get());
+         switch (ftextann->getQuadding()) {
+         case VariableTextQuadding::leftJustified:
+             return InplaceAlignLeft;
+@@ -1824,7 +1811,7 @@ void TextAnnotation::setInplaceAlign(InplaceAlignPosition align)
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeFreeText) {
+-        AnnotFreeText *ftextann = static_cast<AnnotFreeText *>(d->pdfAnnot);
++        AnnotFreeText *ftextann = static_cast<AnnotFreeText *>(d->pdfAnnot.get());
+         ftextann->setQuadding(alignToQuadding(align));
+     }
+ }
+@@ -1851,7 +1838,7 @@ QVector<QPointF> TextAnnotation::calloutPoints() const
+         return QVector<QPointF>();
+     }
+
+-    const AnnotFreeText *ftextann = static_cast<const AnnotFreeText *>(d->pdfAnnot);
++    const AnnotFreeText *ftextann = static_cast<const AnnotFreeText *>(d->pdfAnnot.get());
+     const AnnotCalloutLine *callout = ftextann->getCalloutLine();
+
+     if (!callout) {
+@@ -1883,7 +1870,7 @@ void TextAnnotation::setCalloutPoints(const QVector<QPointF> &points)
+         return;
+     }
+
+-    AnnotFreeText *ftextann = static_cast<AnnotFreeText *>(d->pdfAnnot);
++    AnnotFreeText *ftextann = static_cast<AnnotFreeText *>(d->pdfAnnot.get());
+     const int count = points.size();
+
+     if (count == 0) {
+@@ -1896,7 +1883,7 @@ void TextAnnotation::setCalloutPoints(const QVector<QPointF> &points)
+         return;
+     }
+
+-    AnnotCalloutLine *callout;
++    std::unique_ptr<AnnotCalloutLine> *callout;
+     double x1, y1, x2, y2;
+     double MTX[6];
+     d->fillTransformationMTX(MTX);
+@@ -1906,13 +1893,12 @@ void TextAnnotation::setCalloutPoints(const QVector<QPointF> &points)
+     if (count == 3) {
+         double x3, y3;
+         XPDFReader::invTransform(MTX, points[2], x3, y3);
+-        callout = new AnnotCalloutMultiLine(x1, y1, x2, y2, x3, y3);
++        callout = std::make_unique<AnnotCalloutMultiLine>(x1, y1, x2, y2, x3, y3);
+     } else {
+-        callout = new AnnotCalloutLine(x1, y1, x2, y2);
++        callout = std::make_unique<AnnotCalloutLine>(x1, y1, x2, y2);
+     }
+
+-    ftextann->setCalloutLine(callout);
+-    delete callout;
++    ftextann->setCalloutLine(std::move(callout));
+ }
+
+ TextAnnotation::InplaceIntent TextAnnotation::inplaceIntent() const
+@@ -1924,7 +1910,7 @@ TextAnnotation::InplaceIntent TextAnnotation::inplaceIntent() const
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeFreeText) {
+-        const AnnotFreeText *ftextann = static_cast<const AnnotFreeText *>(d->pdfAnnot);
++        const AnnotFreeText *ftextann = static_cast<const AnnotFreeText *>(d->pdfAnnot.get());
+         return (TextAnnotation::InplaceIntent)ftextann->getIntent();
+     }
+
+@@ -1941,7 +1927,7 @@ void TextAnnotation::setInplaceIntent(TextAnnotation::InplaceIntent intent)
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeFreeText) {
+-        AnnotFreeText *ftextann = static_cast<AnnotFreeText *>(d->pdfAnnot);
++        AnnotFreeText *ftextann = static_cast<AnnotFreeText *>(d->pdfAnnot.get());
+         ftextann->setIntent((AnnotFreeText::AnnotFreeTextIntent)intent);
+     }
+ }
+@@ -1951,8 +1937,8 @@ class LineAnnotationPrivate : public AnnotationPrivate
+ {
+ public:
+     LineAnnotationPrivate();
+-    Annotation *makeAlias() override;
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
++    std::unique_ptr<Annotation> makeAlias() override;
++    std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
+
+     // data fields (note uses border for rendering style)
+     QVector<QPointF> linePoints;
+@@ -1972,15 +1958,15 @@ LineAnnotationPrivate::LineAnnotationPrivate()
+ {
+ }
+
+-Annotation *LineAnnotationPrivate::makeAlias()
++std::unique_ptr<Annotation> LineAnnotationPrivate::makeAlias()
+ {
+-    return new LineAnnotation(*this);
++    return std::unique_ptr<Annotation>(new LineAnnotation(*this));
+ }
+
+-Annot *LineAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
++std::shared_ptr<Annot> LineAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
+ {
+     // Setters are defined in the public class
+-    LineAnnotation *q = static_cast<LineAnnotation *>(makeAlias());
++    std::unique_ptr<LineAnnotation> q = static_pointer_cast<LineAnnotation *>(makeAlias());
+
+     // Set page and document
+     pdfPage = destPage;
+@@ -1989,9 +1975,9 @@ Annot *LineAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *
+     // Set pdfAnnot
+     PDFRectangle rect = boundaryToPdfRectangle(boundary, flags);
+     if (lineType == LineAnnotation::StraightLine) {
+-        pdfAnnot = new AnnotLine(doc->doc, &rect);
++        pdfAnnot = std::make_shared<AnnotLine>(doc->doc, &rect);
+     } else {
+-        pdfAnnot = new AnnotPolygon(doc->doc, &rect, lineClosed ? Annot::typePolygon : Annot::typePolyLine);
++        pdfAnnot = std::make_shared<AnnotPolygon>(doc->doc, &rect, lineClosed ? Annot::typePolygon : Annot::typePolyLine);
+     }
+
+     // Set properties
+@@ -2005,8 +1991,6 @@ Annot *LineAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *
+     q->setLineShowCaption(lineShowCaption);
+     q->setLineIntent(lineIntent);
+
+-    delete q;
+-
+     linePoints.clear(); // Free up memory
+
+     return pdfAnnot;
+@@ -2063,14 +2047,14 @@ QVector<QPointF> LineAnnotation::linePoints() const
+
+     QVector<QPointF> res;
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot);
++        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot.get());
+         QPointF p;
+         XPDFReader::transform(MTX, lineann->getX1(), lineann->getY1(), p);
+         res.append(p);
+         XPDFReader::transform(MTX, lineann->getX2(), lineann->getY2(), p);
+         res.append(p);
+     } else {
+-        const AnnotPolygon *polyann = static_cast<const AnnotPolygon *>(d->pdfAnnot);
++        const AnnotPolygon *polyann = static_cast<const AnnotPolygon *>(d->pdfAnnot.get());
+         const AnnotPath *vertices = polyann->getVertices();
+
+         for (int i = 0; i < vertices->getCoordsLength(); ++i) {
+@@ -2093,7 +2077,7 @@ void LineAnnotation::setLinePoints(const QVector<QPointF> &points)
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot);
++        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot.get());
+         if (points.size() != 2) {
+             error(errSyntaxError, -1, "Expected two points for a straight line");
+             return;
+@@ -2105,7 +2089,7 @@ void LineAnnotation::setLinePoints(const QVector<QPointF> &points)
+         XPDFReader::invTransform(MTX, points.last(), x2, y2);
+         lineann->setVertices(x1, y1, x2, y2);
+     } else {
+-        AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot);
++        AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot.get());
+         AnnotPath *p = d->toAnnotPath(points);
+         polyann->setVertices(p);
+         delete p;
+@@ -2121,10 +2105,10 @@ LineAnnotation::TermStyle LineAnnotation::lineStartStyle() const
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot);
++        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot.get());
+         return (LineAnnotation::TermStyle)lineann->getStartStyle();
+     } else {
+-        const AnnotPolygon *polyann = static_cast<const AnnotPolygon *>(d->pdfAnnot);
++        const AnnotPolygon *polyann = static_cast<const AnnotPolygon *>(d->pdfAnnot.get());
+         return (LineAnnotation::TermStyle)polyann->getStartStyle();
+     }
+ }
+@@ -2139,10 +2123,10 @@ void LineAnnotation::setLineStartStyle(LineAnnotation::TermStyle style)
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot);
++        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot.get());
+         lineann->setStartEndStyle((AnnotLineEndingStyle)style, lineann->getEndStyle());
+     } else {
+-        AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot);
++        AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot.get());
+         polyann->setStartEndStyle((AnnotLineEndingStyle)style, polyann->getEndStyle());
+     }
+ }
+@@ -2156,10 +2140,10 @@ LineAnnotation::TermStyle LineAnnotation::lineEndStyle() const
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot);
++        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot.get());
+         return (LineAnnotation::TermStyle)lineann->getEndStyle();
+     } else {
+-        const AnnotPolygon *polyann = static_cast<const AnnotPolygon *>(d->pdfAnnot);
++        const AnnotPolygon *polyann = static_cast<const AnnotPolygon *>(d->pdfAnnot.get());
+         return (LineAnnotation::TermStyle)polyann->getEndStyle();
+     }
+ }
+@@ -2174,10 +2158,10 @@ void LineAnnotation::setLineEndStyle(LineAnnotation::TermStyle style)
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot);
++        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot.get());
+         lineann->setStartEndStyle(lineann->getStartStyle(), (AnnotLineEndingStyle)style);
+     } else {
+-        AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot);
++        AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot.get());
+         polyann->setStartEndStyle(polyann->getStartStyle(), (AnnotLineEndingStyle)style);
+     }
+ }
+@@ -2203,7 +2187,7 @@ void LineAnnotation::setLineClosed(bool closed)
+     }
+
+     if (d->pdfAnnot->getType() != Annot::typeLine) {
+-        AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot);
++        AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot.get());
+
+         // Set new subtype and switch intent if necessary
+         if (closed) {
+@@ -2231,10 +2215,10 @@ QColor LineAnnotation::lineInnerColor() const
+     AnnotColor *c;
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot);
++        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot.get());
+         c = lineann->getInteriorColor();
+     } else {
+-        const AnnotPolygon *polyann = static_cast<const AnnotPolygon *>(d->pdfAnnot);
++        const AnnotPolygon *polyann = static_cast<const AnnotPolygon *>(d->pdfAnnot.get());
+         c = polyann->getInteriorColor();
+     }
+
+@@ -2253,10 +2237,10 @@ void LineAnnotation::setLineInnerColor(const QColor &color)
+     auto c = convertQColor(color);
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot);
++        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot.get());
+         lineann->setInteriorColor(std::move(c));
+     } else {
+-        AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot);
++        AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot.get());
+         polyann->setInteriorColor(std::move(c));
+     }
+ }
+@@ -2270,7 +2254,7 @@ double LineAnnotation::lineLeadingForwardPoint() const
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot);
++        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot.get());
+         return lineann->getLeaderLineLength();
+     }
+
+@@ -2287,7 +2271,7 @@ void LineAnnotation::setLineLeadingForwardPoint(double point)
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot);
++        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot.get());
+         lineann->setLeaderLineLength(point);
+     }
+ }
+@@ -2301,7 +2285,7 @@ double LineAnnotation::lineLeadingBackPoint() const
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot);
++        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot.get());
+         return lineann->getLeaderLineExtension();
+     }
+
+@@ -2318,7 +2302,7 @@ void LineAnnotation::setLineLeadingBackPoint(double point)
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot);
++        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot.get());
+         lineann->setLeaderLineExtension(point);
+     }
+ }
+@@ -2332,7 +2316,7 @@ bool LineAnnotation::lineShowCaption() const
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot);
++        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot.get());
+         return lineann->getCaption();
+     }
+
+@@ -2349,7 +2333,7 @@ void LineAnnotation::setLineShowCaption(bool show)
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot);
++        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot.get());
+         lineann->setCaption(show);
+     }
+ }
+@@ -2363,10 +2347,10 @@ LineAnnotation::LineIntent LineAnnotation::lineIntent() const
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot);
++        const AnnotLine *lineann = static_cast<const AnnotLine *>(d->pdfAnnot.get());
+         return (LineAnnotation::LineIntent)(lineann->getIntent() + 1);
+     } else {
+-        const AnnotPolygon *polyann = static_cast<const AnnotPolygon *>(d->pdfAnnot);
++        const AnnotPolygon *polyann = static_cast<const AnnotPolygon *>(d->pdfAnnot.get());
+         if (polyann->getIntent() == AnnotPolygon::polygonCloud) {
+             return LineAnnotation::PolygonCloud;
+         } else { // AnnotPolygon::polylineDimension, AnnotPolygon::polygonDimension
+@@ -2389,10 +2373,10 @@ void LineAnnotation::setLineIntent(LineAnnotation::LineIntent intent)
+     }
+
+     if (d->pdfAnnot->getType() == Annot::typeLine) {
+-        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot);
++        AnnotLine *lineann = static_cast<AnnotLine *>(d->pdfAnnot.get());
+         lineann->setIntent((AnnotLine::AnnotLineIntent)(intent - 1));
+     } else {
+-        AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot);
++        AnnotPolygon *polyann = static_cast<AnnotPolygon *>(d->pdfAnnot.get());
+         if (intent == LineAnnotation::PolygonCloud) {
+             polyann->setIntent(AnnotPolygon::polygonCloud);
+         } else // LineAnnotation::Dimension
+@@ -2411,8 +2395,8 @@ class GeomAnnotationPrivate : public AnnotationPrivate
+ {
+ public:
+     GeomAnnotationPrivate();
+-    Annotation *makeAlias() override;
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
++    std::unique_ptr<Annotation> makeAlias() override;
++    std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
+
+     // data fields (note uses border for rendering style)
+     GeomAnnotation::GeomType geomType;
+@@ -2421,15 +2405,15 @@ public:
+
+ GeomAnnotationPrivate::GeomAnnotationPrivate() : AnnotationPrivate(), geomType(GeomAnnotation::InscribedSquare) { }
+
+-Annotation *GeomAnnotationPrivate::makeAlias()
++std::unique_ptr<Annotation> GeomAnnotationPrivate::makeAlias()
+ {
+-    return new GeomAnnotation(*this);
++    return std::unique_ptr<Annotation>(new GeomAnnotation(*this));
+ }
+
+-Annot *GeomAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
++std::shared_ptr<Annot> GeomAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
+ {
+     // Setters are defined in the public class
+-    GeomAnnotation *q = static_cast<GeomAnnotation *>(makeAlias());
++    std::unique_ptr<GeomAnnotation> q = static_pointer_cast<GeomAnnotation *>(makeAlias());
+
+     // Set page and document
+     pdfPage = destPage;
+@@ -2444,13 +2428,12 @@ Annot *GeomAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *
+
+     // Set pdfAnnot
+     PDFRectangle rect = boundaryToPdfRectangle(boundary, flags);
+-    pdfAnnot = new AnnotGeometry(destPage->getDoc(), &rect, type);
++    pdfAnnot = std::make_shared<AnnotGeometry>(destPage->getDoc(), &rect, type);
+
+     // Set properties
+     flushBaseAnnotationProperties();
+     q->setGeomInnerColor(geomInnerColor);
+
+-    delete q;
+     return pdfAnnot;
+ }
+
+@@ -2489,7 +2472,7 @@ void GeomAnnotation::setGeomType(GeomAnnotation::GeomType type)
+         return;
+     }
+
+-    AnnotGeometry *geomann = static_cast<AnnotGeometry *>(d->pdfAnnot);
++    AnnotGeometry *geomann = static_cast<AnnotGeometry *>(d->pdfAnnot.get());
+     if (type == GeomAnnotation::InscribedSquare) {
+         geomann->setType(Annot::typeSquare);
+     } else { // GeomAnnotation::InscribedCircle
+@@ -2505,7 +2488,7 @@ QColor GeomAnnotation::geomInnerColor() const
+         return d->geomInnerColor;
+     }
+
+-    const AnnotGeometry *geomann = static_cast<const AnnotGeometry *>(d->pdfAnnot);
++    const AnnotGeometry *geomann = static_cast<const AnnotGeometry *>(d->pdfAnnot.get());
+     return convertAnnotColor(geomann->getInteriorColor());
+ }
+
+@@ -2518,7 +2501,7 @@ void GeomAnnotation::setGeomInnerColor(const QColor &color)
+         return;
+     }
+
+-    AnnotGeometry *geomann = static_cast<AnnotGeometry *>(d->pdfAnnot);
++    AnnotGeometry *geomann = static_cast<AnnotGeometry *>(d->pdfAnnot.get());
+     geomann->setInteriorColor(convertQColor(color));
+ }
+
+@@ -2527,8 +2510,8 @@ class HighlightAnnotationPrivate : public AnnotationPrivate
+ {
+ public:
+     HighlightAnnotationPrivate();
+-    Annotation *makeAlias() override;
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
++    std::unique_ptr<Annotation> makeAlias() override;
++    std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
+
+     // data fields
+     HighlightAnnotation::HighlightType highlightType;
+@@ -2542,9 +2525,9 @@ public:
+
+ HighlightAnnotationPrivate::HighlightAnnotationPrivate() : AnnotationPrivate(), highlightType(HighlightAnnotation::Highlight) { }
+
+-Annotation *HighlightAnnotationPrivate::makeAlias()
++std::unique_ptr<Annotation> HighlightAnnotationPrivate::makeAlias()
+ {
+-    return new HighlightAnnotation(*this);
++    return std::unique_ptr<Annotation>(new HighlightAnnotation(*this));
+ }
+
+ Annot::AnnotSubtype HighlightAnnotationPrivate::toAnnotSubType(HighlightAnnotation::HighlightType type)
+@@ -2617,10 +2600,10 @@ AnnotQuadrilaterals *HighlightAnnotationPrivate::toQuadrilaterals(const QList<Hi
+     return new AnnotQuadrilaterals(std::move(ac), count);
+ }
+
+-Annot *HighlightAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
++std::shared_ptr<Annot> HighlightAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
+ {
+     // Setters are defined in the public class
+-    HighlightAnnotation *q = static_cast<HighlightAnnotation *>(makeAlias());
++    std::unique_ptr<HighlightAnnotation> q = static_pointer_cast<HighlightAnnotation *>(makeAlias());
+
+     // Set page and document
+     pdfPage = destPage;
+@@ -2628,7 +2611,7 @@ Annot *HighlightAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentD
+
+     // Set pdfAnnot
+     PDFRectangle rect = boundaryToPdfRectangle(boundary, flags);
+-    pdfAnnot = new AnnotTextMarkup(destPage->getDoc(), &rect, toAnnotSubType(highlightType));
++    pdfAnnot = std::make_shared<AnnotTextMarkup>(destPage->getDoc(), &rect, toAnnotSubType(highlightType));
+
+     // Set properties
+     flushBaseAnnotationProperties();
+@@ -2636,8 +2619,6 @@ Annot *HighlightAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentD
+
+     highlightQuads.clear(); // Free up memory
+
+-    delete q;
+-
+     return pdfAnnot;
+ }
+
+@@ -2682,7 +2663,7 @@ void HighlightAnnotation::setHighlightType(HighlightAnnotation::HighlightType ty
+         return;
+     }
+
+-    AnnotTextMarkup *hlann = static_cast<AnnotTextMarkup *>(d->pdfAnnot);
++    AnnotTextMarkup *hlann = static_cast<AnnotTextMarkup *>(d->pdfAnnot.get());
+     hlann->setType(HighlightAnnotationPrivate::toAnnotSubType(type));
+ }
+
+@@ -2694,7 +2675,7 @@ QList<HighlightAnnotation::Quad> HighlightAnnotation::highlightQuads() const
+         return d->highlightQuads;
+     }
+
+-    const AnnotTextMarkup *hlann = static_cast<AnnotTextMarkup *>(d->pdfAnnot);
++    const AnnotTextMarkup *hlann = static_cast<AnnotTextMarkup *>(d->pdfAnnot.get());
+     return d->fromQuadrilaterals(hlann->getQuadrilaterals());
+ }
+
+@@ -2707,7 +2688,7 @@ void HighlightAnnotation::setHighlightQuads(const QList<HighlightAnnotation::Qua
+         return;
+     }
+
+-    AnnotTextMarkup *hlann = static_cast<AnnotTextMarkup *>(d->pdfAnnot);
++    AnnotTextMarkup *hlann = static_cast<AnnotTextMarkup *>(d->pdfAnnot.get());
+     AnnotQuadrilaterals *quadrilaterals = d->toQuadrilaterals(quads);
+     hlann->setQuadrilaterals(quadrilaterals);
+     delete quadrilaterals;
+@@ -2718,10 +2699,10 @@ class StampAnnotationPrivate : public AnnotationPrivate
+ {
+ public:
+     StampAnnotationPrivate();
+-    Annotation *makeAlias() override;
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
++    std::unique_ptr<Annotation> makeAlias() override;
++    std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
+
+-    AnnotStampImageHelper *convertQImageToAnnotStampImageHelper(const QImage &qimg);
++    std::unique_ptr<AnnotStampImageHelper> convertQImageToAnnotStampImageHelper(const QImage &qimg);
+
+     // data fields
+     QString stampIconName;
+@@ -2730,14 +2711,14 @@ public:
+
+ StampAnnotationPrivate::StampAnnotationPrivate() : AnnotationPrivate(), stampIconName(QStringLiteral("Draft")) { }
+
+-Annotation *StampAnnotationPrivate::makeAlias()
++std::unique_ptr<Annotation> StampAnnotationPrivate::makeAlias()
+ {
+-    return new StampAnnotation(*this);
++    return std::unique_ptr<Annotation>(new StampAnnotation(*this));
+ }
+
+-Annot *StampAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
++std::shared_ptr<Annot> StampAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
+ {
+-    StampAnnotation *q = static_cast<StampAnnotation *>(makeAlias());
++    std::shared_ptr<StampAnnotation> q = static_pointer_cast<StampAnnotation *>(makeAlias());
+
+     // Set page and document
+     pdfPage = destPage;
+@@ -2745,21 +2726,19 @@ Annot *StampAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData
+
+     // Set pdfAnnot
+     PDFRectangle rect = boundaryToPdfRectangle(boundary, flags);
+-    pdfAnnot = new AnnotStamp(destPage->getDoc(), &rect);
++    pdfAnnot = std::make_shared<AnnotStamp>(destPage->getDoc(), &rect);
+
+     // Set properties
+     flushBaseAnnotationProperties();
+     q->setStampIconName(stampIconName);
+     q->setStampCustomImage(stampCustomImage);
+
+-    delete q;
+-
+     stampIconName.clear(); // Free up memory
+
+     return pdfAnnot;
+ }
+
+-AnnotStampImageHelper *StampAnnotationPrivate::convertQImageToAnnotStampImageHelper(const QImage &qimg)
++std::unique_ptr<AnnotStampImageHelper> *StampAnnotationPrivate::convertQImageToAnnotStampImageHelper(const QImage &qimg)
+ {
+     QImage convertedQImage = qimg;
+
+@@ -2838,13 +2817,13 @@ AnnotStampImageHelper *StampAnnotationPrivate::convertQImageToAnnotStampImageHel
+
+     getRawDataFromQImage(convertedQImage, convertedQImage.depth(), &data, &sMaskData);
+
+-    AnnotStampImageHelper *annotImg;
++    std::unique_ptr<AnnotStampImageHelper> annotImg;
+
+     if (sMaskData.size() > 0) {
+         AnnotStampImageHelper sMask(parentDoc->doc, width, height, ColorSpace::DeviceGray, 8, sMaskData.data(), sMaskData.size());
+-        annotImg = new AnnotStampImageHelper(parentDoc->doc, width, height, colorSpace, bitsPerComponent, data.data(), data.size(), sMask.getRef());
++        annotImg = std::make_unique<AnnotStampImageHelper>(parentDoc->doc, width, height, colorSpace, bitsPerComponent, data.data(), data.size(), sMask.getRef());
+     } else {
+-        annotImg = new AnnotStampImageHelper(parentDoc->doc, width, height, colorSpace, bitsPerComponent, data.data(), data.size());
++        annotImg = std::make_unique<AnnotStampImageHelper>(parentDoc->doc, width, height, colorSpace, bitsPerComponent, data.data(), data.size());
+     }
+
+     return annotImg;
+@@ -2869,7 +2848,7 @@ QString StampAnnotation::stampIconName() const
+         return d->stampIconName;
+     }
+
+-    const AnnotStamp *stampann = static_cast<const AnnotStamp *>(d->pdfAnnot);
++    const AnnotStamp *stampann = static_cast<const AnnotStamp *>(d->pdfAnnot.get());
+     return QString::fromLatin1(stampann->getIcon()->c_str());
+ }
+
+@@ -2882,7 +2861,7 @@ void StampAnnotation::setStampIconName(const QString &name)
+         return;
+     }
+
+-    AnnotStamp *stampann = static_cast<AnnotStamp *>(d->pdfAnnot);
++    AnnotStamp *stampann = static_cast<AnnotStamp *>(d->pdfAnnot.get());
+     QByteArray encoded = name.toLatin1();
+     GooString s(encoded.constData());
+     stampann->setIcon(&s);
+@@ -2901,9 +2880,9 @@ void StampAnnotation::setStampCustomImage(const QImage &image)
+         return;
+     }
+
+-    AnnotStamp *stampann = static_cast<AnnotStamp *>(d->pdfAnnot);
+-    AnnotStampImageHelper *annotCustomImage = d->convertQImageToAnnotStampImageHelper(image);
+-    stampann->setCustomImage(annotCustomImage);
++    AnnotStamp *stampann = static_cast<AnnotStamp *>(d->pdfAnnot.get());
++    std::unique_ptr<AnnotStampImageHelper> annotCustomImage = d->convertQImageToAnnotStampImageHelper(image);
++    stampann->setCustomImage(std::move(annotCustomImage));
+ }
+
+ /** InkAnnotation [Annotation] */
+@@ -2911,8 +2890,8 @@ class InkAnnotationPrivate : public AnnotationPrivate
+ {
+ public:
+     InkAnnotationPrivate();
+-    Annotation *makeAlias() override;
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
++    std::unique_ptr<Annotation> makeAlias() override;
++    std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
+
+     // data fields
+     QList<QVector<QPointF>> inkPaths;
+@@ -2923,9 +2902,9 @@ public:
+
+ InkAnnotationPrivate::InkAnnotationPrivate() : AnnotationPrivate() { }
+
+-Annotation *InkAnnotationPrivate::makeAlias()
++std::unique_ptr<Annotation> *InkAnnotationPrivate::makeAlias()
+ {
+-    return new InkAnnotation(*this);
++    return std::unique_ptr<Annotation>(new InkAnnotation(*this));
+ }
+
+ // Note: Caller is required to delete array elements and the array itself after use
+@@ -2939,10 +2918,10 @@ AnnotPath **InkAnnotationPrivate::toAnnotPaths(const QList<QVector<QPointF>> &pa
+     return res;
+ }
+
+-Annot *InkAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
++std::shared_ptr<Annot> InkAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
+ {
+     // Setters are defined in the public class
+-    InkAnnotation *q = static_cast<InkAnnotation *>(makeAlias());
++    std::unique_ptr<InkAnnotation> q = static_pointer_cast<InkAnnotation *>(makeAlias());
+
+     // Set page and document
+     pdfPage = destPage;
+@@ -2950,7 +2929,7 @@ Annot *InkAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *d
+
+     // Set pdfAnnot
+     PDFRectangle rect = boundaryToPdfRectangle(boundary, flags);
+-    pdfAnnot = new AnnotInk(destPage->getDoc(), &rect);
++    pdfAnnot = std::make_shared<AnnotInk>(destPage->getDoc(), &rect);
+
+     // Set properties
+     flushBaseAnnotationProperties();
+@@ -2958,8 +2937,6 @@ Annot *InkAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *d
+
+     inkPaths.clear(); // Free up memory
+
+-    delete q;
+-
+     return pdfAnnot;
+ }
+
+@@ -2982,7 +2959,7 @@ QList<QVector<QPointF>> InkAnnotation::inkPaths() const
+         return d->inkPaths;
+     }
+
+-    const AnnotInk *inkann = static_cast<const AnnotInk *>(d->pdfAnnot);
++    const AnnotInk *inkann = static_cast<const AnnotInk *>(d->pdfAnnot.get());
+
+     const AnnotPath *const *paths = inkann->getInkList();
+     if (!paths || !inkann->getInkListLength()) {
+@@ -3020,7 +2997,7 @@ void InkAnnotation::setInkPaths(const QList<QVector<QPointF>> &paths)
+         return;
+     }
+
+-    AnnotInk *inkann = static_cast<AnnotInk *>(d->pdfAnnot);
++    AnnotInk *inkann = static_cast<AnnotInk *>(d->pdfAnnot.get());
+     AnnotPath **annotpaths = d->toAnnotPaths(paths);
+     const int pathsNumber = paths.size();
+     inkann->setInkList(annotpaths, pathsNumber);
+@@ -3037,8 +3014,8 @@ class LinkAnnotationPrivate : public AnnotationPrivate
+ public:
+     LinkAnnotationPrivate();
+     ~LinkAnnotationPrivate() override;
+-    Annotation *makeAlias() override;
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
++    std::unique_ptr<Annotation> makeAlias() override;
++    std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
+
+     // data fields
+     std::unique_ptr<Link> linkDestination;
+@@ -3050,12 +3027,12 @@ LinkAnnotationPrivate::LinkAnnotationPrivate() : AnnotationPrivate(), linkHLMode
+
+ LinkAnnotationPrivate::~LinkAnnotationPrivate() { }
+
+-Annotation *LinkAnnotationPrivate::makeAlias()
++std::unique_ptr<Annotation> LinkAnnotationPrivate::makeAlias()
+ {
+-    return new LinkAnnotation(*this);
++    return std::unique_ptr<Annotation>(new LinkAnnotation(*this));
+ }
+
+-Annot *LinkAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
++std::unique_ptr<Annot> *LinkAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
+ {
+     return nullptr; // Not implemented
+ }
+@@ -3076,7 +3053,6 @@ Link *LinkAnnotation::linkDestination() const
+     Q_D(const LinkAnnotation);
+     return d->linkDestination.get();
+ }
+-
+ void LinkAnnotation::setLinkDestination(std::unique_ptr<Link> &&link)
+ {
+     Q_D(LinkAnnotation);
+@@ -3120,8 +3096,8 @@ class CaretAnnotationPrivate : public AnnotationPrivate
+ {
+ public:
+     CaretAnnotationPrivate();
+-    Annotation *makeAlias() override;
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
++    std::unique_ptr<Annotation> makeAlias() override;
++    std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
+
+     // data fields
+     CaretAnnotation::CaretSymbol symbol;
+@@ -3129,15 +3105,15 @@ public:
+
+ CaretAnnotationPrivate::CaretAnnotationPrivate() : AnnotationPrivate(), symbol(CaretAnnotation::None) { }
+
+-Annotation *CaretAnnotationPrivate::makeAlias()
++std::unique_ptr<Annotation> CaretAnnotationPrivate::makeAlias()
+ {
+-    return new CaretAnnotation(*this);
++    return std::unique_ptr<Annotation>(new CaretAnnotation(*this));
+ }
+
+-Annot *CaretAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
++std::shared_ptr<Annot> CaretAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
+ {
+     // Setters are defined in the public class
+-    CaretAnnotation *q = static_cast<CaretAnnotation *>(makeAlias());
++    std::unique_ptr<CaretAnnotation> q = static_pointer_cast<CaretAnnotation *>(makeAlias());
+
+     // Set page and document
+     pdfPage = destPage;
+@@ -3145,13 +3121,12 @@ Annot *CaretAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData
+
+     // Set pdfAnnot
+     PDFRectangle rect = boundaryToPdfRectangle(boundary, flags);
+-    pdfAnnot = new AnnotCaret(destPage->getDoc(), &rect);
++    pdfAnnot = std::make_shared<AnnotCaret>(destPage->getDoc(), &rect);
+
+     // Set properties
+     flushBaseAnnotationProperties();
+     q->setCaretSymbol(symbol);
+
+-    delete q;
+     return pdfAnnot;
+ }
+
+@@ -3174,7 +3149,7 @@ CaretAnnotation::CaretSymbol CaretAnnotation::caretSymbol() const
+         return d->symbol;
+     }
+
+-    const AnnotCaret *caretann = static_cast<const AnnotCaret *>(d->pdfAnnot);
++    const AnnotCaret *caretann = static_cast<const AnnotCaret *>(d->pdfAnnot.get());
+     return (CaretAnnotation::CaretSymbol)caretann->getSymbol();
+ }
+
+@@ -3187,7 +3162,7 @@ void CaretAnnotation::setCaretSymbol(CaretAnnotation::CaretSymbol symbol)
+         return;
+     }
+
+-    AnnotCaret *caretann = static_cast<AnnotCaret *>(d->pdfAnnot);
++    AnnotCaret *caretann = static_cast<AnnotCaret *>(d->pdfAnnot.get());
+     caretann->setSymbol((AnnotCaret::AnnotCaretSymbol)symbol);
+ }
+
+@@ -3197,8 +3172,8 @@ class FileAttachmentAnnotationPrivate : public AnnotationPrivate
+ public:
+     FileAttachmentAnnotationPrivate();
+     ~FileAttachmentAnnotationPrivate() override;
+-    Annotation *makeAlias() override;
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
++    std::unique_ptr<Annotation> makeAlias() override;
++    std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
+
+     // data fields
+     QString icon;
+@@ -3212,12 +3187,12 @@ FileAttachmentAnnotationPrivate::~FileAttachmentAnnotationPrivate()
+     delete embfile;
+ }
+
+-Annotation *FileAttachmentAnnotationPrivate::makeAlias()
++std::unique_ptr<Annotation> FileAttachmentAnnotationPrivate::makeAlias()
+ {
+-    return new FileAttachmentAnnotation(*this);
++    return std::unique_ptr<Annotation>(new FileAttachmentAnnotation(*this));
+ }
+
+-Annot *FileAttachmentAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
++std::shared_ptr<Annot> *FileAttachmentAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
+ {
+     return nullptr; // Not implemented
+ }
+@@ -3263,8 +3238,8 @@ class SoundAnnotationPrivate : public AnnotationPrivate
+ public:
+     SoundAnnotationPrivate();
+     ~SoundAnnotationPrivate() override;
+-    Annotation *makeAlias() override;
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
++    std::unique_ptr<Annotation> makeAlias() override;
++    std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
+
+     // data fields
+     QString icon;
+@@ -3278,12 +3253,12 @@ SoundAnnotationPrivate::~SoundAnnotationPrivate()
+     delete sound;
+ }
+
+-Annotation *SoundAnnotationPrivate::makeAlias()
++std::unique_ptr<Annotation> SoundAnnotationPrivate::makeAlias()
+ {
+-    return new SoundAnnotation(*this);
++    return std::unique_ptr<Annotation>(new SoundAnnotation(*this));
+ }
+
+-Annot *SoundAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
++std::shared_ptr<Annot> SoundAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
+ {
+     return nullptr; // Not implemented
+ }
+@@ -3329,8 +3304,8 @@ class MovieAnnotationPrivate : public AnnotationPrivate
+ public:
+     MovieAnnotationPrivate();
+     ~MovieAnnotationPrivate() override;
+-    Annotation *makeAlias() override;
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
++    std::unique_ptr<Annotation> makeAlias() override;
++    std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
+
+     // data fields
+     MovieObject *movie;
+@@ -3344,12 +3319,12 @@ MovieAnnotationPrivate::~MovieAnnotationPrivate()
+     delete movie;
+ }
+
+-Annotation *MovieAnnotationPrivate::makeAlias()
++std::unique_ptr<Annotation> MovieAnnotationPrivate::makeAlias()
+ {
+-    return new MovieAnnotation(*this);
++    return std::unique_ptr<Annotation>(new MovieAnnotation(*this));
+ }
+
+-Annot *MovieAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
++std::shared_ptr<Annot> MovieAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
+ {
+     return nullptr; // Not implemented
+ }
+@@ -3395,8 +3370,8 @@ class ScreenAnnotationPrivate : public AnnotationPrivate
+ public:
+     ScreenAnnotationPrivate();
+     ~ScreenAnnotationPrivate() override;
+-    Annotation *makeAlias() override;
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
++    std::unique_ptr<Annotation> makeAlias() override;
++    std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
+
+     // data fields
+     LinkRendition *action;
+@@ -3412,12 +3387,12 @@ ScreenAnnotationPrivate::~ScreenAnnotationPrivate()
+
+ ScreenAnnotation::ScreenAnnotation(ScreenAnnotationPrivate &dd) : Annotation(dd) { }
+
+-Annotation *ScreenAnnotationPrivate::makeAlias()
++std::unique_ptr<Annotation> ScreenAnnotationPrivate::makeAlias()
+ {
+-    return new ScreenAnnotation(*this);
++    return std::unique_ptr<Annotation>(new ScreenAnnotation(*this));
+ }
+
+-Annot *ScreenAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
++std::shared_ptr<Annot> ScreenAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
+ {
+     return nullptr; // Not implemented
+ }
+@@ -3465,16 +3440,16 @@ std::unique_ptr<Link> ScreenAnnotation::additionalAction(AdditionalActionType ty
+ class WidgetAnnotationPrivate : public AnnotationPrivate
+ {
+ public:
+-    Annotation *makeAlias() override;
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override;
++    std::unique_ptr<Annotation> makeAlias() override;
++    std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override;
+ };
+
+-Annotation *WidgetAnnotationPrivate::makeAlias()
++std::unique_ptr<Annotation> WidgetAnnotationPrivate::makeAlias()
+ {
+-    return new WidgetAnnotation(*this);
++    return std::unique_ptr<Annotation>(new WidgetAnnotation(*this));
+ }
+
+-Annot *WidgetAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
++std::shared_ptr<Annot> WidgetAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
+ {
+     return nullptr; // Not implemented
+ }
+@@ -3788,9 +3763,9 @@ public:
+
+     ~RichMediaAnnotationPrivate() override;
+
+-    Annotation *makeAlias() override { return new RichMediaAnnotation(*this); }
++    std::unique_ptr<Annotation> makeAlias() override { return std::unique_ptr<Annotation>(new RichMediaAnnotation(*this)); }
+
+-    Annot *createNativeAnnot(::Page *destPage, DocumentData *doc) override
++    std::shared_ptr<Annot> createNativeAnnot(::Page *destPage, DocumentData *doc) override
+     {
+         Q_UNUSED(destPage);
+         Q_UNUSED(doc);
+diff --git a/qt6/src/poppler-form.cc b/qt6/src/poppler-form.cc
+index b415c08..74ba075 100644
+--- a/qt6/src/poppler-form.cc
++++ b/qt6/src/poppler-form.cc
+@@ -269,7 +269,7 @@ std::unique_ptr<Link> FormField::additionalAction(AdditionalActionType type) con
+
+ std::unique_ptr<Link> FormField::additionalAction(Annotation::AdditionalActionType type) const
+ {
+-    ::AnnotWidget *w = m_formData->fm->getWidgetAnnotation();
++    ::AnnotWidget *w = m_formData->fm->getWidgetAnnotation().get();
+     if (!w) {
+         return {};
+     }
+@@ -350,7 +350,7 @@ void FormFieldButton::setIcon(const FormFieldIcon &icon)
+
+     FormWidgetButton *fwb = static_cast<FormWidgetButton *>(m_formData->fm);
+     if (fwb->getButtonType() == formButtonPush) {
+-        ::AnnotWidget *w = m_formData->fm->getWidgetAnnotation();
++        std::shared_ptr<::AnnotWidget> w = m_formData->fm->getWidgetAnnotation();
+         FormFieldIconData *data = FormFieldIconData::getData(icon);
+         if (data->icon != nullptr) {
+             w->setNewAppearance(data->icon->lookup("AP"));
+diff --git a/utils/HtmlOutputDev.cc b/utils/HtmlOutputDev.cc
+index b45a5ff..ec51a24 100644
+--- a/utils/HtmlOutputDev.cc
++++ b/utils/HtmlOutputDev.cc
+@@ -1252,8 +1252,8 @@ void HtmlOutputDev::startPage(int pageNumA, GfxState *state, XRef *xref)
+ void HtmlOutputDev::endPage()
+ {
+     std::unique_ptr<Links> linksList = docPage->getLinks();
+-    for (AnnotLink *link : linksList->getLinks()) {
+-        doProcessLink(link);
++    for (const std::shared_ptr<AnnotLink> &link : linksList->getLinks()) {
++        doProcessLink(link.get());
+     }
+
+     pages->conv();
+diff --git a/utils/pdfdetach.cc b/utils/pdfdetach.cc
+index 247c2c1..cbb4f30 100644
+--- a/utils/pdfdetach.cc
++++ b/utils/pdfdetach.cc
+@@ -150,11 +150,11 @@ int main(int argc, char *argv[])
+             break;
+         }
+
+-        for (Annot *annot : annots->getAnnots()) {
++        for (const std::shared_ptr<Annot> &annot : annots->getAnnots()) {
+             if (annot->getType() != Annot::typeFileAttachment) {
+                 continue;
+             }
+-            embeddedFiles.push_back(std::make_unique<FileSpec>(static_cast<AnnotFileAttachment *>(annot)->getFile()));
++            embeddedFiles.push_back(std::make_unique<FileSpec>(static_cast<AnnotFileAttachment *>(annot.get())->getFile()));
+         }
+     }
+
+diff --git a/utils/pdfinfo.cc b/utils/pdfinfo.cc
+index 5f96b41..7b3896e 100644
+--- a/utils/pdfinfo.cc
++++ b/utils/pdfinfo.cc
+@@ -415,7 +415,7 @@ static void printUrlList(PDFDoc *doc)
+         Page *page = doc->getPage(pg);
+         if (page) {
+             std::unique_ptr<Links> links = page->getLinks();
+-            for (AnnotLink *annot : links->getLinks()) {
++            for (const std::shared_ptr<AnnotLink> &annot : links->getLinks()) {
+                 LinkAction *action = annot->getAction();
+                 if (action->getKind() == actionURI) {
+                     LinkURI *linkUri = dynamic_cast<LinkURI *>(action);
+--
+2.40.0
diff --git a/meta-oe/recipes-support/poppler/poppler/CVE-2025-52886-0002.patch b/meta-oe/recipes-support/poppler/poppler/CVE-2025-52886-0002.patch
new file mode 100644
index 0000000000..d8668699c1
--- /dev/null
+++ b/meta-oe/recipes-support/poppler/poppler/CVE-2025-52886-0002.patch
@@ -0,0 +1,58 @@ 
+From ac36affcc8486de38e8905a8d6547a3464ff46e5 Mon Sep 17 00:00:00 2001
+From: Sune Vuorela <sune@vuorela.dk>
+Date: Tue, 3 Jun 2025 00:35:19 +0200
+Subject: [PATCH] Limit ammount of annots per document/page
+
+CVE: CVE-2025-52886
+Upstream-Status: Backport [https://gitlab.freedesktop.org/poppler/poppler/-/commit/ac36affcc8486de38e8905a8d6547a3464ff46e5
+
+Signed-off-by: Yogita Urade <yogita.urade@windriver.com>
+---
+ poppler/Annot.cc |  4 ++++
+ poppler/Page.cc  | 16 ++++++++++++++++
+ 2 files changed, 20 insertions(+)
+
+diff --git a/poppler/Annot.cc b/poppler/Annot.cc
+index b98df5d..3e9dfac 100644
+--- a/poppler/Annot.cc
++++ b/poppler/Annot.cc
+@@ -7450,6 +7450,10 @@ Annots::Annots(PDFDoc *docA, int page, Object *annotsObj)
+                 const Object &obj2 = annotsObj->arrayGetNF(i);
+                 std::shared_ptr<Annot> annot = createAnnot(std::move(obj1), &obj2);
+                 if (annot) {
++                    if (annot.use_count() > 100000) {
++                        error(errSyntaxError, -1, "Annotations likely malformed. Too many references. Stopping processing annots on page {0:d}", page);
++                        break;
++                    }
+                     if (annot->isOk()) {
+                         annot->setPage(page, false); // Don't change /P
+                         appendAnnot(annot);
+diff --git a/poppler/Page.cc b/poppler/Page.cc
+index 234f124..858b128 100644
+--- a/poppler/Page.cc
++++ b/poppler/Page.cc
+@@ -288,6 +288,22 @@ Page::Page(PDFDoc *docA, int numA, Object &&pageDict, Ref pageRefA, PageAttrs *a
+         goto err2;
+     }
+
++    if (annotsObj.isArray() && annotsObj.arrayGetLength() > 10000) {
++        error(errSyntaxError, -1, "Page annotations object (page {0:d}) is likely malformed. Too big: ({1:d})", num, annotsObj.arrayGetLength());
++        goto err2;
++    }
++    if (annotsObj.isRef()) {
++        auto resolvedObj = getAnnotsObject();
++        if (resolvedObj.isArray() && resolvedObj.arrayGetLength() > 10000) {
++            error(errSyntaxError, -1, "Page annotations object (page {0:d}) is likely malformed. Too big: ({1:d})", num, resolvedObj.arrayGetLength());
++            goto err2;
++        }
++        if (!resolvedObj.isArray() && !resolvedObj.isNull()) {
++            error(errSyntaxError, -1, "Page annotations object (page {0:d}) is wrong type ({1:s})", num, resolvedObj.getTypeName());
++            goto err2;
++        }
++    }
++
+     // contents
+     contents = pageObj.dictLookupNF("Contents").copy();
+     if (!(contents.isRef() || contents.isArray() || contents.isNull())) {
+--
+2.40.0
diff --git a/meta-oe/recipes-support/poppler/poppler_23.04.0.bb b/meta-oe/recipes-support/poppler/poppler_23.04.0.bb
index a8ab19064d..81574177e0 100644
--- a/meta-oe/recipes-support/poppler/poppler_23.04.0.bb
+++ b/meta-oe/recipes-support/poppler/poppler_23.04.0.bb
@@ -16,6 +16,8 @@  SRC_URI = "http://poppler.freedesktop.org/${BP}.tar.xz \
            file://CVE-2025-32365.patch \
            file://CVE-2025-43903-0001.patch \
            file://CVE-2025-43903-0002.patch \
+           file://CVE-2025-52886-0001.patch \
+           file://CVE-2025-52886-0002.patch \
            "
 SRC_URI[sha256sum] = "b6d893dc7dcd4138b9e9df59a13c59695e50e80dc5c2cacee0674670693951a1"