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

SIGSEGV when converting lines to closure iterator, most likely caused by defer #5321

Closed
bluenote10 opened this issue Feb 2, 2017 · 1 comment
Closed
Labels

Comments

@bluenote10
Copy link
Contributor

@bluenote10 bluenote10 commented Feb 2, 2017

I was trying to convert the (inline) lines operator into a closure iterator like so:

proc lineIter*(filename: string): iterator(): string =
  result = iterator(): string {.closure.} =
    for line in lines(filename):
      yield line

var count = 0
let iter = lineIter("/tmp/test_file")
for line in iter():
  count += 1

This crashes with: SIGSEGV: Illegal storage access. (Attempt to read from nil?)

The same goes for rewriting the lines iterator directly as a closure iterator:

iterator lineIter*(filename: string): string {.closure.} =
  var f = open(filename, bufSize=8000)
  defer: close(f)   # <-- commenting defer "solves" the problem
  var res = TaintedString(newStringOfCap(80))
  while f.readLine(res): yield res

var count = 0
for line in lineIter("/tmp/test_file"):
  count += 1
echo count

Commenting out the defer avoids the crash, but probably leaves the file open.

Might be related to #4966

@Araq Araq added the Crash label Feb 2, 2017
@Araq
Copy link
Member

@Araq Araq commented Feb 2, 2017

Seems to be a duplicate of #4966 indeed.

@Araq Araq closed this in 4644484 Oct 18, 2018
narimiran added a commit to narimiran/Nim that referenced this issue Oct 31, 2018
narimiran added a commit to narimiran/Nim that referenced this issue Nov 1, 2018
narimiran added a commit that referenced this issue Nov 1, 2018
narimiran added a commit that referenced this issue Nov 1, 2018
(cherry picked from commit 4644484)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants