Timo Gransch via Digitalmars-d-learn píše v Čt 01. 01. 2015 v 16:14 +0100: > Hi, > > I have a class which unzips an archive into a temporary directory below the > system temp folder. I want to delete this temporary directory in the class's > destructor, but when I call rmdir there, I get an > > core.exception.InvalidMemoryOperationError@(0) > > The effect is not limited to this special case. Whenever I call rmdir in the > destructor, no matter for which directory, I get the same error. > > Environment: DMD v2.066.1 (from D-Apt) on Ubuntu 14.10 (32 and 64 bit). > > Sample code: > > // ------------------------------------------- > module main; > > import std.stdio; > import std.file; > import std.path; > > class RmdirTest > { > private string sTempDir; > > this(string sInstanceName) > { > writeln("Constructor called"); > sTempDir=tempDir() ~ dirSeparator ~ sInstanceName; > mkdir (sTempDir); > } > > ~this() > { > writeln("Destructor called"); > > if (sTempDir !is null) > { > rmdir(sTempDir); > } > } > } > > void main(string[] args) > { > RmdirTest rmDirTest=new RmdirTest("123"); > } > // ------------------------------------------- > > Console output is: > > Constructor called > Destructor called > core.exception.InvalidMemoryOperationError@(0) > > The directory "/tmp/123" is created, but not deleted. > > When I change the line "(sTempDir !is null)" to "((sTempDir !is null) && > (exists(sTempDir)) && (isDir(sTempDir)))", the exception is thrown already on > this line, so obviously the problem also applies to other file functions > like exists and isDir. > > Is there any solution for this? > > Thanks and best regards, > Timo You shoud not use destructor for this operation. In D there is no guarantee that class destructor will be called. So you can probablly use struct instead of class or add some method and called it explicitly when needed
Re: Call of rmdir in destructor causes InvalidMemoryOperationError
Daniel Kozak via Digitalmars-d-learn Thu, 01 Jan 2015 07:21:13 -0800
- Call of rmdir in destructor causes In... Timo Gransch via Digitalmars-d-learn
- Re: Call of rmdir in destructor ... Daniel Kozak via Digitalmars-d-learn
- Re: Call of rmdir in destructor ... thedeemon via Digitalmars-d-learn
- Re: Call of rmdir in destruc... Timo Gransch via Digitalmars-d-learn