[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
st: Files left open in Mata
I am wondering how I can clean up if a Mata function fails after opening a
file. Here is some code to demonstrate the issue
// open a file
ofh = fopen("locked.txt","w")
// make a run time error
a = (1,2)
i = 3
// (never) close
Now screwup() will exit with error code 3301 "subscript invalid", which is
fine. The problem is that the file is still locked. Try
and Mata will return error 3621 "attempt to write read-only file". Asking
the OS (WinXP) to delete the file doesn't work either, because the file "is
being used by another person or program", unless I exit Stata.
This can be a problem while debugging. My workaround is to use file
locked2.txt, then locked3.txt, and so forth, until I get the code right, but
there must be a better way.
It would be nice if Mata could automatically close open files, and perhaps
release other resources, when it abends. The alternative would be a way to
handle exceptions. Try catch finally anyone?
* For searches and help try: