ARC: fixes memory leaks with newSeq used in a loop [backport:1.4] (#18040)

* ARC: fixes memory leaks with newSeq used in a loop [backport:1.4]
* Update tests/arc/tnewseq_legacy.nim
This commit is contained in:
Andreas Rumpf 2021-05-18 21:45:37 +02:00 • committed by GitHub
commit 53935b8b27
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 1 deletions

View file

@ -2319,7 +2319,12 @@ proc genMagicExpr(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
gcUsage(p.config, e) gcUsage(p.config, e)
else: else:
genNewFinalize(p, e) genNewFinalize(p, e)
of mNewSeq: genNewSeq(p, e) of mNewSeq:
if optSeqDestructors in p.config.globalOptions:
e[1] = makeAddr(e[1], p.module.idgen)
genCall(p, e, d)
else:
genNewSeq(p, e)
of mNewSeqOfCap: genNewSeqOfCap(p, e, d) of mNewSeqOfCap: genNewSeqOfCap(p, e, d)
of mSizeOf: of mSizeOf:
let t = e[1].typ.skipTypes({tyTypeDesc}) let t = e[1].typ.skipTypes({tyTypeDesc})

View file

@ -124,3 +124,7 @@ proc setLen[T](s: var seq[T], newlen: Natural) =
if xu.p == nil or xu.p.cap < newlen: if xu.p == nil or xu.p.cap < newlen:
xu.p = cast[typeof(xu.p)](prepareSeqAdd(oldLen, xu.p, newlen - oldLen, sizeof(T), alignof(T))) xu.p = cast[typeof(xu.p)](prepareSeqAdd(oldLen, xu.p, newlen - oldLen, sizeof(T), alignof(T)))
xu.len = newlen xu.len = newlen
proc newSeq[T](s: var seq[T], len: Natural) =
shrink(s, 0)
setLen(s, len)

View file

@ -0,0 +1,13 @@
discard """
output: "(allocCount: 201, deallocCount: 201)"
cmd: "nim c --gc:orc -d:nimAllocStats $file"
"""
proc main(prefix: string) =
var c: seq[string]
for i in 0..<100:
newSeq(c, 100)
c[i] = prefix & $i
main("abc")
echo getAllocStats()