Fix BNode reference counting issues in writer.