執行低級別X11編程時,通常需要在返回的結構上調用XFree()。例如XGetWMName,我在下面的代碼,然後與JNA X11和XFree
AssertionMessage: *** Error in `/opt/jdk1.8.0_40/bin/java': double free or corruption (fasttop): 0x00007f7ca822bdd0 ***
這是由XFree的()調用導致幾秒鐘後炸燬做到了這一點,如果被去除的問題消失。但是監測與頂級工藝穩定顯示內存增長暗示我需要它...
public class X11WindowFinder {
private X11 x11;
public X11WindowFinder() {
x11 = X11.INSTANCE;
}
public List<Window> find(Pattern title) {
Display display = x11.XOpenDisplay(null);
Window root = x11.XDefaultRootWindow(display);
List<Window> windows = recurse(x11, display, root, title);
x11.XCloseDisplay(display);
return windows;
}
private synchronized List<Window> recurse(X11 x11, Display display, Window root, Pattern pattern) {
List<Window> windows = new ArrayList<>(1);
X11.WindowByReference windowRef = new X11.WindowByReference();
X11.WindowByReference parentRef = new X11.WindowByReference();
PointerByReference childrenRef = new PointerByReference();
IntByReference childCountRef = new IntByReference();
x11.XQueryTree(display, root, windowRef, parentRef, childrenRef, childCountRef);
if (childrenRef.getValue() == null) {
return Collections.emptyList();
}
long[] ids = {};
if (Native.LONG_SIZE == Long.BYTES) {
ids = childrenRef.getValue().getLongArray(0, childCountRef.getValue());
} else if (Native.LONG_SIZE == Integer.BYTES) {
int[] intIds = childrenRef.getValue().getIntArray(0, childCountRef.getValue());
ids = new long[intIds.length];
for (int i = 0; i < intIds.length; i++) {
ids[i] = intIds[i];
}
}
for (long id : ids) {
Window child = new Window(id);
X11.XTextProperty name = new X11.XTextProperty();
x11.XGetWMName(display, child, name);
String value = name.value;
if (value != null) {
System.out.println(String.format("Found window %s free %s", value, name));
}
if (value != null && pattern.matcher(value).matches()) {
windows.add(child);
}
x11.XFree(name.getPointer());
windows.addAll(recurse(x11, display, child, pattern));
}
return windows;
}
public static void main(String[] args) {
X11WindowFinder finder = new X11WindowFinder();
while (true) {
finder.find(Pattern.compile(".*Firefox.*"));
}
}
}
在JNA指針似乎使用的finalize()來釋放內存,即com.sun.jna.Memory.finalize()
/** Properly dispose of native memory when this object is GC'd. */
protected void finalize() {
dispose();
}
所以,如果我打電話給XFree()我冒着被雙重釋放的風險...這個假設是否正確。我可以找到幾個JNA的例子,尤其是X11平臺相關的例子。
XLib不會爲您分配'XTextProperty'。你用'new X11.XTextProperty()'分配一個。因此你不應該'XFree'它。 –