www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - emplace bug?


    When using a emplace on a struct the emplace function will call the 
destructor of the struct it seems. When using structs that has a 
desctructor with RefCounted this makes it have an unintuitive behavior.

This is unfortunate if you want to use RefCounted on a struct for RAII e.g:

struct Resource {
    ~this() {
    int fd;

void main(..) {
    RefCounted!Resource r;

    // initialize() will call emplace() which will do a
    // *result = T(args);
    // result is a pointer to a malloced Resource struct
    // When *result is assigned to opAssign is called.

According to the language docs opAssign will call the destructor of a 
tmp struct.


S* opAssign(S s) {
    ... bitcopy *this into tmp ...
    ... bitcopy s into *this ...
    ... call destructor on tmp ...
    return this;

This will call close(fd) on the tmp struct where fd is 0!

Would it be possible for all branches in emplace to use the embedded 
initialize() helper function to get around this problem somehow?

Nov 05 2011