i've found a problem i really have no idea how to solve. in a Win32 pure API program, i declare a global FILE pointer (im not sure if global is the right term, but the code below will show what i mean). in WM_CREATE, i fopen() a file. in WM_COMMAND, when i try to use fprintf, it exits the program and gives me a second chance assertion failure, in fprintf.c line 56. the way it exits the program is also interesting. the window is destroyed, and a messagebox pops up saying, "User breakpoint called from code at 0xbff768a4". that makes me think this is more than just a simple error, because i dont have any breakpoints whatsoever, but the fprintf() call is causing one. and by the way, that address, "0xbff768a4", is always the same.
i orginally thought maybe fopen() stored some important information on the local stack, which then got popped when the Window Procedure function returned, but if that was the case, the fprintf() in WM_CLOSE wouldn't work.Code:#include <windows.h> #include <stdio.h> FILE *fp; LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { ... case WM_CREATE: if ( (fp=fopen("file.dat","wb"))==NULL) exit(1); ... case WM_COMMAND: fprintf(fp,"......"); //this line causes the error case WM_CLOSE: fprintf(fp,"Shutting down"); //but this one works fine fclose(fp); }
no, i just cant see any reason whatsoever why this is happening. someone out there must know what to do.