Skip to content

Commit f3e6609

Browse files
lesteveogrisel
authored andcommitted
MAINT simplify freeing logic for Barnes-Hut SNE memory leak fix
1 parent 8007d91 commit f3e6609

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

sklearn/manifold/_barnes_hut_tsne.pyx

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -365,20 +365,20 @@ cdef int free_tree(Tree* tree) nogil:
365365
for i in range(3):
366366
cnt[i] = 0
367367
free_recursive(tree, tree.root_node, cnt)
368-
if not tree.root_node.is_leaf:
369-
free(tree.root_node.children)
370-
free(tree.root_node.width)
371-
free(tree.root_node.left_edge)
372-
free(tree.root_node.center)
373-
free(tree.root_node.barycenter)
374-
free(tree.root_node.leaf_point_position)
375-
free(tree.root_node)
376368
check = cnt[0] == tree.n_cells
377369
check &= cnt[2] == tree.n_points
378370
free(tree)
379371
free(cnt)
380372
return check
381373

374+
cdef void free_post_children(Node *node) nogil:
375+
free(node.width)
376+
free(node.left_edge)
377+
free(node.center)
378+
free(node.barycenter)
379+
free(node.leaf_point_position)
380+
free(node)
381+
382382
cdef void free_recursive(Tree* tree, Node *root, long* counts) nogil:
383383
# Free up all of the tree nodes recursively
384384
# while counting the number of nodes visited
@@ -396,13 +396,14 @@ cdef void free_recursive(Tree* tree, Node *root, long* counts) nogil:
396396
counts[2] +=1
397397
else:
398398
free(child.children)
399-
free(child.width)
400-
free(child.left_edge)
401-
free(child.center)
402-
free(child.barycenter)
403-
free(child.leaf_point_position)
404-
free(child)
405399

400+
free_post_children(child)
401+
402+
if root == tree.root_node:
403+
if not root.is_leaf:
404+
free(root.children)
405+
406+
free_post_children(root)
406407

407408
cdef long count_points(Node* root, long count) nogil:
408409
# Walk through the whole tree and count the number

0 commit comments

Comments
 (0)