Nathann Cohen
unread,Apr 26, 2015, 1:55:44 PM4/26/15Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Sign in to report message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to cython...@googlegroups.com
Hello everybody,
I come to you with a problem, without knowing what a Cython answer might be.
iterators that leak memory:
I often have to create iterators (i.e. use the yield keyword) in a function
that allocates heap memory. At the end of the function I can free it, but of
course that part of the code is never read if the iterator is not read
"until the end". Thus, iterators that are not read to their end never free
their memory.
def my_iterator():
<allocate_memory>
while True:
<do_something>
yield <something>
<deallocate_memory>
Usual workaround:
This problem can be solved by creating a custom class, with a `__cinit__`
(to allocate the memory) function, a `__iter__` function (which contains the
yield) and a `__dealloc__` (to free the memory) to replace the iterator. But
that is seriously more code, more doctests, more documentation to write when
all we need is a 'free'.
It is easier to deal with exceptions:
When a code that allocates memory is interrupted by an exception, it is not
a problem to free the memory. For instance:
def my_function():
try:
<allocate memory>
<do whatever you like>
finally:
<free memory>
Would it be possible to have a similar syntax for iterators? Would it be
possible that the iterator function itself would trigger a
"IteratorIsBeingDeallocatedException", that we could use to deallocate memory?
I really have no idea how this could be made available. I just thought that the
exception mechanism made things so simple that it could be used to solve this
problem.
Thaaaaaaaaaaanks,
Nathann