Array appending segfaults

Artur Skawina art.08.09 at gmail.com
Sat Oct 29 09:37:18 PDT 2011


Tried D today for the very first time, and the first program started segfaulting
soon enough... The simplified version is this:

-----
import core.thread;
import std.stdio;

class MyThread : Thread {
   this() {
      super(&run);
   }

   private:
   string s = "abcdefg";
   
   void run() {
      writef("New thread starting.\n");
      
      while (s.length < 300_000_000)
         s ~= s[0..4];
      
      writef("New thread done.\n");
   }
}

void main() {
   for( int n=0 ; n<8 ; n++ ) {
      Thread t = new MyThread();
      t.start();
   }
   
   writef("Main thread done.\n");
}
-----

which often (>50%) results in:

> New thread starting.
> Main thread done.
> New thread starting.
> New thread starting.
> New thread starting.
> New thread starting.
> New thread starting.
> New thread starting.
> New thread starting.
> Segmentation fault (core dumped)


> Program terminated with signal 11, Segmentation fault.
> #0  0x0000000000416019 in _d_arrayappendcTp (ti=..., px=..., n=<optimized out>)
>     at ../../../libphobos/rt/lifetime.d:1739
> 1739	                if(*(cast(size_t*)info.base) == size + offset)
> (gdb) bt full
> #0  0x0000000000416019 in _d_arrayappendcTp (ti=..., px=..., n=<optimized out>)
>     at ../../../libphobos/rt/lifetime.d:1739
>         newdata = <optimized out>
>         newcap = 78018
>         newsize = 48159
>         length = <optimized out>
>         info = {base = 0x0, size = 49152, attr = 10}
>         sizeelem = 1
>         offset = <optimized out>
>         size = 48155
>         newlength = 48159
>         bic = <optimized out>
>         isshared = false
> #1  0x000000000041652b in _d_arrayappendT (ti=..., x=..., y=...)
>     at ../../../libphobos/rt/lifetime.d:1578
>         sizeelem = <optimized out>
>         length = 48155
> #2  0x0000000000404522 in _D12bugstrthsegf8MyThread3runMFZv (this=...) at bugstrthsegf.d:17
> No locals.
> #3  0x000000000041ec07 in run (this=<optimized out>) at ../../../libphobos/core/thread.d:1405
> No locals.

So it dies while trying to access info.base which is null.
Nothing short of adding explicit locking around the append op seems to help...

gcc version 4.6.2 20111024 (prerelease gdc 0.30, using dmd 2.055) (GCC)
Target: x86_64-unknown-linux-gnu


artur


More information about the Digitalmars-d-learn mailing list