First, when I run your code fragment that has the duplicate name ( 'dend5.1', 'dend5.1') I get
Code: Select all
File "sec.py", line 8, in make_sec
raise Exception("Section %s already exists." % name)
Exception: Section dend5.1 already exists.
which, I gather, is what you intended with the fragment:
Code: Select all
if name in sections:
raise Exception("Section %s already exists." % name)
I'll assume this aspect just introduces some confusion and the duplicate name issue is irrelevant to your point, so I changed one the of the dend5.1 to dend5.1.1 .
Of course, the assertion at the end of the script fails
But on my machine it does not fail because the the answer to
Code: Select all
sections.clear() # no more Section references, right?
was yes when I put the fragment in file and executed it or copy pasted to the terminal.
However, I am aware of the possibiltiy of deferred garbage collection and so added the fragment
Code: Select all
for s in h.allsec(): h.delete_section()
Unfortunately, although this would work for HOC sections that are instantiated via h('create ...') statements (and in fact is the ONLY way to destroy such sections), if one tries to do this with a Python created section, you get:
Code: Select all
nrniv: Cannot delete an unnamed section
So this raises the delicate question of whether I should change the behavion of h.delete_section() so that it removes Python created sections from the internal list of sections that are to be
simulated (and causes an error message to be generated if the section is used in any way). It also raises a little flock of questions about alternatives to deleting sections and merely
removing them from the list of sections to be simulated.
Anyway, so far I am unable to reproduce the problem we are discussing and it would be helpful to have a more robust example. Is it really the case that the problem is that a section is
not deleted when the reference count goes to 0, or is the problem that the program code or user or graphical interface holds a reference to the section that no one knows about and therefore
finds it difficult to destroy the section?