Proper way to work around `Invalid memory operation`?

Kagamin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Sep 29 03:17:21 PDT 2016


On Sunday, 25 September 2016 at 16:07:12 UTC, Matthias Klumpp 
wrote:
> 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);
>     }
> }
> ```

Another problem here is that tmpDir is GC-allocated, but by the 
time the constructor is called the string can be already 
collected, you can't read it. A possible solution would be to 
have a service which you would notify to delete the folder.


More information about the Digitalmars-d-learn mailing list