Thread overview
Segmentation fault
Dec 23, 2012
Shadow_exe
Dec 23, 2012
Maxim Fomin
Dec 23, 2012
Michael
December 23, 2012
There is an error, which I can not solve, because I can't understand why it happens.
If it's not an error, correct me please!

Code:
module main;

import core.thread;
import std.stdio;
import core.memory;

class DerivedThread : Thread {
    this(uint index){
        this._index = index;
        super( &run );
    }

    private uint _index;


    private {
        void run(){
                writeln("run ", _index);
                uint[] test11;
                for(uint y=0; y<1e6; ++y){
                    test11 ~= y;
                }
                writeln("done ", _index);
        }
    }
}

void main(string[] argv)
{
    for(uint i=0; i<10; ++i){
        (new DerivedThread(i)).start();
    }
}

compil:
dmd ./main.d -debug -profile -w -v -g -cov -gs
// version   v2.060

Compiles without a single warning, well, that's understandable, code there simple...
But when I run! Sometimes displays like this:
./main
run 3
run 1
run 0
run 2
run 4
run 5
run 6
run 7
run 8
run 9
done 0
done 8
done 3
done 1
done 2
done 4
done 6
done 5
done 7
done 9
// And everything is fine!
But sometimes:
./main
Segmentation fault
Or is it:
./main
run 0
Segmentation fault

If You after the launch of You have worked without errors, pozapuskayte several times.
What am I doing wrong? Help.
December 23, 2012
On Sunday, 23 December 2012 at 09:54:18 UTC, Shadow_exe wrote:
> There is an error, which I can not solve, because I can't understand why it happens.
> If it's not an error, correct me please!
>
> Code:
> module main;
>
> import core.thread;
> import std.stdio;
> import core.memory;
>
> class DerivedThread : Thread {
>     this(uint index){
>         this._index = index;
>         super( &run );
>     }
>
>     private uint _index;
>
>
>     private {
>         void run(){
>                 writeln("run ", _index);
>                 uint[] test11;
>                 for(uint y=0; y<1e6; ++y){
>                     test11 ~= y;
>                 }
>                 writeln("done ", _index);
>         }
>     }
> }
>
> void main(string[] argv)
> {
>     for(uint i=0; i<10; ++i){
>         (new DerivedThread(i)).start();
>     }
> }
>
> <skipped>

The problem is in heap corruption detected by valgrind in gc.gc.mark() and some other functions. I remember similar issue with using std.format functions in a loop. Increasing iterations from 7000 up to 7500 lead to heap corruption.

The issue can be reduced to :

import std.stdio;

void main()
{
	uint[] test;
	for(uint y=0; y<1e6; ++y){
		test ~= y;
	}
	writeln(test.length);
}


> If You after the launch of You have worked without errors, pozapuskayte several times.

He meant rerun multiple times here.
December 23, 2012
> dmd ./main.d -debug -profile -w -v -g -cov -gs
> // version   v2.060
Reproducible.


> dmd ./main.d -release -noboundscheck -O
> // version   v2.060

Irreproducible.

Win 8 x64, dmd x32.