Search code examples
objective-csegmentation-faultarchive

What is segmentation fault in Objective-C?


I have written a small Objective-C program to archive an object. The object is an NSDictionary. There was no problem during compiling. But when it was called it displayed a message "Segmentation fault" in the terminal, and no archive XML file was created. The problem was caused by the writeToFile method, as after I had commented it out there was no error (of course no archiving either). What does this error message mean? And how to fix it?

I just found out the reason of the problem. My program is based on the example in a book and there is a misprint so the dictionary object has never actually initialized. Sorry I did not check this problem throughly before asking.


Solution

  • It's hard to pinpoint the exact problem without any sample code, but a segmentation fault occurs when the CPU is trying to access memory that it does not own. This generally means that you have a pointer that is not initialized, or doesn't point to the right place, and then you're attempting to de-reference it which causes the crash. I suggest combing over your code and make sure all your pointers are doing what you expect them to do.