daSn0wie
daSn0wie

Reputation: 919

NSError *error; vs NSError *error = nil;

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?

Upvotes: 5

Views: 2419

Answers (2)

coneybeare
coneybeare

Reputation: 33101

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.

Upvotes: 3

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726639

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.

Upvotes: 6

Related Questions