Using emplace leading to memory corruption

d coder dlang.coder at gmail.com
Sat Sep 28 23:37:42 PDT 2013


Greetings

I am trying to use emplace and it is seemingly leading to memory
corruption. Before I file a bug on DMD Buzilla, I want to find out if I am
doing something wrong or if it is a known issue. I am using a linux box and
this testcase is freaky -- if I comment out either line 20 or line 24, the
segfault vanishes.

Thanks
- Puneet

$ rdmd --force -version=EMPLACE -Isrc -J.. test.d
4000
Segmentation fault (core dumped)
$ rdmd --force -Isrc -J.. test.d
4000

struct bdd {}                                              // 01
class Frop {                                               // 02
  int[] _var;                                              // 03
  int[] var() {                                            // 04
    return _var;                                           // 05
  }                                                        // 06
  this() {                                                 // 07
    this._var.length = 1;                                  // 08
  }                                                        // 09
}                                                          // 10
class Foo {                                                // 11
  long[] nodes;                                            // 12
  Frop[] frops;                                            // 13
  long[] table;                                            // 14
  this() {                                                 // 15
    nodes.length = 120000;                                 // 16
    frops.length = 1;                                      // 17
    frops[0] = new Frop();                                 // 18
    initTable();                                           // 19
    zoo(frops[0].var);                                     // 20
  }                                                        // 21
  void initTable() {                                       // 22
    import std.stdio;                                      // 23
    writeln(4000);                                         // 24
    table.length = 40000;                                  // 25
  }                                                        // 26
  void zoo(int[] varset) {}                                // 27
}                                                          // 28
class Bar {                                                // 29
  Foo _foo;                                                // 30
  this() {                                                 // 31
    version(EMPLACE) {                                     // 32
      import std.conv, core.stdc.stdlib;                   // 33
      enum size_t size = __traits(classInstanceSize, Foo); // 34
      assert(size is 32);                                  // 35
      void* tmp = core.stdc.stdlib.malloc(size);           // 36
      if (!tmp)                                            // 37
        throw new Exception("Memory allocation failed");   // 38
      void[] mem = tmp[0..size];                           // 39
      _foo = emplace!(Foo)(mem);                           // 40
    }                                                      // 41
    else {                                                 // 42
      _foo = new Foo();                                    // 43
    }                                                      // 44
  }                                                        // 45
}                                                          // 46
void main() {                                              // 47
  auto bar = new Bar;                                      // 48
}                                                          // 49
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20130929/bc277610/attachment-0001.html>


More information about the Digitalmars-d mailing list