nimskull
nimskull copied to clipboard
procedure result is not cleaned up if the procedure raises
Example
type Object = object
has: bool
var wasDestroyed: bool
proc `=destroy`(x: var Object) =
if x.has:
wasDestroyed = true
proc create(doRaise: bool): Object =
result = Object(has: true)
if doRaise:
raise CatchableError.newException("")
proc test() =
try:
var x = create(true)
except:
# the value created in `create` must have been destroyed at this point
doAssert wasDestroyed
test()
Actual Output
The assertion fails.
Expected Output
The program compiles and runs successfully.
Possible Solution
Either the caller or callee needs to destroy the result in case the callee raises. The simplest solution would be wrapping the body of a procedure that can raise in a try
/except
that destroys the result
and re-raises the exception.
Additional Information
This is a problem at the MIR-level, meaning that all backends are affected.