Class NativeString
This class takes care of freeing the memory that was natively allocated for the string, if needed.
Generally, any native API that returns "const char*" must not be freed, so in such cases the native return
type mapping can actually be String rather than Pointer. Alternatively, copyNativeString(Pointer)
can be used.
Generally, Any native API that returns "char*" must be freed, so in such cases the native return type
mapping must be Pointer and copyAndFreeNativeString(Pointer) must be used.
Where a native string is contained in a Structure those strings should not be freed if
the structure itself is subsequently freed (usually by a companion release native method), so in these cases
copyNativeString(Pointer) must be used.
-
Method Summary
Modifier and TypeMethodDescriptionstatic final StringcopyAndFreeNativeString(com.sun.jna.Pointer pointer) Get a String from a native string pointer, freeing the native string pointer when done.static final StringcopyNativeString(com.sun.jna.Pointer pointer) Copy a String from a native string pointer, without freeing the native pointer.
-
Method Details
-
copyAndFreeNativeString
Get a String from a native string pointer, freeing the native string pointer when done.If the native string pointer is not freed then a native memory leak will occur.
Use this method if the native string type is "char*", i.e. lacking the "const" modifier.
- Parameters:
pointer- pointer to native string, may benull- Returns:
- string, or
nullif the pointer wasnull
-
copyNativeString
Copy a String from a native string pointer, without freeing the native pointer.Use this method if the native string type is "const char*".
- Parameters:
pointer- pointer to native string, may benull- Returns:
- string, or
nullif the pointer wasnull
-