Skip to content
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

free objects in the right context #139

Merged
merged 8 commits into from
Oct 22, 2022
Merged

free objects in the right context #139

merged 8 commits into from
Oct 22, 2022

Conversation

jw3126
Copy link
Member

@jw3126 jw3126 commented Oct 17, 2022

No description provided.

@jw3126
Copy link
Member Author

jw3126 commented Oct 17, 2022

This fixes some of the problems in #91

ctx = contexts[Threads.threadid()]
g1 = LibGEOS.Polygon(p, ctx)
g2 = LibGEOS.Polygon(p, ctx)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In #91 it was actually tried to intersect objects that live in one context in another context. I am not sure if is that actually supported by the C library?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LibGEOS.intersects(geo1 #=global context=#, geo2 #=global context=#, ctx #= local context =#)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nraynaud is that supported by the C-library? Was this on purpose or an oversight to use differenc contexts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we were trying to trigger a bug on purpose, it was before confirming that even "read only" operations are unsafe.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify doing this operation in C code is it known to you whether that is safe?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we now know it's unsafe in C.

Copy link
Member Author

@jw3126 jw3126 Oct 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I think it might be a good idea to carry context around with objects (as already implemented in this PR) and also add an assertion in every function that contexts of all involved objects are equal. What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I will add assertions in a follow up PR to keep this one as small as possible.

@nraynaud
Copy link
Contributor

nraynaud commented Oct 17, 2022 via email

@jw3126 jw3126 mentioned this pull request Oct 18, 2022
@jw3126
Copy link
Member Author

jw3126 commented Oct 18, 2022

Ok I think this PR is ready. @visr could you review?

@yeesian yeesian requested a review from visr October 19, 2022 05:09
@jw3126
Copy link
Member Author

jw3126 commented Oct 21, 2022

fixes #140

Copy link
Member

@visr visr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, thanks for the contribution!

@visr visr merged commit 23f0222 into JuliaGeo:master Oct 22, 2022
@maxfreu
Copy link
Contributor

maxfreu commented Nov 10, 2022

Hi @jw3126! Could you explain why there was a memory leak in the STRTree? I wrote it initally and thought it was ok. Why does the STRTree struct have to be mutable and what is GC.@preserve for?

@jw3126
Copy link
Member Author

jw3126 commented Nov 10, 2022

Hi @jw3126! Could you explain why there was a memory leak in the STRTree?

Sure. Generelly every create must be matched by a destroy, or else you leak memory. In your version there was a GEOSSTRtree_create_r but no GEOSSTRtree_destroy_r.

I wrote it initally and thought it was ok. Why does the STRTree struct have to be mutable

Same reason, why all other wrappers in this package are mutable. Because we need to attach finalizers and this is not possible for bitstypes.

and what is GC.@preserve for?

You are talking about this right?

    GC.@preserve context tree geometry matches begin
       GEOSSTRtree_query_r (
            context.ptr,
            tree.ptr,
            geometry.ptr,
            cfun_callback,
            pointer_from_objref(matches),
        )
    end

The thing is we only pass pointers to GEOSSTRtree_query_r. That means if no other references to these objects exist, the GC has permission to delete them before or while GEOSSTRtree_query_r is called. I don't think it will do in practice though and in this package, there are lots of similar places, where the GC has permission to delete stuff that is used via pointers.

@maxfreu
Copy link
Contributor

maxfreu commented Nov 11, 2022

Thank you, that sounds logical! This sounds like LibGEOS objects behave like interactive ArchGDAL objects, correct? (See https://github.com/yeesian/ArchGDAL.jl/blob/master/docs/src/memory.md)

@jw3126
Copy link
Member Author

jw3126 commented Nov 11, 2022

Thank you, that sounds logical! This sounds like LibGEOS objects behave like interactive ArchGDAL objects, correct? (See https://github.com/yeesian/ArchGDAL.jl/blob/master/docs/src/memory.md)

I am not familiar with ArchGDAL, but yes it seems so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants