[shared_ptr] Set pointer to null

Hi, I'd like to make a custom deleter resetting pointer to 0 or other value (e.g. for debugging purposes) after resource has been deallocated. Would anyone suggest better option to this presented below? struct DeleterT { void operator()(::T* ptr) { ::DestroyT(ptr); // uses Visual C++ specific integer type *reinterpret_cast<__int64*>(&ptr) = 0; } }; Best regards, -- Mateusz Loskot, http://mateusz.loskot.net

Mateusz Loskot wrote:
Hi,
I'd like to make a custom deleter resetting pointer to 0 or other value (e.g. for debugging purposes) after resource has been deallocated. Would anyone suggest better option to this presented below?
struct DeleterT { void operator()(::T* ptr) { ::DestroyT(ptr);
// uses Visual C++ specific integer type *reinterpret_cast<__int64*>(&ptr) = 0; } }; This doesn't actually do anything. The code is undefined (accessing casted pointer, aliasing violation), but even the most common interpretation simply does the same thing as "ptr = 0;". ptr is a by-val argument. You cannot know where the original member you want to reset is, so you can't reset it. All you are modifying here is your local function argument - and possibly the function return address, if you're on a 32-bit system ...
Sebastian

Sebastian Redl wrote:
Mateusz Loskot wrote:
Hi,
I'd like to make a custom deleter resetting pointer to 0 or other value (e.g. for debugging purposes) after resource has been deallocated. Would anyone suggest better option to this presented below?
struct DeleterT { void operator()(::T* ptr) { ::DestroyT(ptr);
// uses Visual C++ specific integer type *reinterpret_cast<__int64*>(&ptr) = 0; } };
This doesn't actually do anything. The code is undefined (accessing casted pointer, aliasing violation), but even the most common interpretation simply does the same thing as "ptr = 0;". ptr is a by-val argument.
Yes, I aware of that. I use this undefined behaviour effect with premeditaion under Visual C++. But I agree it's extremely ugly.
You cannot know where the original member you want to reset is, so you can't reset it. All you are modifying here is your local function argument - and possibly the function return address, if you're on a 32-bit system ...
Right. At least, I've got confirmed that it's a good idea to not to follow this idea. Best regards, -- Mateusz Loskot, http://mateusz.loskot.net
participants (2)
-
Mateusz Loskot
-
Sebastian Redl