Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

destructor failing should not lead to leaks #1012

Closed
alaviss opened this issue Nov 4, 2023 · 1 comment
Closed

destructor failing should not lead to leaks #1012

alaviss opened this issue Nov 4, 2023 · 1 comment
Assignees
Labels
bug Something isn't working compiler General compiler tag

Comments

@alaviss
Copy link
Contributor

alaviss commented Nov 4, 2023

Example

var counter = 0

type
  Lease = object
    initialized: bool

proc `=destroy`(v: var Lease) =
  if v.initialized:
    dec counter

proc lease(): Lease =
  inc counter
  Lease(initialized: true)

type
  Fail = object
    initialized: bool

  FailError = object of CatchableError

proc `=destroy`(v: var Fail) =
  if v.initialized:
    raise newException(FailError, "operation failed")

proc initFail(): Fail =
  Fail(initialized: true)

proc leaky() =
  let a = lease()
  let b = initFail()

proc main() =
  doAssert counter == 0, "counter is not zero at start"

  try:
    leaky()
  except FailError:
    discard "not an issue"

  doAssert counter == 0, "the lease should be released"

main()

Actual Output

fatal.nim(48) sysFatal
Error: unhandled exception: test.nim(40, 12) `counter == 0` the lease should be released [AssertionDefect]

Expected Output

No output

Possible Solution

  • Force Defect to be the only allowed exception type raised by destructors
  • Generates code to continue cleanup even after exception, then raises the exception stack afterwards
@alaviss alaviss added bug Something isn't working compiler General compiler tag labels Nov 4, 2023
@zerbina zerbina self-assigned this Mar 15, 2024
@zerbina
Copy link
Collaborator

zerbina commented Mar 17, 2024

With #1236, hooks can no longer raise exceptions of any kind, so it's not possible for leaks to happen anymore.

@zerbina zerbina closed this as completed Mar 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler General compiler tag
Projects
None yet
Development

No branches or pull requests

2 participants