Closed JPEWdev closed 3 months ago
This is correct behaviour (see §7.6.5). Calling reset()
on a (not yet released) resource is going to call the deleter on it.
In your example this will free the memory, thus you can't use it afterwards.
A resource can be of any type, thus there's no universally valid value. As you have mentioned already – you can pass some kind of empty or null-like value to reset(…)
if type and domain allow this.
Btw. depending on your compiler and settings you might get an use-after-free warning on *p = 2;
.
@JPEWdev Does this solve your issue?
I'm closing here as the behaviour is correct. Please feel free to reopen if there's something left :-).
When using
unique_resource
I discovered that it happily let me access memory that was invalid after callingreset()
. For example:I originally came across this because I (incorrectly) assumed that
reset()
was equivalent toreset(nullptr)
as is the case forstd::shared_ptr
et. al.I realize that
unique_resource
isn't necessarily a pointer so it might not be possible to return a known value (like nullptr) if the resource is no longer valid, but there doesn't actually appear to be any way to know if the resource is valid or not right now.In the mean time, I was able to fix this by calling
reset(nullptr)
and making sure that my deleter correctly handlesnullptr