On Wednesday, 28 November 2012 at 18:08:59 UTC, Dan wrote:
Thanks! I see what you are saying in valgrind. However, the following shows no problem in valgrind. Same code, only using S instead of RefCounted!(int).
How could that be explained?
Note that both RefCount!() and your posted S have opAssign, whereas the one below does not. Perhaps it is a problem only when there exists a custom opAssign?


Thanks
Dan

<skipped>

Because code does not access memory expected to be corrupted.

import core.stdc.stdio : printf;

struct S {
  long x = 42;
  version(bug) {
    void opAssign(S rhs)
    {
      printf("%d\n", this.x);
    }
  }
}

//alias RefCounted!(int) Foo;
alias S Foo;
Foo[int] map;

void main() {
  map[3] = Foo();
  printf("%d\n", map[3].x);
}

This code with version=bug produces garbage because of opAssign. It seems that opAssign is actually called before accessing map:

push   %rbp
mov    %rsp,%rbp
sub    $0x28,%rsp
push   %rbx
movabs $0x2a,%rax
mov    %rax,-0x10(%rbp)
mov    %rax,%rsi
lea    -0x18(%rbp),%rdi
callq  0x418c70 <_D3aux1S8opAssignMFS3aux1SZv>
mov    -0x18(%rbp),%rcx
mov    %rcx,-0x28(%rbp)
mov    $0x3,%edx
mov    %edx,-0x20(%rbp)
lea    -0x20(%rbp),%rcx
mov    $0x8,%dl
movabs $0x430db0,%rsi
mov    %fs:0x0,%rdi
add    0x21d2c1(%rip),%rdi        # 0x635fb0
callq  0x419048 <_aaGetX>


Reply via email to