Skip to content

Commit b83e977

Browse files
committed
ensure memory always initialized when calling useMemory, even if layout is unknown
1 parent ab2d5a7 commit b83e977

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/com/sun/jna/Structure.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -286,15 +286,15 @@ protected void useMemory(Pointer m) {
286286
}
287287

288288
/** Set the memory used by this structure. This method is used to
289-
* indicate the given structure is nested within another or otherwise
290-
* overlaid on some other memory block and thus does not own its own
291-
* memory.
289+
* indicate the given structure is based on natively-allocated data,
290+
* nested within another, or otherwise overlaid on existing memory and
291+
* thus does not own its own memory allocation.
292292
*/
293293
protected void useMemory(Pointer m, int offset) {
294-
// Invoking calculateSize() here is important when this method is
295-
// invoked from the ctor, to ensure fields are properly scanned and
296-
// allocated
297294
try {
295+
// Ensure our memory pointer is initialized, even if we can't
296+
// yet figure out a proper size/layout
297+
this.memory = m.share(offset);
298298
if (size == CALCULATE_SIZE) {
299299
size = calculateSize(false);
300300
}
@@ -923,7 +923,8 @@ private LayoutInfo deriveLayout(boolean force, boolean avoidFFIType) {
923923
throw new IllegalArgumentException("This VM does not support read-only fields (field '"
924924
+ field.getName() + "' within " + getClass() + ")");
925925
}
926-
// In J2SE VMs, this allows overriding the value of final fields
926+
// In J2SE VMs, this allows overriding the value of final
927+
// fields
927928
field.setAccessible(true);
928929
}
929930
structField.field = field;

0 commit comments

Comments
 (0)