Skip to content

Commit

Permalink
Prevent losing track of BSTRByReference memory
Browse files Browse the repository at this point in the history
  • Loading branch information
dbwiddis committed Jan 19, 2021
1 parent f3fd2ca commit dfe4bdc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Bug Fixes
* [#1279](https://github.com/java-native-access/jna/issues/1279): Remove `DLLCallback` import from `CallbackReference` - [@dyorgio](https://github.com/dyorgio).
* [#1278](https://github.com/java-native-access/jna/pull/1278): Improve compatibility of `c.s.j.p.WindowUtils#getProcessFilePath` and fix unittests for windows 32bit intel - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#1284](https://github.com/java-native-access/jna/pull/1284): Fix illegal access exceptions, when retrieving options for private library interfaces with an instance field - [@fkistner](https://github.com/fkistner).
* [#1300](https://github.com/java-native-access/jna/pull/1300): Prevent losing track of `BSTRByReference` memory - [@dbwiddis](https://github.com/dbwiddis).


Release 5.6.0
=============
Expand Down
24 changes: 21 additions & 3 deletions contrib/platform/src/com/sun/jna/platform/win32/WTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
*/
package com.sun.jna.platform.win32;

import java.io.UnsupportedEncodingException;

import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.PointerType;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.WinDef.USHORT;
import com.sun.jna.ptr.ByReference;
import java.io.UnsupportedEncodingException;

/**
* Constant defined in WTypes.h
Expand Down Expand Up @@ -150,6 +151,9 @@ public String toString() {
}

public class BSTRByReference extends ByReference {

private BSTR bstr = null;

public BSTRByReference() {
super(Native.POINTER_SIZE);
}
Expand All @@ -160,15 +164,29 @@ public BSTRByReference(BSTR value) {
}

public void setValue(BSTR value) {
// Save a local reference to prevent losing value's memory
this.bstr = value;
this.getPointer().setPointer(0, value.getPointer());
}

public BSTR getValue() {
return new BSTR(getPointer().getPointer(0));
// If memory address hasn't changed, return the stored BSTR
if (bstr != null) {
if (getPointer().equals(bstr.getPointer())) {
return bstr;
}
// The stored memory has been changed, memory is tracked elsewhere.
// Clean up unneeded reference.
bstr = null;
}
// Null check the pointer
Pointer p = getPointer().getPointer(0);
return p != null ? new BSTR(p) : null;
}

public String getString() {
return this.getValue().getValue();
BSTR b = this.getValue();
return b != null ? b.getValue() : null;
}
}

Expand Down

0 comments on commit dfe4bdc

Please sign in to comment.