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

[ARC] SIGSEGV when calling a closure as a tuple field in a seq #15038

Closed
ghost opened this issue Jul 22, 2020 · 2 comments
Closed

[ARC] SIGSEGV when calling a closure as a tuple field in a seq #15038

ghost opened this issue Jul 22, 2020 · 2 comments

Comments

@ghost
Copy link

ghost commented Jul 22, 2020

This code works on refc, but doesn't work on ARC. As I presume the problem is that for some reason Nim doesn't detect that mCreator is captured by the add and it destroys the closure. Found in https://github.com/ftsf/nimsynth when trying to compile it with ARC :)

Example

type
  Machine = ref object
    hello: string

var machineTypes: seq[tuple[factory: proc(): Machine]]

proc registerMachine(factory: proc(): Machine) =
  var mCreator = proc(): Machine =
    result = factory()

  machineTypes.add((factory: mCreator))

proc facproc(): Machine = 
  result = Machine(hello: "hello")

registerMachine(facproc)

proc createMachine = 
  for machine in machineTypes:
    echo machine.factory().hello
    # different crash with this:
    # echo machine.factory()[]

createMachine()

Current Output

hello
Traceback (most recent call last)
/home/dian/Stuff/arctest/a.nim(5) a
/home/dian/Things/Nim/lib/system/refs_v2.nim(177) nimDestroyAndDispose
/home/dian/Things/Nim/lib/system/refs_v2.nim(162) nimRawDispose
/home/dian/Things/Nim/lib/system/alloc.nim(972) dealloc
/home/dian/Things/Nim/lib/system/alloc.nim(871) rawDealloc
/home/dian/Things/Nim/lib/system/avltree.nim(74) del
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

Expected Output

hello
$ nim -v
Nim Compiler Version 1.3.5 [Linux: amd64]
Compiled at 2020-07-21
Copyright (c) 2006-2020 by Andreas Rumpf

git hash: 911e39351e34c1022b586fe9bfc3a67b2ce3bf44
active boot switches: -d:release -d:danger
@ghost ghost changed the title [ARC] Closure captured in a seq gets destroyed in the end of the scope [ARC] SIGSEGV when calling a closure as a tuple field in a seq Jul 22, 2020
@Araq
Copy link
Member

Araq commented Jul 22, 2020

@Clyybber this one is for you.

@Araq
Copy link
Member

Araq commented Jul 25, 2020

I almost have a fix for this.

@Araq Araq assigned Araq and unassigned Clyybber Jul 25, 2020
Araq added a commit that referenced this issue Jul 25, 2020
@Araq Araq closed this as completed in aae998f Jul 25, 2020
narimiran pushed a commit that referenced this issue Jul 29, 2020
(cherry picked from commit aae998f)
mildred pushed a commit to mildred/Nim that referenced this issue Jan 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants