hzzc1987 / jnaerator

Automatically exported from code.google.com/p/jnaerator
0 stars 0 forks source link

Structure autoWrite breaks by-reference structures #56

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Code:

mystruct s = new mystruct();
s.something = value;
library.function(s.byReference());

The struct that arrives in C land doesn't contain value.

What's happening, as far as I can tell:

1. byReference calls setupClone
2. setupClone calls JNA's Structure#write to write original struct to its 
memory
3. setupClone sets up the new by-reference struct to use the same memory
4. the C function is invoked on the Java side
5. JNA calls into autoWrite of the by-reference clone which then writes its 
zero'd Java values to the memory again
6. the real C function is invoked and receives the zero'd values

I believe jnaerator should do

clone.setAutoWrite(false);
clone.setAutoRead(true);

for a by-reference clone.

Not sure what it should do with by-value and a "normal" clone.

Also, it would be nice to be able to do

s.byReference(false)

which is then passed to setupClone as an auto-read flag to turn off auto-
reading in the case that you know that the C code won't modify the 
reference that is passed to it. 

I'm using JNA 3.2.4 and com.ochafik.lang.jnaerator.runtime.Structure from 
SVN. I know some code related to Structure has changed in JNA SVN, but that 
just has to do with avoiding an unnecessary allocation when the structure 
is later changed to share existing memory.

Hope that makes sense. Let me know if it doesn't.

Original issue reported on code.google.com by fullung@gmail.com on 25 Mar 2010 at 10:38

GoogleCodeExporter commented 8 years ago
It seems JNA treats Structureas by-reference by default so as long as one 
doesn't call 
byReference(), you avoid the auto-writing overwriting problem.

Original comment by fullung@gmail.com on 25 Mar 2010 at 10:56

GoogleCodeExporter commented 8 years ago
Hi fullung,
Seems to make a lot of sense, thanks a lot for your report :-)
Cheers

Original comment by olivier.chafik@gmail.com on 25 Mar 2010 at 4:22

GoogleCodeExporter commented 8 years ago
Any news on this issue please? Could simplify a lot of spider code if this 
worked :-) Just checked SVN and it still seems to be broken.

Original comment by night19...@googlemail.com on 28 Sep 2010 at 8:22

GoogleCodeExporter commented 8 years ago
Hello,

I've committed a change that should fix this issue 
(http://code.google.com/p/nativelibs4java/source/detail?r=1248).

Please let me know if it does not work with your code...
Cheers

Original comment by olivier.chafik@gmail.com on 28 Sep 2010 at 10:26

GoogleCodeExporter commented 8 years ago
Should be fixed in release 0.9.5. Please reopen if it's not the case...

Cheers

Original comment by olivier.chafik@gmail.com on 11 Oct 2010 at 1:51