What happens if OC sends a message to an object that has already been disposed of
Keywords: MRC, ARC, Exc_bad_instruction, deallocated
First, ask questions
What happens when OC sends a message to an already-freed object.
second, make assumptions
Sends a message to an already freed object, and the program will flash back.
third, verification issues
1. Create MRC Project
To facilitate verification, we use MRC to test.
Create a project, switch the environment to Mrc,target's build settings search, Objective-c Automatic Reference counting, objective-c Automatic Reference Counting is set to No.
2. Create an Object person
Add a Eat method to print "Eat"
3. Send a EAT message to the released Person object
Inside the Viewcontroller.
-(void) viewdidload {
[super viewdidload];
Person *p = [[Person alloc] init];
[P eat];
[P release]; Call release, Release P
[P eat];//Send a EAT message to the already freed P object
}
(1) Run the program;
(2) again in [P release] This line break point, let the program execute to the previous line, p is not released when the Lldb inside PO p.
4. Results
(1) The running program Flash back to [P eat] this line, the flashback information is as follows:
[P Eat] line error message
Thread 1:exc_bad_instruction (CODE=EXC_I386_INVOP, subcode=0x0)
Lldb inside Log
2018-03-27 16:20:12.558905+0800 runloop[2930:216258] * * *-[person Eat]: message sent to deallocated instance 0x60000000b6 D0
(2) Breakpoint debugging, PO p return
(LLDB) Po P
2018-03-27 16:31:50.075191+0800 runloop[3052:225294] * *-[person respondstoselector:]: Message sent to D eallocated instance 0x60000000b060
0x000060000000b060
(LLDB)
Iv. Draw Conclusions
Sends a message to an object that has been released in OC, the program flashes directly and reports the following main error message: exc_bad_instruction, message sent to deallocated instance
[Reference exc_bad_access] (http://www.cocoachina.com/ios/20160226/15324.html)