Jump to page: 1 2
Thread overview
Re: Why is a static struct's dtor called at the exit of a function?
Jul 20, 2011
Jonathan M Davis
Jul 21, 2011
Jonathan M Davis
Jul 21, 2011
Andrej Mitrovic
Jul 22, 2011
Diego Canuhé
Jul 22, 2011
Jacob Carlborg
Jul 22, 2011
Diego Canuhé
Jul 22, 2011
Jonathan M Davis
Jul 23, 2011
Andrej Mitrovic
Jul 22, 2011
Jonathan M Davis
Jul 23, 2011
Jonathan M Davis
Jul 23, 2011
Andrej Mitrovic
Jul 23, 2011
Jonathan M Davis
July 20, 2011
On Wednesday 20 July 2011 21:54:19 Andrej Mitrovic wrote:
> import std.stdio;
> 
> void main()
> {
>     writeln("test");
>     test();
>     writeln();
>     writeln("test");
>     test();
> }
> 
> struct Foo
> {
>     int state;
>     this (int i)
>     {
>         state = i;
>     }
> 
>     ~this()
>     {
>         writeln("dtor");
>     }
> }
> 
> void test()
> {
>     static Foo foo = Foo(1);
>     writeln(foo.state);
> }
> 
> Prints:
> test
> 1
> dtor
> 
> test
> 1
> dtor
> 
> Shouldn't static variables be left untouched? I tried to use a struct to handle an external device context, however the dtor would be kept called between function calls, so this caused crashes for me.
> 
> Using a class instead fixes my issue of course.
> 
> I thought "static" for variables implied it has lifetime until the end of the application, regardless of whether the memory for the variable was allocated on the heap or is in the .data segment or somewhere else.

static variables are supposed to have essentially the same lifetime as the program. So, it definitely looks like a bug.

- Jonathan M Davis
July 21, 2011
On Wed, 20 Jul 2011 15:57:44 -0400, Jonathan M Davis <jmdavisProg@gmx.com> wrote:

> On Wednesday 20 July 2011 21:54:19 Andrej Mitrovic wrote:
>> import std.stdio;
>>
>> void main()
>> {
>>     writeln("test");
>>     test();
>>     writeln();
>>     writeln("test");
>>     test();
>> }
>>
>> struct Foo
>> {
>>     int state;
>>     this (int i)
>>     {
>>         state = i;
>>     }
>>
>>     ~this()
>>     {
>>         writeln("dtor");
>>     }
>> }
>>
>> void test()
>> {
>>     static Foo foo = Foo(1);
>>     writeln(foo.state);
>> }
>>
>> Prints:
>> test
>> 1
>> dtor
>>
>> test
>> 1
>> dtor
>>
>> Shouldn't static variables be left untouched? I tried to use a struct
>> to handle an external device context, however the dtor would be kept
>> called between function calls, so this caused crashes for me.
>>
>> Using a class instead fixes my issue of course.
>>
>> I thought "static" for variables implied it has lifetime until the end
>> of the application, regardless of whether the memory for the variable
>> was allocated on the heap or is in the .data segment or somewhere
>> else.
>
> static variables are supposed to have essentially the same lifetime as the
> program. So, it definitely looks like a bug.

slight correction -- static variables are thread local (must be marked shared to be shared between threads), so the destructor should be called on Thread destruction.

However, I don't think that any destructors are called on thread destruction...

But yes, they should *definitely* not be called on function exit.  This is a bug.

-Steve
July 21, 2011
On Thursday 21 July 2011 11:52:58 Steven Schveighoffer wrote:
> On Wed, 20 Jul 2011 15:57:44 -0400, Jonathan M Davis <jmdavisProg@gmx.com>
> 
> wrote:
> > On Wednesday 20 July 2011 21:54:19 Andrej Mitrovic wrote:
> >> import std.stdio;
> >> 
> >> void main()
> >> {
> >> 
> >>     writeln("test");
> >>     test();
> >>     writeln();
> >>     writeln("test");
> >>     test();
> >> 
> >> }
> >> 
> >> struct Foo
> >> {
> >> 
> >>     int state;
> >>     this (int i)
> >>     {
> >> 
> >>         state = i;
> >> 
> >>     }
> >> 
> >>     ~this()
> >>     {
> >> 
> >>         writeln("dtor");
> >> 
> >>     }
> >> 
> >> }
> >> 
> >> void test()
> >> {
> >> 
> >>     static Foo foo = Foo(1);
> >>     writeln(foo.state);
> >> 
> >> }
> >> 
> >> Prints:
> >> test
> >> 1
> >> dtor
> >> 
> >> test
> >> 1
> >> dtor
> >> 
> >> Shouldn't static variables be left untouched? I tried to use a struct to handle an external device context, however the dtor would be kept called between function calls, so this caused crashes for me.
> >> 
> >> Using a class instead fixes my issue of course.
> >> 
> >> I thought "static" for variables implied it has lifetime until the end of the application, regardless of whether the memory for the variable was allocated on the heap or is in the .data segment or somewhere else.
> > 
> > static variables are supposed to have essentially the same lifetime as
> > the
> > program. So, it definitely looks like a bug.
> 
> slight correction -- static variables are thread local (must be marked shared to be shared between threads), so the destructor should be called on Thread destruction.

Ah, yes. There is that slight change in D from C++.

> However, I don't think that any destructors are called on thread destruction...

That doesn't sound good.

- Jonathan M Davis
July 21, 2011
On Thu, 21 Jul 2011 12:39:27 -0400, Jonathan M Davis <jmdavisProg@gmx.com> wrote:

>> However, I don't think that any destructors are called on thread
>> destruction...
>
> That doesn't sound good.

I may be wrong, a simple test should suffice.

-Steve
July 21, 2011
On 7/21/11, Steven Schveighoffer <schveiguy@yahoo.com> wrote:
> On Wed, 20 Jul 2011 15:57:44 -0400, Jonathan M Davis <jmdavisProg@gmx.com> wrote:
>
>> On Wednesday 20 July 2011 21:54:19 Andrej Mitrovic wrote:
>>> import std.stdio;
>>>
>>> void main()
>>> {
>>>     writeln("test");
>>>     test();
>>>     writeln();
>>>     writeln("test");
>>>     test();
>>> }
>>>
>>> struct Foo
>>> {
>>>     int state;
>>>     this (int i)
>>>     {
>>>         state = i;
>>>     }
>>>
>>>     ~this()
>>>     {
>>>         writeln("dtor");
>>>     }
>>> }
>>>
>>> void test()
>>> {
>>>     static Foo foo = Foo(1);
>>>     writeln(foo.state);
>>> }
>>>
>>> Prints:
>>> test
>>> 1
>>> dtor
>>>
>>> test
>>> 1
>>> dtor
>>>
>>> Shouldn't static variables be left untouched? I tried to use a struct to handle an external device context, however the dtor would be kept called between function calls, so this caused crashes for me.
>>>
>>> Using a class instead fixes my issue of course.
>>>
>>> I thought "static" for variables implied it has lifetime until the end of the application, regardless of whether the memory for the variable was allocated on the heap or is in the .data segment or somewhere else.
>>
>> static variables are supposed to have essentially the same lifetime as
>> the
>> program. So, it definitely looks like a bug.
>
> slight correction -- static variables are thread local (must be marked shared to be shared between threads), so the destructor should be called on Thread destruction.
>
> However, I don't think that any destructors are called on thread destruction...
>
> But yes, they should *definitely* not be called on function exit.  This is a bug.
>
> -Steve
>

Is this a known bug? I can't find anything about this in bugzilla. On a related note, I had a dtor of a class not being called even with explicit call to clear() or the older delete statement, and it wasn't even called after application exit. But I have to make a minimal test case first.
July 22, 2011
Hi,
I'm not really an experienced programmer so I might be missing something
but...

in
http://d-programming-language.org/attribute.html
static is described as follows:

"The static attribute applies to functions and data. It means that the declaration does not apply to a particular instance of an object, but to the type of the object. In other words, it means there is no this reference. static is ignored when applied to other declarations."

What I understand from that text is that static only applies to functions
and data *of a class
*
As far as I'm concerned static only makes a method non-virtal and a variable
a class-variable.
(static if has nothing to do with this static. I think)


If I just talked nonsense out of my ignorance, I apologize
Static data has only one instance for the entire program, not once per objec


July 22, 2011
On Friday 22 July 2011 03:12:00 Diego Canuhé wrote:
> Hi,
> I'm not really an experienced programmer so I might be missing something
> but...
> 
> in
> http://d-programming-language.org/attribute.html
> static is described as follows:
> 
> "The static attribute applies to functions and data. It means that the declaration does not apply to a particular instance of an object, but to the type of the object. In other words, it means there is no this reference. static is ignored when applied to other declarations."
> 
> What I understand from that text is that static only applies to functions
> and data *of a class
> *
> As far as I'm concerned static only makes a method non-virtal and a variable
> a class-variable.
> (static if has nothing to do with this static. I think)
> 
> 
> If I just talked nonsense out of my ignorance, I apologize
> Static data has only one instance for the entire program, not once per objec

static is used for a number of things. In D, it does one of several things:

1. static on a function on a class or struct makes it so that that the function has no this pointer/reference, and so you can use it without an instance of that object.

2. static on a variable makes it so that the variable exists for the duration of the thread that it's on (or for the duration of the program if it's shared). So, if you declare a variable static inside of a function, then the variable will be unchanged between calls to that function. e.g.

int func()
{
    static int i = 0;
    return i++;
}

func will return a value one greater each time that it's called, since is created when the thread is created and it stays around until the thread is gone, whereas if it weren't static it would be recreated each time that func was called.

3. static on a nested function makes it so that it's a function instead of a delegate (that is, it has no access to the function that it's in).

4. A nested struct or class which is _not_ static is tied to the instance of an object of its outer class/struct and has access to that object's variables, whereas a nested struct or class which _is_ static is not tied to any particular instance of its outer class/struct. There can be multiple of them; they do not have direct access to their outer class/struct (since they're not tied to a particular instance); and they can even be shared between instances of their outer class/struct. They're not tied to their outer class/struct at all and act more like normal structs or classes.

5. static on a class or struct constructor means that it applies to the class as a whole rather than a particular instance of that class or struct, and it is run when the thread is initialized. The same goes for static class or struct destructors except that they're run when the thread is shut down.

6. static goes on a constructors and destructors at module scope, and they are then module constructors. They're run when a thread is created and destroyed respectively.

7. static on an if makes it so that the if is run at compile time rather than runtime. e.g.

auto func(T)(T val)
{
    static if(Unsigned!T)
        return val * 2;
    else
        return val;
}

If func is used with an unsigned integral value, then it returns the original value * 2. Otherwise, it returns the original value.

8. static on an import makes it so that you must explicitly qualify the function every time that you use it. e.g.

static import algorithm;
auto found = std.algorithm.find(range, 7);

Just using find doesn't work, because the import was static.


Okay. I _think_ that that's all of them, but I might have missed one. But as you can see, static is used for a lot of different things. It all depends on the context.

- Jonathan M Davis
July 22, 2011
On 2011-07-22 08:12, Diego Canuhé wrote:
> Hi,
> I'm not really an experienced programmer so I might be missing something
> but...
>
> in
> http://d-programming-language.org/attribute.html
> static is described as follows:
>
> "The static attribute applies to functions and data. It means that the
> declaration does not apply to a particular instance of an object, but to
> the type of the object. In other words, it means there is no this
> reference. static is ignored when applied to other declarations."
>
> What I understand from that text is that static only applies to
> functions and data *of a class
> *
> As far as I'm concerned static only makes a method non-virtal and a
> variable a class-variable.
> (static if has nothing to do with this static. I think)
>
>
> If I just talked nonsense out of my ignorance, I apologize
> Static data has only one instance for the entire program, not once per
> objec

static can be applied to data as well, also in global functions:

import std.stdio;

void foo ()
{
    static int i;
    writeln(i++);
}

void main ()
{
    foo();
    foo();
}

Will print

0
1

-- 
/Jacob Carlborg
July 22, 2011
thanks ;)


July 22, 2011
On Fri, 22 Jul 2011 02:57:38 -0400, Diego Canuhé <canuhedc@gmail.com> wrote:

> thanks ;)

Don't feel bad, static is probably the most abused keyword in D and C++.  It has about 3 or 4 meanings depending on context.

-Steve
« First   ‹ Prev
1 2