Skip to content

Commit 28f555d

Browse files
Store annotated local variables to avoid memory leaks (pyccel#1904)
Add annotated local variable to the `_allocs` variable so its deallocation node can be added in `_garbage_collector()`. This fixes pyccel#1903
1 parent 5a75f34 commit 28f555d

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ All notable changes to this project will be documented in this file.
3636
- #1853 : Fix translation of a file whose name conflicts with Fortran keywords.
3737
- Link and mention `devel` branch, not `master`.
3838
- #1047 : Print the value of an unrecognised constant.
39+
- #1903 : Fix memory leak when using type annotations on local variables.
3940

4041
### Changed
4142

pyccel/parser/semantic.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1683,7 +1683,9 @@ def _ensure_inferred_type_matches_existing(self, class_type, d_var, var, is_auga
16831683

16841684
new_expressions.append(Allocate(var, shape=d_var['shape'], status=status))
16851685

1686-
if status != 'unallocated':
1686+
if status == 'unallocated':
1687+
self._allocs[-1].add(var)
1688+
else:
16871689
errors.report(ARRAY_REALLOCATION, symbol=var.name,
16881690
severity='warning',
16891691
bounding_box=(self.current_ast_node.lineno,

tests/ndarrays/leaks_check.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# pylint: disable=missing-function-docstring, missing-module-docstring
2-
from numpy import array, zeros
2+
from numpy import array, zeros, ones
33

44
def create_array():
55
a = array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])# pylint: disable=unused-variable
@@ -45,6 +45,9 @@ def arrays_in_multi_returns():
4545
b = zeros(4)
4646
return a, b, 4
4747

48+
def type_annotated_numpy_array():
49+
a : 'float[:]' = ones(5) # pylint: disable=unused-variable
50+
4851
# testing garbage collecting in a Function
4952

5053
if __name__ == '__main__':
@@ -56,6 +59,7 @@ def arrays_in_multi_returns():
5659
conditional_alloc(True,True)
5760
conditional_alloc(True,False)
5861
conditional_alloc(False,False)
62+
type_annotated_numpy_array()
5963

6064
# testing garbage collecting in a Program
6165

0 commit comments

Comments
 (0)