-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
XPath issues when doc goes out of scope #61
Comments
@dginev
|
@jangernert I need to find some time to look into this more closely. My first instinct is that I would like to keep the The reason I started using Weak is to avoid getting into a very complex reference-counting model for the wrappers over the C data structures. The idea being that if you are using xpath over a document, it is the author's responsibility to execute it while the document is in scope, or suffer a runtime panic otherwise. Holding a strong reference leaves a lot of room open for subtle memory leaks - especially in programs which process N documents with M xpaths, and - at least to my abilities - it was a lot harder to reason about that model, and to fish out the memory leaks. Finding that a document is not in scope with a runtime panic on the other hand is very direct to both understand and fix. I need to rush now, but I'd like to come back and understand better the exact problem you are seeing - do you have a use case where you want the xpath to outlive its document? Or would you be happy with a very informative error message when the said panic takes place? |
Here is the PR that introduced the |
A runtime error would be sufficient I guess. If with some rust magic that error could be moved to compile time I would be even happier. With the behaviour as it is, it is quite easy to not notice the problem for a long time. Apparently I already encountered it before as instead of functions for some reason I wrote edit:
Just to be clear: the code example above does not panic for me. That's why I ran into this bug twice already. Thinking about it: maybe requiring a reference to a document when calling |
I think I just ran into this one: xpath seemed to work, but the symptom I had is that |
On my way debugging and separating the issues listed in #60 I came across another thing that isn't related to async or threading.
An xpath context is non-functional after the document from which it was created goes out of scope and is dropped.
There is no warning or error. It just doesn't return any results any more.
Here is a small example. The
parse_html
function causes the xpath evaluation to return an empty vector, but no unwrap fails. Doing the same thing in the unit test itself (the commented out code) works as expected:I see that the XPath context does contain a weak reference to the document to prevent exactly this issue. But from my understanding the context needs a strong reference to extend the lifetime of the document.
Weak documentation:
Not sure if I understand everything correctly, but theDocument
implementing drop directly and freeing the doc pointer seems wrong to me. Shouldn't theDocument
just drop its reference to the_Document
and only when all references to_Document
are dropped_Document
itself callsxmlFreeDoc
-> oops, misread the code. It works exactly as I just explained. Guess it's a bit confusing that
Document
is declared in L82 and right after that the drop trait is implemented for_Document
which is declared beforeDocument
.The text was updated successfully, but these errors were encountered: