Skip to content

Commit 92d7724

Browse files
committed
Add DANGER ZONE comment in detail/init.h, similar to a comment added on the smart_holder branch (all the way back in 2021).
1 parent 54fd559 commit 92d7724

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

include/pybind11/detail/init.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,13 @@ void construct(value_and_holder &v_h, Cpp<Class> *ptr, bool need_alias) {
128128
// the holder and destruction happens when we leave the C++ scope, and the holder
129129
// class gets to handle the destruction however it likes.
130130
v_h.value_ptr() = ptr;
131-
v_h.set_instance_registered(true); // To prevent init_instance from registering it
132-
v_h.type->init_instance(v_h.inst, nullptr); // Set up the holder
131+
v_h.set_instance_registered(true); // Trick to prevent init_instance from registering it
132+
// DANGER ZONE BEGIN: exceptions will leave v_h in an invalid state.
133+
v_h.type->init_instance(v_h.inst, nullptr); // Set up the holder
133134
Holder<Class> temp_holder(std::move(v_h.holder<Holder<Class>>())); // Steal the holder
134135
v_h.type->dealloc(v_h); // Destroys the moved-out holder remains, resets value ptr to null
135136
v_h.set_instance_registered(false);
137+
// DANGER ZONE END.
136138

137139
construct_alias_from_cpp<Class>(is_alias_constructible<Class>{}, v_h, std::move(*ptr));
138140
} else {

0 commit comments

Comments
 (0)