OC Memory Management

Source: Internet
Author: User

When passing parameters between view controllers, pay special attention to the "wild Pointer" issue.

Because the program is always beating between view controllers, some view controllers have variables (related to the UI). When the view jumps, will be released (to save iPhone memory ?).

For example:

In a viewcontroller:

Recipientroll * controller = [[recipientroll alloc] initwithnibname: @ "recipientroll" msgtext: tvbill. Text
Expenseid: expense_id];
[Self presentmodalviewcontroller: controller animated: Yes];

Pay attention to the red part and pass a string parameter to another viewcontroller. However, this parameter references the UI control attributes of this viewcontroller.

Then, another viewcontroller is displayed in the form of a pattern. The ui of the first viewcontroller is released. So the memory of the string you passed is released, and its lifecycle is only a short period of time during the construction. When the mode form is displayed, it is released. If you want to do something with it later and the program crashes, xcode reports exec bad.

Therefore, we need to use its short life cycle time to copy strings. Write in the constructor as follows:

-(ID) initwithnibname :( nsstring *) nibnameornil msgtext :( nsstring *) d expenseid :( INT) idx {
// Nslog (@ "initwithnibname ");
If (Self = [Super initwithnibname: nibnameornil Bundle: Nil]) {
Expense_id = idx;
Msgtext = [[nsstring alloc] initwithstring: d];
// Do not write it like this: msgtext = d

Return self;
In this way, you can safely use the msgtext variable before the viewcontroller is released.

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.