Author: Russell Reagan
Date: 14:10:07 07/24/03
Go up one level in this thread
On July 24, 2003 at 15:06:41, Matthias Gemuh wrote: >Hi Russel, Hi Matthias, >you miss the point a bit :) I think you do too, a bit :) >I was talking of situations where your engine >crashes but the open files are those of unrelated utilities, caught unawares. >Asserting in your engine will not close the files of the utilities before the >delayed crash. You can write your own assert statements and call your own "cleanup" function on a failed assertion. I'm probably skipping over some details, but basically it works like this. #define Assert(expr) ((expr) || CleanupFunction()) void CleanupFunction () { WriteCurrentGameDataToFile(); CloseLogFile(); WhateverElseYouWantToSave(); } If the first part of the OR expression in your assert macro is "true", then the second part won't be called. If the first part is false, the second part will be called. This way you *could* close your files or save anything you wanted to ensure that nothing gets lost. What I was saying is that this method only works if you put your special Assert() in the right places. If you knew what the "right places" were you could probably find the bug though, so that's why I said I wasn't sure how useful this technique would be for your situation. A couple of other things you could try are to call fflush() (or the equivalent iostream method) after each file write you do. This way the data should be written to disk immediately and won't get left in a buffer. Another way to solve this is to disable buffering, probably with setbuf(file_name, 0); That should make it so that everytime you do something like fprintf(...), the data will not sit in a buffer, but will be directly written to disk. Using either of these approaches, if the program crashes and you don't get a chance to fclose() the file, the data should still be saved in your file. Another option I can think of is to use exceptions and catch any unhandled ones at the top level, then call a cleanup function and close all of your files and save whatever you want. Maybe like this. void main () { try { RunProgram(); } catch (...) { printf("there was an error\n"); printf("saving your current game\n"); CleanupFunction(); } } Like the Assert() approach, this might not work very well in your case since you don't know where the bug might be.
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.