What happens if OC sends a message to an object that has already been disposed of

Source: Internet
Author: User
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)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.