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

Alter the generator to free strings returned by GEOS. #122

Merged
merged 7 commits into from
Sep 4, 2022

Conversation

nraynaud
Copy link
Contributor

I have a memory leak in my use of GEOS, and I studied freeing strings as an hypothesis, so here is the code.

Sadly, I couldn't find my leak.

@nraynaud nraynaud marked this pull request as ready for review July 19, 2022 10:31
@visr
Copy link
Member

visr commented Jul 24, 2022

Too bad this didn't solve your issue. What are your thoughts about merging this? Do you know that this is always safe?

Right now an exception is made for GEOSVersion since it is the only function from http://libgeos.org/doxygen/geos__c_8h.html that returns const char *. Do you know if it is easy to detect that return type instead of using the name, in case more such functions get added in the future?

Here are some related links for GDAL.jl:
JuliaGeo/GDAL.jl#77
https://github.com/JuliaGeo/GDAL.jl/blob/8ffa73af0da91e3aa54163052ddb1369ebaf3682/gen/generator.jl#L61-L65
https://github.com/JuliaGeo/GDAL.jl/blob/d44dc737ec16a60d1edb81bb71e0cb5f0666f165/src/error.jl#L65-L75

@nraynaud
Copy link
Contributor Author

Hi Martijn, I am as circumspect as you are about this PR TBH.
I think it's a step in the right direction, but I don't know if it's enough.
For the const char*, I searched the GEOS.h documentation. I couldn't find how to interrogate the return types in the clang library.
The part I neglected is the strings returned in a pointer parameter (GEOSisValidDetail() Is the only instance I think)
I asked some friends about injecting custom free/malloc in GEOS for testing, nobody came up with a way that wouldn't also inject it in the julia environment.

I you are interested in this PR, I can try to improve it until it's good enough to be merged.

Maybe you or some onlookers have suggestions to improve it.

Nico.

@nraynaud
Copy link
Contributor Author

Hi @visr I think I'm ready for a round of review.

@visr visr force-pushed the nr-free-strings branch from 53bb48f to 6fd06db Compare July 31, 2022 19:03
@visr
Copy link
Member

visr commented Jul 31, 2022

This looks quite good, thanks for going through the extra effort to automatically detect when to free strings. That might be useful in other wrappers as well.

I rebased the branch, and if you don't mind renamed to string_copy_free to make it more explicit what it does. I also added the optional argument for the context handle. Could you perhaps, for the _r functions that get the handle as an argument, forward that handle to the string_copy_free argument? Otherwise you could have say a user creating their own context handle and calling GEOSRelate_r(handle, g1, g2), only for it to still use the global context when freeing the resulting string, which (I assume) would not be what a user would excpect. Besides that, I think this is good to go!

@nraynaud
Copy link
Contributor Author

yes, I spent an inordinate amount of time looking into how to use clang, but I think it gives a good hint for other people who want to do things with types, I couldn't find any example anywhere.

I'll takle the context pointer soon.

@visr
Copy link
Member

visr commented Jul 31, 2022

@Gnimuc do you think https://github.com/JuliaGeo/LibGEOS.jl/blob/6fd06dbc7994bb4a221c4bac53f2c5a9a42e31b7/gen/generator.jl is worth linking to in https://github.com/JuliaInterop/Clang.jl#examples? The rewrite function in which we use MacroTools to match and decompose functions for rewriting is quite a convenient pattern, and the newly added return_type_is_const_char could perhaps be useful for others. https://github.com/JuliaGeo/GDAL.jl and https://github.com/JuliaGeo/Proj.jl are similar.

@nraynaud
Copy link
Contributor Author

nraynaud commented Aug 3, 2022

I am ready for a next round of review. I unwrapped the pointer for the context, because from the low level we only have access to the pointer, not the full object.

@visr visr merged commit 8ea1e52 into JuliaGeo:master Sep 4, 2022
@visr
Copy link
Member

visr commented Sep 4, 2022

Sorry for letting this slip @nraynaud. Thank you for your effort!

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.

2 participants