The managed object is not valid error when accesing shared par

So i wanted to get all the parameter binding from the document and put them in the dict.
This code works when you run it once, but after that it wont work until you restart the entire project (lets say you changed something in python script, when you hit run after change it will throw an error).

dict = {}
map=doc.ParameterBindings
it=map.ForwardIterator()
it.Reset()	
while(it.MoveNext()):
	key=it.Key
	item=it.Current
	name=key.Name
	dict[name]=[item,key]

i keep getting the “The managed object is not valid” on the row where i get the key.Name.
Any one has any idea where this error comes from?

Have you tried it.Key.Name? I know, key should work but wondering if it’s not casting it correctly.

Yes, same eror, same behaviour. The strangest thing is that it wont work again unless i restart revit.

Do it in a try / except block and dispose of the iterator. Gotta be something hanging in memory.

I know in the past I have had some issues with syntax on the iterator, but not sure here.