www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Array appending segfaults

reply Artur Skawina <art.08.09 gmail.com> writes:
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
Oct 29 2011
parent Jesse Phillips <jessekphillips+d gmail.com> writes:
On Sat, 29 Oct 2011 18:37:18 +0200, Artur Skawina wrote:

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

Not of much help, but I'm no reproducing it with DMD 2.056 Debian amd64.
Oct 29 2011