object.Exception at std/stdio.d(1321): Enforcement failed - I must be doing something wrong?

Ali Çehreli acehreli at yahoo.com
Thu Aug 15 10:33:10 PDT 2013


On 08/15/2013 10:03 AM, Colin Grogan wrote:

 > I've done this a million times (I thought!) but I'm getting a strange
 > error I cant figure out.
 >
 > The code:
 >
 > void writeMsg(string msg){
 >        logFile.writeln(msg);

What is logFile?

 > }
 >
 > is failing with this error:
 >
 > object.Exception at std/stdio.d(1321): Enforcement failed
 > ----------------
 > ./tester(pure @safe bool std.exception.enforce!(bool).enforce(bool, lazy
 > const(char)[], immutable(char)[], ulong)+0x6b) [0x49a343]
 > ./tester(ref std.stdio.File.LockingTextWriter
 > std.stdio.File.LockingTextWriter.__ctor(ref std.stdio.File)+0x59)
 > [0x4ce019]

...

 > Also, I'm creating the logFile variable as follows:
 > File logFile = File(fileName, "w");

That looks like a local variable. How does writeMsg see it?

 > If ye need the code around it I'll be glad to post it

It is very helpful if you can reproduce it in a minimal example. The 
following program works with v2.064-devel-52cc287:

import std.stdio;

enum fileName = "deneme.txt";
File logFile;

static this() {
     logFile = File(fileName, "w");
}

void writeMsg(string msg){
       logFile.writeln(msg);
}

void main()
{
     writeMsg("hello");
     writeMsg("world");
}

Can you add to it and see when the problem appears.

Ali



More information about the Digitalmars-d-learn mailing list