lifetime of dynamically allocated memory

dave garapata at gmail.com
Mon May 31 14:59:44 PDT 2010


== Quote from torhu (no at spam.invalid)'s article
> On 31.05.2010 22:27, dave wrote:
> > I'm trying to figure out if a dynamically allocated memory in D is getting
> > collected with this simple test (using tango):
> >
> > class Foo {
> >      ~this() {
> >          // destructor
> >      }
> > }
> >
> > struct Item {
> >      Foo a;
> > }
> >
> > Item[] items;
> > items.length = 1;
> > items[0] = Item();
> > items[0].a = new Foo();
> >
> > items = null;
> >
> > GC.collect();
> >
> > I assume that calling GC.collect(); forces gc to go through and free up memory
> > for any unreferenced objects. It seems that the destructor for Foo isn't being
> > called. I'm not sure if the reference is still used somewhere or do I have to
> > explicitly call 'delete items[0].a' to ensure that the memory is freed?
> I'm not sure why it doesn't work.  But it's always better to post the
> code you're actually using, since now we can't be sure that you're not
> doing something else then what you think you're doing.  If you know what
> I mean. :)

I was trying it out on a clean slate...then I realized it works as intended, see
attach file.

Then I was trying to figure out why it didn't work when I was testing it before
and realized that I defined both the class and the struct within the scope of the
function (attachment 2). Seems kinda weird that it wouldn't work that way...
begin 644 hello.d
M;6]D=6QE(&UA:6X[#0H-"FEM<&]R="!T86YG;RYI;RY3=&1O=70L('1A;F=O
M+F-O<F4N365M;W)Y.PT*#0IC;&%S<R!&;V\@>PT*("`@('YT:&ES*"D@>PT*
M("`@("`@("!S=&1O=70H(D9O;R!D97-T<G5C=&]R(BD[#0H@("`@?0T*?0T*
M#0IS=')U8W0 at 271E;2![#0H@("`@1F]O(&$[#0I]#0H-"FEN="!M86EN*&-H
M87);75M=(&%R9W,I#0I[#0H@("`@271E;5M=(&ET96US.PT*("`@(&ET96US
M+FQE;F=T:"`](#$[#0H@("`@:71E;7-;,%T@/2!)=&5M*"D[#0H@("`@:71E
M;7-;,%TN82`](&YE=R!&;V\H*3L-"@T*("`@(&ET96US(#T@;G5L;#L-"@T*
M("`@($=#+F-O;&QE8W0H*3L-"@T*("`@('-T9&]U="@B7&YA9G1E<B!C;VQL
M96-T+B(I.PT*#0H)<F5T=7)N(#`[#0I]#0H-"G-T871I8R!^=&AI<R at I('L-
I"B`@("!S=&1O=70H(EQN;6]D=6QE(&1E<W1R=6-T;W(N(BD[#0I]#0H`
`
end
begin 644 hello2.d
M;6]D=6QE(&UA:6X[#0H-"FEM<&]R="!T86YG;RYI;RY3=&1O=70L('1A;F=O
M+F-O<F4N365M;W)Y.PT*#0II;G0@;6%I;BAC:&%R6UU;72!A<F=S*0T*>PT*
M("`@(&-L87-S($9O;R![#0H@("`@("`@('YT:&ES*"D@>PT*("`@("`@("`@
M("`@<W1D;W5T*")&;V\@9&5S=')U8W1O<B(I.PT*("`@("`@("!]#0H@("`@
M?0T*#0H@("`@<W1R=6-T($ET96T@>PT*("`@("`@("!&;V\@83L-"B`@("!]
M#0H-"B`@("!)=&5M6UT@:71E;7,[#0H@("`@:71E;7,N;&5N9W1H(#T@,3L-
M"B`@("!I=&5M<ULP72`]($ET96TH*3L-"B`@("!I=&5M<ULP72YA(#T@;F5W
M($9O;R at I.PT*#0H@("`@:71E;7,@/2!N=6QL.PT*#0H@("`@1T,N8V]L;&5C
M="@I.PT*#0H@("`@<W1D;W5T*")<;F%F=&5R(&-O;&QE8W0N(BD[#0H-"@ER
M971U<FX@,#L-"GT-"@T*<W1A=&EC('YT:&ES*"D@>PT*("`@('-T9&]U="@B
<7&YM;V1U;&4 at 9&5S=')U8W1O<BXB*3L-"GT-"@``
`
end


More information about the Digitalmars-d-learn mailing list