another compiler bug?

Marcin Kuszczak aarti_please_no at spam_interia.pl
Sat Jan 12 08:14:09 PST 2008


Frits van Bommel wrote:

>>>> although it will cause memory leaks for every type for which the
>>>> function is instantiated. So I consider bug in DMD as quite serious...
>>>
>>> That will only allocate one reference per type (*not* per call to the
>>> function). Try this workaround instead:
>> 
>> That's exactly what I said :-)
> 
> No, you say it will allocate a reference per instantiation and refer to
> this as a memory leak (presumably because they're not gc-ed after
> they're no longer in use).

Yes, I probably should say "memory leak" (singular) to express what I meant.

> My point was that a second call to the function (for the same type) will
> overwrite the location returned from the first call.

That leads me to discuss another bug which I discovered. 

Let's say we have program like below:
--------
import std.stdio;
struct Pattern(T : bool) {
    string beg = "test";
}

Pattern!(T) get(T)() {
    static Pattern!(T) pattern;
    return pattern;
}

void main() {
    writefln(get!(bool).beg);
    get!(bool).beg = "other";
    writefln(get!(bool).beg);
}
--------

Above program prints:
test
test

although I would expect it will print:
test
other

It seems that memory reserved for struct in template function is initialized
every time the function is executed.

This behaviour is different when we change Pattern to be class instead of
struct. Then it is possible to create kind of singleton pattern for every
type.

What do you think?

BTW. Where should I discuss bugs? I have another dozen of them and would
like to discuss some of them before posting to bugzilla. Is it better to
post to d.learn or d.bugs?

-- 
Regards
Marcin Kuszczak (Aarti_pl)
-------------------------------------
Ask me why I believe in Jesus - http://www.zapytajmnie.com (en/pl)
Doost (port of few Boost libraries) - http://www.dsource.org/projects/doost/
-------------------------------------



More information about the Digitalmars-d-learn mailing list