Skip to content

Commit

Permalink
fix: don't call free() on Windows
Browse files Browse the repository at this point in the history
This may lead to some memory leaks,
but at least it won't crash.
  • Loading branch information
ObserverOfTime committed Dec 8, 2024
1 parent 7369f7f commit 9c5eb24
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/main/java/io/github/treesitter/jtreesitter/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public final class Node {
private final Arena arena = Arena.ofAuto();
private boolean wasEdited = false;

// FIXME: figure out why free() crashes on Windows
private static final boolean IS_UNIX = !System.getProperty("os.name").startsWith("Windows");

Node(MemorySegment self, Tree tree) {
this.self = self;
this.tree = tree;
Expand Down Expand Up @@ -437,7 +440,7 @@ public TreeCursor walk() {
public String toSexp() {
var string = ts_node_string(self);
var result = string.getString(0);
free(string);
if (IS_UNIX) free(string);
return result;
}

Expand Down
7 changes: 5 additions & 2 deletions src/main/java/io/github/treesitter/jtreesitter/Tree.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public final class Tree implements AutoCloseable, Cloneable {
private final Language language;
private @Nullable List<Range> includedRanges;

// FIXME: figure out why free() crashes on Windows
private static final boolean IS_UNIX = !System.getProperty("os.name").startsWith("Windows");

Tree(MemorySegment self, Language language, @Nullable String source, @Nullable Charset charset) {
arena = Arena.ofShared();
this.self = self.reinterpret(arena, TreeSitter::ts_tree_delete);
Expand Down Expand Up @@ -92,7 +95,7 @@ public List<Range> getIncludedRanges() {
var range = TSRange.asSlice(ranges, i);
includedRanges.add(Range.from(range));
}
free(ranges);
if (IS_UNIX) free(ranges);
}
}
return Collections.unmodifiableList(includedRanges);
Expand All @@ -119,7 +122,7 @@ public List<Range> getChangedRanges(Tree newTree) {
var range = TSRange.asSlice(ranges, i);
changedRanges.add(Range.from(range));
}
free(ranges);
if (IS_UNIX) free(ranges);
return changedRanges;
}
}
Expand Down

0 comments on commit 9c5eb24

Please sign in to comment.