Thread overview
Uninitialized object hangs without warning.
Nov 26, 2014
Bear Cherian
Nov 26, 2014
Meta
Nov 26, 2014
bearophile
Nov 26, 2014
Bear Cherian
November 26, 2014
I ran into this a while ago and have already moved on, but I had a class such as this

Class MyClass{

    this(){}

    void someFunction(){
        //body
    }

}

And in my app I had something like

    MyClass classObject;
    classObject.someFunction();

When I compile, no warnings or errors. But when I run the application it just hangs at the function call. Eventually the app kills itself.

The problem is classObject is never initialized. This drove me crazy for an entire evening before I added a bunch of debug lines to narrow it down and then visually saw what the problem was.

I feel there should be some type of warning or error when compiling, or an error when we get to classObject.someFunction().

This is on 2.065, so forgive me if this has already been addressed. Again, it's been a while since I actually encountered the error, so not sure if i missed an aspect of it.
November 26, 2014
On Wednesday, 26 November 2014 at 05:24:49 UTC, Bear Cherian wrote:
> I ran into this a while ago and have already moved on, but I had a class such as this
>
> Class MyClass{
>
>     this(){}
>
>     void someFunction(){
>         //body
>     }
>
> }
>
> And in my app I had something like
>
>     MyClass classObject;
>     classObject.someFunction();
>
> When I compile, no warnings or errors. But when I run the application it just hangs at the function call. Eventually the app kills itself.
>
> The problem is classObject is never initialized. This drove me crazy for an entire evening before I added a bunch of debug lines to narrow it down and then visually saw what the problem was.
>
> I feel there should be some type of warning or error when compiling, or an error when we get to classObject.someFunction().
>
> This is on 2.065, so forgive me if this has already been addressed. Again, it's been a while since I actually encountered the error, so not sure if i missed an aspect of it.

This *does* cause an error on 2.065: http://dpaste.dzfl.pl/0d6663890081

Or did you mistakenly type "Class" instead of "class"? In the above code, when you replace "Class" with "class", it runs and then segfaults. Either way, there is no hang.

Where did you install the compiler from? This is definitely a problem on your end.
November 26, 2014
Bear Cherian:

> Class MyClass{
>
>     this(){}
>
>     void someFunction(){
>         //body
>     }
>
> }
>
> And in my app I had something like
>
>     MyClass classObject;
>     classObject.someFunction();
>
> When I compile, no warnings or errors.

If you compile that code (with lowercase Class) with -O the compiler finds the bug:



class MyClass {
    this() {}

    void someFunction() {
        //body
    }
}

void main() {
    MyClass classObject;
    classObject.someFunction;
}


test.d(11,5): Error: null dereference in function _Dmain


But in more complex cases the compiler doesn't.

Bye,
bearophile
November 26, 2014
On Wednesday, 26 November 2014 at 09:38:11 UTC, bearophile wrote:
> Bear Cherian:
>
>> Class MyClass{
>>
>>    this(){}
>>
>>    void someFunction(){
>>        //body
>>    }
>>
>> }
>>
>> And in my app I had something like
>>
>>    MyClass classObject;
>>    classObject.someFunction();
>>
>> When I compile, no warnings or errors.
>
> If you compile that code (with lowercase Class) with -O the compiler finds the bug:
>
>
>
> class MyClass {
>     this() {}
>
>     void someFunction() {
>         //body
>     }
> }
>
> void main() {
>     MyClass classObject;
>     classObject.someFunction;
> }
>
>
> test.d(11,5): Error: null dereference in function _Dmain
>
>
> But in more complex cases the compiler doesn't.
>
> Bye,
> bearophile

Yes, the capital C was a typo.

I still think this shouldn't be an optimization. Maybe I'm just
used to Java, as this would be a compile error by default.