fix double free bug

Addresses #340.
if there is an error, we break - but after we incremented the ref-counter? just to make sure this is the intended behaviour.