GC interpreting integer values as pointers

%u e at ee.com
Mon Oct 11 10:15:57 PDT 2010


== Quote from Ivo Kasiuk (i.kasiuk at gmx.de)'s article
> Hi!
~snip
> ----------------------------------------
> This writes:
> new uint
> no reference
> ========== reference, f7490e20, f7490e10, f7490df0, f74
> 90dd0
> AA
> struct
> uint
> reference
> So in most but not all situations the integer value keeps the object
> from getting finalised. This observation corresponds to the effects I
> saw in my programs.
> I find this rather unfortunate. Is this known, documented behaviour? In
> a typical program there are such integer values all over the place. How
> should such values be stored to avoid unwanted interaction with the GC?
> Thanks,
> Ivo

In D1:
import std.stdio;
import std.gc;

class C {
  string s;
  this(string s) { this.s=s; }
  ~this() { writefln(s); }
}

class X {
  C c;
  uint r;
  uint[int] a;
  uint* p;

  this()
  {
    c = new C("reference");
    new C("no reference");
    r = cast(uint) cast(void*) new C("uint");
    a[0] = cast(uint) cast(void*) new C("AA");
    p = new uint;
    *p = (cast(uint) cast(void*) new C("new uint"));
  }
}

void main(string[] args) {
  X x = new X;
  std.gc.fullCollect();
  writefln("========== %s, %x, %x, %x", x.c.s, x.r, x.a[0],*x.p);
}

Writes:
no reference
========== reference, ad3fd0, ad3fb0, ad3f90
new uint  << ;)
AA
uint
reference


More information about the Digitalmars-d-learn mailing list