-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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] Memory zeroed after =sink
even for self assignment
#16185
Comments
FYI, Expected way of writing this example: type
CpuStorage*[T] = ref CpuStorageObj[T]
CpuStorageObj[T] = object
size*: int
raw_buffer*: ptr UncheckedArray[T]
Tensor[T] = object
buf*: CpuStorage[T]
TestObject = object
x: Tensor[float]
proc `=destroy`[T](s: var CpuStorageObj[T]) =
if s.raw_buffer != nil:
s.raw_buffer.deallocShared()
s.size = 0
s.raw_buffer = nil
proc `=`[T](a: var CpuStorageObj[T]; b: CpuStorageObj[T]) {.error.}
proc allocCpuStorage[T](s: var CpuStorage[T], size: int) =
new(s)
s.raw_buffer = cast[ptr UncheckedArray[T]](allocShared0(sizeof(T) * size))
s.size = size
proc newTensor[T](size: int): Tensor[T] =
allocCpuStorage(result.buf, size)
proc `[]`[T](t: Tensor[T], idx: int): T = t.buf.raw_buffer[idx]
proc `[]=`[T](t: Tensor[T], idx: int, val: T) = t.buf.raw_buffer[idx] = val
proc toTensor[T](s: seq[T]): Tensor[T] =
result = newTensor[T](s.len)
for i, x in s:
result[i] = x
proc main() =
var t: TestObject
t.x = toTensor(@[1.0, 2, 3, 4])
t.x = t.x
# the following line holds true even if it shouldn't of course
echo t.x.buf.isNil
main() Bug, i will take a look |
Thanks for the super quick response and the example! The additional |
I've just stumbled on a much simpler version of this: type
ValueKind* = enum
VNull,
VFloat,
VString
Value* = object
case kind*: ValueKind
of VString:
str*: string
of VFloat:
fnum*: float
of VNull:
discard
proc `%~`*(v: string): Value =
result = Value(kind: VString, str: v)
proc `%~`*(v: SomeFloat): Value =
result = Value(kind: VFloat, fnum: v.float)
proc main =
var p: tuple[x: Value, y: Value]
p.x = %~ 1.2
p.y = %~ 5.5
echo p # all fine
p.y = if p.y.kind == VNull: %~ 0.0 else: p.y
echo p.y # p.y is going to be `VNull` after this self assignment
main() If the string branch of the |
* fix nim-lang#16185 * fix test * fix comment * fix comment * better approach
This reverts commit bb4b27a.
* fix #16185 * fix test * fix comment * fix comment * better approach * Add more tests and move sameLocation to injectdestructors * Better and more strict sameLocation * Small cleanup and preliminary spec clarification * Fix * Fix doc * Expand test Co-authored-by: Andrey R (cooldome) <[email protected]>
* fix nim-lang#16185 * fix test * fix comment * fix comment * better approach
This reverts commit bb4b27a.
* fix nim-lang#16185 * fix test * fix comment * fix comment * better approach * Add more tests and move sameLocation to injectdestructors * Better and more strict sameLocation * Small cleanup and preliminary spec clarification * Fix * Fix doc * Expand test Co-authored-by: Andrey R (cooldome) <[email protected]>
* test arrayamncer with ORC * fixes shallowCopy * one more fix * one more fix * nim-lang/Nim#16185 has been fixed * it works since 1.6
Working on mratsim/Arraymancer#477 I've chased a weird segfault originating for self assignments of tensors, which only happens for tensors stored in objects.
I've reduced it to a relatively simple piece of code now, which mostly reproduces the bug. However, in my actual code the issue also happens if compiled without
--gc:arc
(sink is used in that case even for default GC. Does that make sense? Or should the=sink
procs always be guarded bydefined(gcDestructors)
?).Or maybe I still misunderstand how to use ARC?
Example
Current Output
Nothing, because the
doAssert
falsely holds.Expected Output
doAssert
should not hold, thus program should fail.Additional Information
It seems to be a more complex version of #10689 to me.
The generated C code (using
--gc:arc -d:danger
):The latter
nimZeroMem
shouldn't be there, because theeqsink
before is a self assignment. It doesn't matter if I define the=sink
in the code or not.I just pulled the newest devel:
The text was updated successfully, but these errors were encountered: