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

Return a struct by value #14

Open
jeromerobert opened this issue Feb 10, 2014 · 8 comments
Open

Return a struct by value #14

jeromerobert opened this issue Feb 10, 2014 · 8 comments

Comments

@jeromerobert
Copy link

Unless I missed something there is no way in jnr-ffi, to bind a function which return a struct by value.

This available at jffi level using Invoker.invoke or Invoker.invokeStruct but it would be nice to have it a jnr-ffi level.

@headius
Copy link
Member

headius commented Sep 26, 2016

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.

@juraj-bicikl
Copy link

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.

jeromerobert added a commit to jeromerobert/jnr-ffi-examples that referenced this issue Dec 24, 2016
@jeromerobert
Copy link
Author

I modified the getpid example as if pid_t was:

struct pid_t { int value; }

@sgnsajgon
Copy link

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?

@charleskorn
Copy link
Contributor

charleskorn commented Aug 27, 2021

This appears to still be an issue - is using a function that returns a struct by value supported?

@basshelal
Copy link
Contributor

@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.

@charleskorn
Copy link
Contributor

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.

@basshelal
Copy link
Contributor

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

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

No branches or pull requests

6 participants