carlw
11/19/2018, 9:43 PMalex
11/20/2018, 6:55 AMwhile (true) {
val ptr = PointerByReference() Shell32.INSTANCE.SHGetKnownFolderPath(KnownFolders.FOLDERID_Desktop, 0, null, ptr)
val sliced = ptr.value.getCharArray(0, WinDef.MAX_PATH).takeWhile { it != '\u0000' }.toCharArray()
val pathStr = String(sliced)
// free memory ???
println(pathStr)
}
Without freeing it ended up with Caused by: java.lang.Error: Invalid memory access
after 1-2 seconds at the line with sliced
.
If I add Ole32.INSTANCE.CoTaskMemFree(ptr.value)
at the end of the loop, there is no exception. I suppose freeing is necessary, but I wonder why documentation is so unclear about it (or at least why I can not find the right page).alex
11/20/2018, 6:58 AMalex
11/20/2018, 7:03 AMNative.free(Pointer.nativeValue(ptr.value))
this one seems like correct onealex
11/20/2018, 7:05 AMalex
11/20/2018, 7:10 AMval ptr = PointerByReference()
try { Shell32.INSTANCE.SHGetKnownFolderPath(KnownFolders.FOLDERID_Desktop, 0, null, ptr)
val sliced = ptr.value.getCharArray(0, WinDef.MAX_PATH).takeWhile { it != '\u0000' }.toCharArray()
val pathStr = String(sliced)
return Paths.get(pathStr)
} finally {
Native.free(Pointer.nativeValue(ptr.value))
}