Comments (8)
I have encountered the same issue as @jeromerobert when trying to connect to a C library. For example, consider the following C code:
struct IntPair {
int a;
int b;
};
// some function which creates a pair
struct IntPair makePair(int x, int y) {
struct IntPair p;
p.a = x;
p.b = y;
return p;
}
Here makePair returns IntPair struct itself, and not just a pointer to it.
Is there a way to bind this function in jnr-ffi?
In all the test examples in the jnr-ffi project, C functions return struct pointers, and not structs.
from jnr-ffi.
Care to put that in terms of an example test case?
Note that there's been improvements in how Struct is laid out and passed around, so check 2.1 out.
from jnr-ffi.
I modified the getpid example as if pid_t
was:
struct pid_t { int value; }
from jnr-ffi.
Is it possible to return a struct by value in version 2.19?
I had asked question on Stack Overflow before I found this GitHub issue:
https://stackoverflow.com/questions/56129984/how-to-return-by-value-from-native-function
How can I prevent memory leak shown in SO question?
from jnr-ffi.
This appears to still be an issue - is using a function that returns a struct by value supported?
from jnr-ffi.
@charleskorn yeah this is still an issue, see #262
Unfortunately it's also quite low priority I'd say, unless you have access to the original library code and don't mind recompiling and using a struct by reference (or pointer) function then you'll have better luck with JNA, which I believe does support structs passed and returned by value, but I might be wrong about that so do check just in case.
from jnr-ffi.
Makes sense, thanks for the quick response @basshelal. If it helps raise the priority, this makes working with functions exported from Golang's cgo quite difficult - in cgo, any function that returns multiple values (a very common pattern in Golang) gets translated to a function that returns a struct by value.
from jnr-ffi.
Thanks @charleskorn
Yeah it's annoying that struct by value is more common than I'd like and we never had support for it.
Personally I'd want it to be higher priority since I've had to deal with my own problems with struct by value as a parameter but the fix would be quite complicated to implement and verify, hence the lower priority.
But you are welcome to look into the code yourself and see if you can help with anything, you can also join our matrix discussion:
https://matrix.to/#/#jruby:matrix.org
from jnr-ffi.
Related Issues (20)
- UnsatisfiedLinkError when loading 3 dependent libraries HOT 11
- Unsatisfied link when used in conjunction with jnr-posix HOT 5
- Investigate failing Pointer Boolean tests on Darwin (macOS)
- LibraryLoader.search() path not taken into consideration unless also in LD_LIBRARY_PATH HOT 3
- What's the mapping Java type of C type FILE *?
- [feature request] HOT 1
- Erorr : jnr.ffi.provider.jffi.Provider
- Native Object filling "not working"
- Native C calling Java method HOT 1
- Cannot pass a struct to 'c' function by value
- Multi-dimensional arrays as struct members HOT 1
- Load the static link library HOT 1
- Release new version of jnr-ffi HOT 1
- Why use JNR over JNA HOT 1
- support convert pointer to struct
- String parameter of a callback function gets messed when passed from DLL (Rust) to Java HOT 2
- Android and bytecode
- document build procedures
- JVM crash invoking some SDL Library function
- Bad dates in jnr/jnr-ffi commits HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jnr-ffi.