Code Monkey home page Code Monkey logo

Comments (5)

mara004 avatar mara004 commented on September 22, 2024

I'm afraid we can't do this because the different close functions must be called in correct order, which can't be guaranteed with destructor methods. Using __del__() is discouraged in general.1 Other libraries like pikepdf or pillow also provide close() instead.

There might be a weakref trick which Mike Kroutikov of pdfbrain is currently investigating, but I'm not convinced if this is a good idea for pypdfium2.

Footnotes

  1. https://stackoverflow.com/questions/1481488/what-is-the-del-method-and-how-do-i-call-it#1481512

from pypdfium2.

mara004 avatar mara004 commented on September 22, 2024

See also innodatalabs/pdfbrain#4

__del__ object method is not guaranteed to be called in the refcount order when program exits. This caused random crashes at the exit of programms using pdfbrain.

from pypdfium2.

mara004 avatar mara004 commented on September 22, 2024

What @mkroutikov did with weakref doesn't work, I think (see innodatalabs/pdfbrain#6).

However, I believe this should be possible with __del__ anyway. We just need to keep references to parent objects (and their respective parents) and ensure none of them has been closed yet before closing the child object.

However, closing of child objects might then be skipped if parents and children are garbage collected in the same cycle.
I need to verify if closing the parent also frees the memory of the kids - if so, this is not a problem. Otherwise, we might be able to keep track of released child objects via a list of weak references and close them first, even if Python attempts to finalise parent before child...

from pypdfium2.

mara004 avatar mara004 commented on September 22, 2024

Actually, I had to revert this again because - despite the order checking - this does still trigger random segfaults on exit, see f59dc65

from pypdfium2.

mara004 avatar mara004 commented on September 22, 2024

I think I finally found a working way with weakref, see innodatalabs/pdfbrain#6 (comment).
I've started a branch: autoclose_via_weakref.

from pypdfium2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.