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 not called for temp objects passed as arguments #22866

Closed
yglukhov opened this issue Oct 24, 2023 · 1 comment · Fixed by #22901
Closed

Destructor not called for temp objects passed as arguments #22866

yglukhov opened this issue Oct 24, 2023 · 1 comment · Fixed by #22901

Comments

@yglukhov
Copy link
Member

yglukhov commented Oct 24, 2023

Description

type
  SmartPtr = object
    o: int

proc newId(): int =
  var g {.global.} = 0
  inc g
  echo "new ", g
  g

proc `=destroy`*(o: var SmartPtr) =
  if o.o != 0:
    echo "destroy ", o.o
    o.o = 0

proc `=copy`*(a: var SmartPtr, b: SmartPtr) =
  if a.o != 0:
    echo "delete ", a.o
  if b.o == 0:
    a.o = 0
  else:
    a.o = newId()

proc foo(s: SmartPtr) =
  discard

proc test() =
  var a = SmartPtr(o: newId())
  foo(SmartPtr(o: newId()))

test()

Nim Version

Nim Compiler Version 2.1.1 [Linux: amd64]
Compiled at 2023-10-22
Copyright (c) 2006-2023 by Andreas Rumpf

git hash: c13c48500b527cb483480702390b1f230ac896a4
active boot switches: -d:release

Current Output

new 1
new 2
destroy 1

Expected Output

Should contain destroy 2 somewhere.

Possible Solution

No response

Additional Information

No response

@ringabout
Copy link
Member

Duplicate of #19998

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants