pdf_create_annot: return an owned reference not a borrowed one.
authorRobin Watts <[email protected]>
Mon, 26 Jul 2021 17:35:51 +0000 (18:35 +0100)
committerRobin Watts <[email protected]>
Tue, 27 Jul 2021 11:31:10 +0000 (12:31 +0100)
This seems much more logical - indeed, the Java, C++ and Python
bindings have all been written to assume this.

Knock on effects are for pdf_annot_create_raw and
pdf_create_signature_widget to also return owned references.

mupdf-gl has been updated to reflect the fact that it owns the
reference.

fz_create_link and pdf_create_link are similarly updated. The
java (and presumably C++ and python) handlers for this already
assumed that they returned an owned reference.

docs/api/changes.html
docs/coding-style.html
include/mupdf/pdf/annot.h
include/mupdf/pdf/form.h
platform/gl/gl-annotate.c
platform/gl/gl-form.c
platform/gl/gl-main.c
source/pdf/pdf-annot.c
source/tools/murun.c

index eaa4da995f3f37284118b9f85199f41ebe3ce050..31948d1b9ba0e5e55b863b5df28c31cb94777b14 100644 (file)
@@ -39,6 +39,17 @@ changes that may be require to update between versions.
 Note, that we only list changes in existing APIs here, not all additions
 to the API.
 
+<h2>Changes from 1.18 to 1.19</h2>
+
+<p>We were inconsistent with the behaviour of fz_create_ and pdf_create_
+functions, in that sometimes they returned a borrowed reference, rather
+than a reference that the caller had to drop. We now always return a
+reference that the caller owns and must drop.</p>
+
+<h2>Changes from 1.17 to 1.18</h2>
+
+<p>None worth mentioning.</p>
+
 <h2>Changes from 1.16 to 1.17</h2>
 
 <p>
index e3dc419cd7caa29874e99ebdada6250ead4a55d4..ac3bb279234a261c7cbfad9e7f88e55294b45ede 100644 (file)
@@ -43,7 +43,7 @@ Avoid using 'get' as this is a meaningless and redundant filler word.
 These words are reserved for reference counting schemes:
 
 <ul>
-<li> new, find, load, open, keep -- return objects that you are responsible for freeing.
+<li> new, create, find, load, open, keep -- return objects that you are responsible for freeing.
 
 <li> drop -- relinquish ownership of the object passed in.
 </ul>
index 6a82ed3a4cdfa498ffd5e4f23dc134f07c447869..ae33a94223e5d185c659ebf2aa8868e97302c7e2 100644 (file)
@@ -253,8 +253,10 @@ fz_link *pdf_create_link(fz_context *ctx, pdf_page *page, fz_rect bbox, const ch
        create a new annotation of the specified type on the
        specified page. Populate it with sensible defaults per the type.
 
-       The returned pdf_annot structure is owned by the page and does
-       not need to be freed.
+       Currently this returns a reference that the caller owns, and
+       must drop when finished with it. Up until release 1.18, the
+       returned reference was owned by the page and did not need to
+       be freed.
 */
 pdf_annot *pdf_create_annot(fz_context *ctx, pdf_page *page, enum pdf_annot_type type);
 
index afbf528fb8c216b4806eb78278e4e575b4789955..cfdb05460e28cebe12dd768d0a9537fbbf6b42d8 100644 (file)
@@ -32,8 +32,12 @@ int pdf_update_widget(fz_context *ctx, pdf_widget *widget);
 
 /*
        create a new signature widget on the specified page, with the
-       specified name. The returned pdf_widget structure is owned by
-       the page and does not need to be freed
+       specified name.
+
+       The returns pdf_widget reference must be dropped by the caller.
+       This is a change from releases up to an including 1.18, where
+       the returned reference was owned by the page and did not need
+       to be freed by the caller.
 */
 pdf_widget *pdf_create_signature_widget(fz_context *ctx, pdf_page *page, char *name);
 
index ff8cad3c959a295c1762356aa94901188bd688d1..b1d26735448d31eaa153156150c5b171cea821b3 100644 (file)
@@ -529,6 +529,7 @@ static void new_annot(int type)
        fz_snprintf(msg, sizeof msg, "Create %s Annotation", pdf_string_from_annot_type(ctx, type));
        pdf_begin_operation(ctx, pdf, msg);
 
+       pdf_drop_annot(ctx, selected_annot);
        selected_annot = pdf_create_annot(ctx, page, type);
 
        pdf_set_annot_modification_date(ctx, selected_annot, time(NULL));
@@ -940,7 +941,8 @@ void do_annotate_panel(void)
                if (ui_list_item(&annot_list, pdf_annot_obj(ctx, annot), buf, selected_annot == annot))
                {
                        trace_action("annot = page.getAnnotations()[%d];\n", idx);
-                       selected_annot = annot;
+                       pdf_drop_annot(ctx, selected_annot);
+                       selected_annot = pdf_keep_annot(ctx, annot);
                }
        }
        ui_list_end(&annot_list);
@@ -1221,6 +1223,7 @@ void do_annotate_panel(void)
                        trace_action("page.deleteAnnotation(annot);\n");
                        pdf_delete_annot(ctx, page, selected_annot);
                        page_annots_changed = 1;
+                       pdf_drop_annot(ctx, selected_annot);
                        selected_annot = NULL;
                        return;
                }
@@ -1240,19 +1243,26 @@ static void new_redaction(pdf_page *page, fz_quad q)
 
        annot = pdf_create_annot(ctx, page, PDF_ANNOT_REDACT);
 
-       pdf_set_annot_modification_date(ctx, annot, time(NULL));
-       if (pdf_annot_has_author(ctx, annot))
-               pdf_set_annot_author(ctx, annot, getuser());
-       pdf_add_annot_quad_point(ctx, annot, q);
-       pdf_set_annot_contents(ctx, annot, search_needle);
-
-       trace_action("annot = page.createAnnotation(%q);\n", "Redact");
-       trace_action("annot.addQuadPoint([%g, %g, %g, %g, %g, %g, %g, %g]);\n",
-               q.ul.x, q.ul.y,
-               q.ur.x, q.ur.y,
-               q.ll.x, q.ll.y,
-               q.lr.x, q.lr.y);
-       trace_action("annot.setContents(%q);\n", search_needle);
+       fz_try(ctx)
+       {
+               pdf_set_annot_modification_date(ctx, annot, time(NULL));
+               if (pdf_annot_has_author(ctx, annot))
+                       pdf_set_annot_author(ctx, annot, getuser());
+               pdf_add_annot_quad_point(ctx, annot, q);
+               pdf_set_annot_contents(ctx, annot, search_needle);
+
+               trace_action("annot = page.createAnnotation(%q);\n", "Redact");
+               trace_action("annot.addQuadPoint([%g, %g, %g, %g, %g, %g, %g, %g]);\n",
+                       q.ul.x, q.ul.y,
+                       q.ur.x, q.ur.y,
+                       q.ll.x, q.ll.y,
+                       q.lr.x, q.lr.y);
+               trace_action("annot.setContents(%q);\n", search_needle);
+       }
+       fz_always(ctx)
+               pdf_drop_annot(ctx, annot);
+       fz_catch(ctx)
+               fz_rethrow(ctx);
 
        pdf_has_redactions_doc = pdf;
        pdf_has_redactions = 1;
@@ -1333,12 +1343,14 @@ void do_redact_panel(void)
                for (i = 0; i < search_hit_count; i++)
                        new_redaction(page, search_hit_quads[i]);
                search_hit_count = 0;
+               pdf_drop_annot(ctx, selected_annot);
                selected_annot = NULL;
        }
        if (ui_button_aux("Mark search in document", search_needle == NULL))
        {
                mark_all_search_results();
                search_hit_count = 0;
+               pdf_drop_annot(ctx, selected_annot);
                selected_annot = NULL;
        }
 
@@ -1354,6 +1366,7 @@ void do_redact_panel(void)
 
        if (ui_button_aux("Redact Page", num_redact == 0))
        {
+               pdf_drop_annot(ctx, selected_annot);
                selected_annot = NULL;
                trace_action("page.applyRedactions(%s, %d);\n",
                        redact_opts.black_boxes ? "true" : "false",
@@ -1365,6 +1378,7 @@ void do_redact_panel(void)
 
        if (ui_button_aux("Redact Document", !pdf_has_redactions))
        {
+               pdf_drop_annot(ctx, selected_annot);
                selected_annot = NULL;
                redact_all_pages(&redact_opts);
        }
@@ -1384,7 +1398,8 @@ void do_redact_panel(void)
                        if (ui_list_item(&annot_list, pdf_annot_obj(ctx, annot), buf, selected_annot == annot))
                        {
                                trace_action("annot = page.getAnnotations()[%d];\n", idx);
-                               selected_annot = annot;
+                               pdf_drop_annot(ctx, selected_annot);
+                               selected_annot = pdf_keep_annot(ctx, annot);
                        }
                }
        }
@@ -1426,6 +1441,7 @@ void do_redact_panel(void)
                        trace_action("page.deleteAnnotation(annot);\n");
                        pdf_delete_annot(ctx, page, selected_annot);
                        page_annots_changed = 1;
+                       pdf_drop_annot(ctx, selected_annot);
                        selected_annot = NULL;
                        return;
                }
@@ -1884,7 +1900,8 @@ void do_annotate_canvas(fz_irect canvas_area)
                                        if (!selected_annot && !showannotate)
                                                toggle_annotate(ANNOTATE_MODE_NORMAL);
                                        ui.active = annot;
-                                       selected_annot = annot;
+                                       pdf_drop_annot(ctx, selected_annot);
+                                       selected_annot = pdf_keep_annot(ctx, annot);
                                }
                        }
                }
@@ -1973,7 +1990,10 @@ void do_annotate_canvas(fz_irect canvas_area)
        if (ui_mouse_inside(canvas_area) && ui.down)
        {
                if (!ui.active && ui.hot == nothing)
+               {
+                       pdf_drop_annot(ctx, selected_annot);
                        selected_annot = NULL;
+               }
        }
 
        if (ui.right)
index a6590eadab0321d031cbb181381b7f83dede9444..352dc4fe2bfd4aa576f07e59b9ce9ceaca032a9c 100644 (file)
@@ -616,7 +616,8 @@ void do_widget_canvas(fz_irect canvas_area)
                                                pdf_annot_event_blur(ctx, selected_annot);
                                        }
                                        trace_action("widget = page.getWidgets()[%d];\n", idx);
-                                       selected_annot = widget;
+                                       pdf_drop_annot(ctx, selected_annot);
+                                       selected_annot = pdf_keep_annot(ctx, widget);
                                        trace_action("widget.eventFocus();\n");
                                        pdf_annot_event_focus(ctx, widget);
                                }
index 843c6497a15af3d55ecd7ba9e37ba48b921c7ff7..17239690d34282161d98b94a18e651b6756c885b 100644 (file)
@@ -798,6 +798,7 @@ static void clear_selected_annot(void)
        /* clear all editor selections */
        if (selected_annot && pdf_annot_type(ctx, selected_annot) == PDF_ANNOT_WIDGET)
                pdf_annot_event_blur(ctx, selected_annot);
+       pdf_drop_annot(ctx, selected_annot);
        selected_annot = NULL;
 }
 
@@ -1939,7 +1940,7 @@ static void do_app(void)
        {
                switch (ui.key)
                {
-               case KEY_ESCAPE: clear_search(); selected_annot = NULL; break;
+               case KEY_ESCAPE: clear_search(); pdf_drop_annot(ctx, selected_annot); selected_annot = NULL; break;
                case KEY_F1: ui.dialog = help_dialog; break;
                case 'a': toggle_annotate(ANNOTATE_MODE_NORMAL); break;
                case 'R': toggle_annotate(ANNOTATE_MODE_REDACT); break;
index 763be4a9ad1f7798e43d7c466914defc2e6829f5..adcfe80ed763b80a5619304f683ec0bd2e5784b5 100644 (file)
@@ -474,7 +474,7 @@ pdf_create_annot_raw(fz_context *ctx, pdf_page *page, enum pdf_annot_type type)
                fz_rethrow(ctx);
        }
 
-       return annot;
+       return pdf_keep_annot(ctx, annot);
 }
 
 fz_link *
@@ -558,7 +558,7 @@ pdf_create_link(fz_context *ctx, pdf_page *page, fz_rect bbox, const char *uri)
                fz_rethrow(ctx);
        }
 
-       return link;
+       return fz_keep_link(ctx, link);
 }
 
 static pdf_obj *
@@ -723,7 +723,10 @@ pdf_create_annot(fz_context *ctx, pdf_page *page, enum pdf_annot_type type)
        fz_always(ctx)
                pdf_end_operation(ctx, page->doc);
        fz_catch(ctx)
+       {
+               pdf_drop_annot(ctx, annot);
                fz_rethrow(ctx);
+       }
 
        return annot;
 }
index 32ec70f4f33e0d4f9fdbcc28c49c96cc61aee6b8..c854671b6299687a9c53d2b0dfefc41d2b37e9fd 100644 (file)
@@ -5185,7 +5185,7 @@ static void ffi_PDFPage_createAnnotation(js_State *J)
        fz_catch(ctx)
                rethrow(J);
        js_getregistry(J, "pdf_annot");
-       js_newuserdata(J, "pdf_annot", pdf_keep_annot(ctx, annot), ffi_gc_pdf_annot);
+       js_newuserdata(J, "pdf_annot", annot, ffi_gc_pdf_annot);
 }
 
 static void ffi_PDFPage_deleteAnnotation(js_State *J)