Proper way to work around `Invalid memory operation`?

Matthias Klumpp via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Sep 25 09:07:12 PDT 2016


Hello!

I have a class similar to this one:
```
class Dummy
{

private:

     string tmpDir;

public:

     this (string fname)
     {
         tmpDir = buildPath ("/tmp", fname.baseName);
         std.file.mkdirRecurse (tmpDir);
     }

     ~this ()
     {
         close ();
     }

     void close ()
     {
         if (std.file.exists (tmpDir))
                 std.file.rmdirRecurse (tmpDir);
     }
}
```

When the GC calls the classes destructor, I get a
`core.exception.InvalidMemoryOperationError@/<...>/ldc/runtime/druntime/src/core/exception.d(693): Invalid memory operation`

Looks like rmdirRecurse tries to allocate with the GC, and the GC 
doesn't like that.
Is there any good way to get the temporary directory deletet 
automatically when the object is freed?
At time, I work around this bug by calling close() manually at 
the appropriate time, but this feel like a rather poor solution.

Cheers,
     Matthias




More information about the Digitalmars-d-learn mailing list