Skip to content

The hook =destroy not called if cycled ref exists when using orc #24161

Open
@forchid

Description

@forchid

Description

The test source

import std/strformat

type
    Person = object of RootObj
        name: string
        age: uint
    Student = object of Person
        id: uint
        self: StudentRef
    StudentRef = ref Student

proc say(s: Student) =
    echo(fmt"Student {s.name}#{s.id} and {s.age} years old")

proc `=destroy`(s: var Student) = # Here `var` compatible with nim-1.6.20
    echo(fmt"Student {s.name}#{s.id} destroyed.")

proc test(useCycle: bool, id: uint) =
    echo(fmt"Use cycled ref? {useCycle}")
    var t = StudentRef(name: "James", age: 36, id: id)
    say(t[])
    # `=destroy` not called if cycled ref exists when using orc!
    if useCycle: 
        t.self = t

test(false, 1)
echo("========================================")
test(true,  2)

Nim Version

The nim 1.6.20

>nim -v
Nim Compiler Version 1.6.20 [Windows: amd64]
Compiled at 2024-04-07
Copyright (c) 2006-2023 by Andreas Rumpf

active boot switches: -d:release

The nim 2.0.8

>%nim20_home%\bin\nim -v
Nim Compiler Version 2.0.8 [Windows: amd64]
Compiled at 2024-07-03
Copyright (c) 2006-2023 by Andreas Rumpf

active boot switches: -d:release

Current Output

# The nim 1.6.20

nim\mem>nim c -d:release --mm:orc dnc.nim
Hint: used config file 'nim-1.6.20\config\nim.cfg' [Conf]
Hint: used config file 'nim-1.6.20\config\config.nims' [Conf]
..........................................................................
nim\mem\dnc.nim(15, 6) template/generic instantiation from here
nim\mem\dnc.nim(16, 13) Warning: formatValue(fmtRes_486539341, s.name, "") can raise an unlisted exception: ref ValueError [Effect]
Hint:  [Link]
Hint: gc: orc; opt: speed; options: -d:release
42215 lines; 0.800s; 48.746MiB peakmem; proj: nim\mem\dnc.nim; out: nim\mem\dnc.exe [SuccessX]

nim\mem>dnc
Use cycled ref? false
Student James#1 and 36 years old
Student James#1 destroyed.
========================================
Use cycled ref? true
Student James#2 and 36 years old

# The nim 2.0.8
```shell
nim\mem>%nim20_home%\bin\nim c -d:release --mm:orc dnc.nim
Hint: used config file 'nim-2.0.8\config\nim.cfg' [Conf]
Hint: used config file 'nim-2.0.8\config\config.nims' [Conf]
...........................................................................................
nim\mem\dnc.nim(15, 1) Warning: A custom '=destroy' hook which takes a 'var T' parameter is deprecated; it should take a 'T' parameter [Deprecated]
CC: dnc.nim
Hint:  [Link]
Hint: mm: orc; threads: on; opt: speed; options: -d:release
42545 lines; 0.910s; 57.543MiB peakmem; proj: nim\mem\dnc.nim; out: nim\mem\dnc.exe [SuccessX]

nim\mem>dnc
Use cycled ref? false
Student James#1 and 36 years old
Student James#1 destroyed.
========================================
Use cycled ref? true
Student James#2 and 36 years old


### Expected Output

```text
# The nim 1.6.20

nim\mem>dnc
Use cycled ref? false
Student James#1 and 36 years old
Student James#1 destroyed.
========================================
Use cycled ref? true
Student James#2 and 36 years old
Student James#2 destroyed.

# The nim 2.0.8
```shell
nim\mem>dnc
Use cycled ref? false
Student James#1 and 36 years old
Student James#1 destroyed.
========================================
Use cycled ref? true
Student James#2 and 36 years old
Student James#2 destroyed.


### Known Workarounds

_No response_

### Additional Information

_No response_

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationRelated to documentation content (not generation).

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions