Make init_holder do registration, and rename to init_instance

The instance registration for offset base types fails (under macOS, with
a segfault) in the presense of virtual base types.  The issue occurs
when trying to `static_cast<Base *>(derived_ptr)` when `derived_ptr` has
been allocated (via `operator new`) but not initialized.

This commit fixes the issue by moving the addition to
`registered_instances` into `init_holder` rather than immediately after
value pointer allocation.

This also renames it to `init_instance` since it does more than holder
initialization now.  (I also further renamed `init_holder_helper` to
`init_holder` since `init_holder` isn't used anymore).

Fixes #959.
This commit is contained in:
Jason Rhinelander
2017-07-25 00:53:23 -04:00
parent 44a17e1f3d
commit 353615f77e
7 changed files with 57 additions and 22 deletions

View File

@@ -201,4 +201,20 @@ TEST_SUBMODULE(multiple_inheritance, m) {
py::class_<VanillaDictMix1, Vanilla, WithDict>(m, "VanillaDictMix1").def(py::init<>());
py::class_<VanillaDictMix2, WithDict, Vanilla>(m, "VanillaDictMix2").def(py::init<>());
#endif
// test_diamond_inheritance
// Issue #959: segfault when constructing diamond inheritance instance
// All of these have int members so that there will be various unequal pointers involved.
struct B { int b; virtual ~B() = default; };
struct C0 : public virtual B { int c0; };
struct C1 : public virtual B { int c1; };
struct D : public C0, public C1 { int d; };
py::class_<B>(m, "B")
.def("b", [](B *self) { return self; });
py::class_<C0, B>(m, "C0")
.def("c0", [](C0 *self) { return self; });
py::class_<C1, B>(m, "C1")
.def("c1", [](C1 *self) { return self; });
py::class_<D, C0, C1>(m, "D")
.def(py::init<>());
}