Placing the pointer inside shared_ptr transfers ownership of the pointer to shared_ptr, so shared_ptr is responsible for removing it. This is a conceptually important operation, so the shared_ptr developers did not want this to just happen as part of its normal form. For example, they wanted to prevent code such as:
some_shared_ptr = some_other_smart_pointer.get();
which looks pretty innocuous, but would mean that both smart pointers believed that they were responsible for clearing the pointer and probably deleting the pointer twice or something like that.
This is what happens with your debug statement. A call to SetNextPtr(&lis) transfers ownership of &lis to shared_ptr , and โownershipโ means that shared_ptr will call delete on its pointer when the last copy of shared_ptr is out of scope. That way, you are actually deleting the local (stack) variable - lis - which corrupts the stack and causes a crash.
Doug
source share