I'm new to (boost) shared pointers and I'm trying to test something in a code which I don't want to rewrite bottom up for that purpose.
It turns out, I can solve my problem quite elegantely, when using one of their structs StructA
and have the member variable shared_ptr<StructA> foo
point to the instance of the struct object.
this is the simplified struct:
struct StructA {
vars...
funcs ...
shared_ptr<StructA> foo;
};
This results in problems of self-reset errors, so I tried to undo the trick, but it doesn't work:
// <their code> (simplified)
StructA lp;
lp.SomePreparation(...);
for-loop () {
lp.SetSomething(...);
// my test
lp.foo.reset(&lp);
// <their large code section which I'd rather not touch...>
// which now works elegantly :)
// A: doing nothing here, will result in self-reset assertation when
// hitting the `reset(&lp)` above again in the loop. Detailed error below.
lp.foo.reset(); // B: doing this here, results in glibc double free
lp.foo.reset(new StructA()); // C: so does this
// lp.foo = NULL; // what I'd do to a pointer in C.
}
No foo.reset() called: Self-reset error
mybin: include/boost/smart_ptr/shared_ptr.hpp:397: void boost::shared_ptr<T>::reset(Y*) [with Y = StructA, T = StructA]: Assertion `p == 0 || p != px' failed.
Aborted
Calling foo.reset():
*** glibc detected *** mybin: double free or corruption (out): 0x00007fff974b3a30 ***
======= Backtrace: =========
/lib64/libc.so.6[0x342b6760e6]
/lib64/libc.so.6[0x342b678c13]
mybin(_ZN5boost14checked_deleteIN1214StructAEEEvPT_+0x26)[0xdd7dfd]
mybin(_ZN5boost6detail17sp_counted_impl_pIN1214LStructAEE7disposeEv+0x1c)[0xdd8b9a]
mybin(_ZN5boost6detail15sp_counted_base7releaseEv+0x42)[0xcedf54]
mybin(_ZN5boost6detail12shared_countD1Ev+0x27)[0xcee017]
mybin(_ZN5boost10shared_ptrIN1214StructAEED1Ev+0x1c)[0xd2dc38]
mybin(_ZN5boost10shared_ptrIN12C14StructAEE5resetEv+0x5b)[0x149b3a9]
....