Memory leak creating annotations


If I create an annotation, but I don’t end up adding it to the document, I see the memory is not released?

For example

Circle circle = Circle::Create(*mp_doc, Rect(x, y, x + 50, y + 50));
circle.SetColor(ColorPt(1, 0, 0), 3);


The memory allocated is not released.


First, you probably disabled disk caching.


This means temporary objects are stored entirely in memory. By default, they are stored on disk so as to avoid memory usage.

Regardless, the call to Circle::Create is creating a temporary SDF::Obj object.

For example, this is perfectly valid code.

UInt32 obj_num = 0; { PDF::Annots::Circle circle = PDF::Annots::Circle::Create(doc, Rect(10, 10, 20, 20)); obj_num = circle.GetSDFObj().GetObjNum(); } { SDF::Obj circle_obj = doc.GetSDFDoc().GetObj(obj_num); PDF::Annot annot(circle_obj); assert(annot.GetType() == PDF::Annot::e_Circle); }

So even though the circle variable went out of scope, it did not own the object, the PDFDoc object owns it, and it is still accessible, though unused currently.

Calling PDFDoc.Save with the e_remove_unused flag is one way to get rid of the unused circle object.