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

Fixed memory allocation in LPWSTR and LPSTR classes #382

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Bug Fixes
* Disable WebStart tests - [@twall](https://github.com/twall).
* Dispose all native resources when JNA's native library is unloaded - Paul Grütter of signotec GmbH and [@twall](https://github.com/twall). This fixes a number of seemingly random, sporadic crashes on windows.
* Weakly hold registered Direct-mapped classes - [@twall](https://github.com/twall).
* Fixed memory allocation in classes `com.sun.jna.platform.win32.WTypes.LPWSTR` and `com.sun.jna.platform.win32.WTypes.LPSTR`.

Release 4.1
===========
Expand Down
4 changes: 2 additions & 2 deletions contrib/platform/src/com/sun/jna/platform/win32/WTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public LPSTR(Pointer pointer) {
}

public LPSTR(String value) {
this();
this(new Memory((value.length() + 1L) * Native.WCHAR_SIZE));
this.setValue(value);
}

Expand Down Expand Up @@ -172,7 +172,7 @@ public LPWSTR(Pointer pointer) {
}

public LPWSTR(String value) {
this();
this(new Memory((value.length() + 1L) * Native.WCHAR_SIZE));
this.setValue(value);
}

Expand Down
60 changes: 60 additions & 0 deletions contrib/platform/test/com/sun/jna/platform/win32/WTypesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* Copyright (c) 2007-2014 Timothy Wall, All Rights Reserved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package com.sun.jna.platform.win32;

import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import junit.framework.TestCase;

public class WTypesTest extends TestCase {

private static final String TEST_STRING = "input";

private static final Pointer TEST_POINTER = new Memory((TEST_STRING.length() + 1L) * Native.WCHAR_SIZE);

static {
TEST_POINTER.setWideString(0, TEST_STRING);
}

public void testLPOLESTRConstruction() {
WTypes.LPOLESTR fromString = new WTypes.LPOLESTR(TEST_STRING);
assertEquals(fromString.getValue(), TEST_STRING);
WTypes.LPOLESTR empty = new WTypes.LPOLESTR();
assertNull(empty.getValue());
WTypes.LPOLESTR fromPointer = new WTypes.LPOLESTR(TEST_POINTER);
assertEquals(fromPointer.getValue(), TEST_STRING);
}

public void testLPSTRConstruction() {
WTypes.LPSTR instance = new WTypes.LPSTR(TEST_STRING);
assertEquals(instance.getValue(), TEST_STRING);
WTypes.LPSTR empty = new WTypes.LPSTR();
assertNull(empty.getValue());
WTypes.LPSTR fromPointer = new WTypes.LPSTR(TEST_POINTER);
assertEquals(fromPointer.getValue(), TEST_STRING);
}

public void testLPWSTRConstruction() {
WTypes.LPWSTR instance = new WTypes.LPWSTR(TEST_STRING);
assertEquals(instance.getValue(), TEST_STRING);
WTypes.LPWSTR empty = new WTypes.LPWSTR();
assertNull(empty.getValue());
WTypes.LPWSTR fromPointer = new WTypes.LPWSTR(TEST_POINTER);
assertEquals(fromPointer.getValue(), TEST_STRING);
}

public static void main(String[] args) {
junit.textui.TestRunner.run(WTypesTest.class);
}
}