Sign in to follow this  

why does fclose() throw an exception

This topic is 4814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I added a function to read a file, but whether I used it or not, fclose() keeps throwing an exception, and it only does that in Debug mode. If I used it (open a file), opening and reading data from it is fine, and I close it right after I am done, and nothing goes wrong. But as soon as I close the program (even though there is no open file and I don't call fclose), fclose() still throws an exception. The strange thing is, if I comment that function, everything is working fine. I use SDL if that makes any difference.

Share this post


Link to post
Share on other sites
What exception gets thrown? And if you get chucked into the debugger, what line of what source file is it at? (It'll be in the CRT somewhere). Can you show us the definition of the function? If not, then try cutting code out of it until the program doesn't crash.

Share this post


Link to post
Share on other sites
Unhandled exception at 0x77f69bdd in Pentominoes.exe: 0xC0000005: Access violation writing location 0x00000010

in fclose.c

int __cdecl fclose (
FILE *stream
)
{
int result = EOF;

_ASSERTE(stream != NULL);

/* If stream is a string, simply clear flag and return EOF */
if (stream->_flag & _IOSTRG)
stream->_flag = 0; /* IS THIS REALLY NEEDED ??? */

/* Stream is a real file. */
else {
_lock_str(stream);
__try {
result = _fclose_lk(stream);
}
__finally {
_unlock_str(stream);
}
}

return(result);
}

Exception thrown at the bold line. Not sure what _lock_str() is.

I don't call this function, why is it throwing an exception? I suspect SDL does some clean up in the background and since there is fclose() in my program, it is complaining?

Share this post


Link to post
Share on other sites
It's not a C++ exception, it's a null pointer error I bet that stream is NULL and that the lock member which _str_lock() tries to access is at offset 10 from stream. [grin]

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
It's not a C++ exception, it's a null pointer error I bet that stream is NULL and that the lock member which _str_lock() tries to access is at offset 10 from stream. [grin]
Thats what I was going to say, but he said that he never calls fclose() at the end of his app, and this line: if (stream->_flag & _IOSTRG) would crash first.

EDIT: What does the stack trace show?

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
It's not a C++ exception, it's a null pointer error I bet that stream is NULL and that the lock member which _str_lock() tries to access is at offset 10 from stream. [grin]

I already checked the stream before calling fclose().



Anyway, it's the SDL. I linked it against Multithreaded Debug DLL for Debug mode, and when I recompile there is a conflict with msvcrt.lib. So I set it to Multithreaded DLL (just as how I do it for Release mode) and the problem is gone. *shrugs*

Share this post


Link to post
Share on other sites

This topic is 4814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this