Wednesday, September 17, 2008

Segmentation Fault

I have some good news and some bad news. Good news is that SWIG driven C++/Python combination is working fine.

The bad news is that this attempt of mine also happens to be the time I have jumped headlong into using pointers. I have used them before, but only in small amounts, where the code was well understood and in working state and even then, they were only introduced as part of optimizations. So now all their nastiness is being exposed to me. I am getting segmentation faults at seemingly random places. An example.

I am using a file and when I am done using it, I set it to NULL. Further, I was closing the file in the destructor as an added precaution as well. So it led to an attempt to

fclose(filePtr);//filePtr is NULL when this is called

This was causing a segmentation fault. I had no idea that you can't close a NULL file pointer. Now I have dropped this call altogether. I am still getting segmentation faults in seemingly random places. I can't use gdb to debug it either. (I don't know how). Segmentation faults are supposed to be the easiest ones to find. You just run them in a debugger and it points to the offending location for you. But, no such luck for me. So bottom line, code on.

No comments: