Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSError *error; vs NSError *error = nil;

Tags:

ios

nserror

I developed an ios app that had a:

NSError *error; 

instead of:

NSError *error = nil;  

It worked fine in while I was debugging in the simulator and debugging on the device while connected. The moment I archived it and sent it into TestFlight to deploy for testing, I started getting Unknown Signal errors coming up in the crash log.

Why does this happen?

like image 464
daSn0wie Avatar asked Feb 25 '12 14:02

daSn0wie


2 Answers

This happens because you have an uninitialized pointer. It does not crash as long as you get lucky, but using such pointers is undefined behavior.

like image 194
Sergey Kalinichenko Avatar answered Nov 17 '22 13:11

Sergey Kalinichenko


To clarify on dasblinkenlights answer, this is declaring a variable:

NSError *error; 

... and this is declaring AND assigning a variable

NSError *error = nil;  

When you use it the first way and try to access it without ever setting it to something, the value it is pointing at is known as "garbage" It is a pointer to some other stack of memory, and accessing it will almost always make your app crash. Thus, it is always best practice to assign a value to your variable as above, or shortly after.

like image 36
coneybeare Avatar answered Nov 17 '22 14:11

coneybeare