struct dynamic allocation error

Dechcaudron via Digitalmars-d digitalmars-d at puremagic.com
Thu Sep 15 13:38:45 PDT 2016


I believe there is some kind of weird issue that won't allow for 
struct instances to be dynamically allocated in a proper way via 
the 'new' keyword. It does actually allocate them and return a 
valid pointer to operate the instances, but whenever the program 
is exited I get the following exception:

core.exception.InvalidMemoryOperationError at src/core/exception.d(693): Invalid memory operation

Calling 'destroy' on the returned pointer only seems to set it to 
null, but it definitely doesn't call the destructor, neither does 
it prevent said exception from being raised. Code to reproduce:

```
import std.conv;
import std.stdio;

struct Foo
{
     int a;

     this(int a)
     {
         this.a = a;
     }

     ~this()
     {
         writeln("a is " ~ to!string(a));
     }
}

void main()
{
     Foo a = Foo(5);
     Foo* b = new Foo(10);
     writeln("Allocation complete");
     destroy(b); //Does nothing
     //Destructor for a is called
}
```


More information about the Digitalmars-d mailing list