ifstream opening file causes crash

Started by
11 comments, last by Marco H 19 years, 3 months ago
What kind of crash is it anyway? An unhandled exception? An access violation?
What is the state of the program at the instant of the crash? Have you traced it in the debugger?
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
Quote:Original post by Marco H
I open the file in a function, like:

*** Source Snippet Removed ***

So it should be deleted anyway, or not?


"Deleted"?????? I hope you just weren't paying attention to what you were writing, but if that is really what you meant: 1) Deleting the file you are trying to read would obviously cause problems, or 2) Deleting the non-dynamically allocated variable in could have disastrous results; you should just be allowing it to go out of scope and be deleted by the compiler.

Assuming that you don't have a line like
delete &in // I hope you don't have this.
I would check file_name. Do you really know for sure that do_something() is being passed the same file_name every time? If file_name is somehow corrupted after the third call of do_something, then that would explain do_something crashing. I would recommend searching (in the code, with the debugger, and by adding extra diagnostic outputs) to find out if file_name is altered anywhere it shouldn't be. The very first thing you should do is make sure file_name is not being changed in do_something() itself. To do this, change the function declaration from
void do_something(char * file_name)
to
void do_something(const char * file_name)
file_name should be a constant in do_something() because there is no legitimate reason for file_name to be changed in do_something() and if you make it a constant the compiler will tell you if it is being changed for some reason. Good luck.

EDIT: I just saw that someone has already asked about file_name. Unless you check file_name right before you open the file each time, it might still be worth looking into. However, even if you feel that it is not worth the time, at least change
void do_something(char * file_name)
to
void do_something(const char * file_name)
because that will only take a moment and that is how it should have been from the beginning anyways.
It is an access violation. It seems like I solved my problem by finding an unclosed file (open_sector() also had opened a file and I did not close it before calling do_something(), perhaps that gave the error), but right now I have got another strange error while allocating some memory.

The line
num_materials = new word;
crashes my program (now the 5th time I reload the sector). I guess I corrupt the heap somewhere, so the program crashes at a certain point, as the crash-causing line is only a clean allocation. Perhaps I should try some sort of memory-leak finder?

@mumpo:
I did not mean to "delete &in //never ever", just to let it go out of scope. The file_name is okay, but your tip with the const char's is good. I saw it before, but never took it for that important. Thanks :)

[Edited by - Marco H on January 1, 2005 6:58:02 PM]

This topic is closed to new replies.

Advertisement