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

documentation ambiguous about callbacks returning String #671

Open
strtok opened this issue Jun 16, 2016 · 3 comments
Open

documentation ambiguous about callbacks returning String #671

strtok opened this issue Jun 16, 2016 · 3 comments

Comments

@strtok
Copy link

strtok commented Jun 16, 2016

If calling a java callback that returns String, the return type is translated to char* on the native side. The documentation is unclear, though, about the lifetime of the string. Is the memory owned by java or should it be freed by C?

@strtok
Copy link
Author

strtok commented Jun 17, 2016

It looks like the char* is freed by JNA and is tied to the lifetime of the source String?

@twall
Copy link
Contributor

twall commented Jun 17, 2016

JNA does what it can to make things easier for you, but fundamentally you
need to be aware of when you're passing memory buffers off to native
code. Once you do so, it's your responsibility to ensure that said
buffer will stick around as long as the native code needs it. JNA can't
predict that for you, but it at least gives you the option of keeping a
reference to the String as proxy for maintaining a reference to the
automatically generated native buffer. JNA in general will try to reclaim
any memory that it allocations, usually by GC when there are no longer any
extant referenes to the allocated memory.

A native API that receives a buffer without explicit information w/r/t its
lifecycle is a code smell. Consider passing a buffer and asking for it to
be populated instead.

Other, less desirable solutions are to document what's expected to be done
with the buffer, or using a global/static buffer.

On Thu, Jun 16, 2016 at 8:42 PM, Erik Bremen [email protected]
wrote:

It looks like the char* is freed by JNA and is tied to the lifetime of the
source String?


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#671 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AAuMrQSOxo93bUUfnJWaqYPGHl8zg8Egks5qMe14gaJpZM4I366i
.

@strtok
Copy link
Author

strtok commented Jun 17, 2016

Thanks. That clarifies what we found in the source. The intention for opening the bug was that the docs could use a note that in every case where JNA might return a pointer to native code (String -> char*, Memory, etc) that JNA maintains responsibility for freeing it and that the memory is directly associated with the lifetime of the object in java being mapped.

mstyura pushed a commit to mstyura/jna that referenced this issue Sep 9, 2024
…ess#671)

Motivation:

We introduced a bug when updating the actions.

Modifications:

Fix version of settings action

Result:

Deploy workflow works again
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

2 participants