digitalmars.D.learn - 2 File IO Questions (Phobos)
- Zane (44/44) Nov 07 2009 Hello all,
- Zane (4/65) Nov 07 2009 Doh! I still need help with number 2, but for number 1, all I needed was...
- Frank Benoit (7/13) Nov 07 2009 desctructors are called in not defined order. The GC puts your class
- Zane (2/18) Nov 07 2009 I understand, thanks very much!
Hello all, I have been looking at Phobos lately, and currently I am having some trouble understanding what is going on with the following 2 trivial examples. First of all, I am using dmd v1.050. bytes come from? (1) ----------------------------------------------------------------> import std.stream; import std.stdio; int main() { char[] stuff = "This is some stuff!"; File f = new File("stuff.txt", FileMode.Out | FileMode.In); f.write(stuff); f.seekSet(0); stuff = f.readLine(); writef("[%s]", stuff); f.close; return 0; } This one I wanted to have a class open a file upon initialization of an instance, and close a file when the destructor is called. I get an "Error: Access Violation" unless I comment out the file.close line. Why? (of course this example also has the same problem as the first example, but I kept the first one simpler to narrow down things) (2) ----------------------------------------------------------------> import std.stream; public class StuffWriter { File file; this(char[] filename) { file = new File(filename, FileMode.Out); } ~this() { file.close; //this causes an access violation??? } public void write(char[] stuff) { file.write(stuff); } } int main() { StuffWriter sw = new StuffWriter("stuff.txt"); sw.write("This is some stuff!"); return 0; } Thanks! Zane
Nov 07 2009
Zane Wrote:Hello all, I have been looking at Phobos lately, and currently I am having some trouble understanding what is going on with the following 2 trivial examples. First of all, I am using dmd v1.050. bytes come from? (1) ----------------------------------------------------------------> import std.stream; import std.stdio; int main() { char[] stuff = "This is some stuff!"; File f = new File("stuff.txt", FileMode.Out | FileMode.In); f.write(stuff); f.seekSet(0); stuff = f.readLine(); writef("[%s]", stuff); f.close; return 0; } This one I wanted to have a class open a file upon initialization of an instance, and close a file when the destructor is called. I get an "Error: Access Violation" unless I comment out the file.close line. Why? (of course this example also has the same problem as the first example, but I kept the first one simpler to narrow down things) (2) ----------------------------------------------------------------> import std.stream; public class StuffWriter { File file; this(char[] filename) { file = new File(filename, FileMode.Out); } ~this() { file.close; //this causes an access violation??? } public void write(char[] stuff) { file.write(stuff); } } int main() { StuffWriter sw = new StuffWriter("stuff.txt"); sw.write("This is some stuff!"); return 0; } Thanks! ZaneDoh! I still need help with number 2, but for number 1, all I needed was to use 'writeString' instead of 'write'. Like I said, still getting used to Phobos. Thanks, Zane
Nov 07 2009
Zane schrieb:Doh! I still need help with number 2, but for number 1, all I needed was to use 'writeString' instead of 'write'. Like I said, still getting used to Phobos. Like I said, I still need help on the Thanks, Zanedesctructors are called in not defined order. The GC puts your class instance 'sw' and the File instance 'sw.file' onto the list for destruction. So the 'sw.file' might be destructed before 'sw' is. Deferencing sw.file from withing sw.~this then fails. A general rule: in a dtor, never dereference member variables. You have to rely on the File dtor, to close himself if still open.
Nov 07 2009
Frank Benoit Wrote:Zane schrieb:I understand, thanks very much!Doh! I still need help with number 2, but for number 1, all I needed was to use 'writeString' instead of 'write'. Like I said, still getting used to Phobos. Like I said, I still need help on the Thanks, Zanedesctructors are called in not defined order. The GC puts your class instance 'sw' and the File instance 'sw.file' onto the list for destruction. So the 'sw.file' might be destructed before 'sw' is. Deferencing sw.file from withing sw.~this then fails. A general rule: in a dtor, never dereference member variables. You have to rely on the File dtor, to close himself if still open.
Nov 07 2009