Nsmutablearray initialization Crash issue

Source: Internet
Author: User

Phenomenon:

@property (nonatomic, copy) Nsmutablearray *array;

Self.array = [Nsmutablearray alloc] initwithcapacipty:5];

[Self.array addobject:@ "Hello"];

Why do the gurus say that they're going to get an error?

If you write like this, there is no error.

_array = [[Nsmutablearray alloc] initwithcapacipty:5];

[_array addobject:@ "Hello"];

Landlord has not @property (I myself also smattering), you use Self.array = is the setter method to assign a value to the array, and you set the setter method to copy, and the copy generated by the array pointer to a non-variable group

(see Apple's official documentation for details), when you call the Add method, there's definitely a problem, and Xcode prompts: "Unrecognized selector sent to instance".

As for _array why not error, very simple, because _array is a variable, there is no copy modification, point to or you originally generated a mutable array, so the Add method is valid.

Symptom 2:

An array of Nsmutablearray *localfiles is declared in a Uitableviewcontroller class as a member variable.

In the Viewdidload method, read out all the file names in the application documents directory in this array, initialized with this method:

Localfiles = [Nsmutablearray array];    This is an automatically released object.

Then loop calls the AddObject method, the other there is no special operation, the program can display a list of files, but scroll up or down a little bit, the program crashes, the error message displayed is also quite strange:

[__nsarrayi AddObject:]: Unrecognized selector sent to instance 0x4b1b8b0

I'll check. The call stack for each error shows an error when executing to the Cellforrowatindexpath method, and when the variable is traced to Localfiles, the out of scope is displayed on the debugger. The contents of this localfiles pointer do not know where to go.

From Google to look up a lot of information, in the message of this site to find the following paragraph, immediately enlightened:

It doesn ' t really matter. [Nsmutablearray array] is a nice shortcut, and if you had to remember to retain it, so the question really was a matter of [[ Nsmutablearray Array] retain] versus [[Nsmutablearray alloc] init]. I usually use the former. The real convenience comes in if you need to statically fill the array; You can do it all in one message. [[Nsmutablearray arraywithobjects: ...] retain] is faster than [[Nsmutablearray alloc] init] followed by numerous [nsmutabl Earray AddObject: (ID)] calls.

The original is retain! after calling the array method. or instead of [[Nsmutablearray alloc] init] method combination, tried 2 kinds of modification methods are good, the program no longer crashes. Just a few words, it took me almost a day.

Localfiles = [[Nsmutablearray array] retain];

This day in the objective-c some of the memory management knowledge, although a few detours, but the term autorelease is a bit of understanding.

Later carefully read the "Objective C tutorial," The 9th chapter "Memory Management", the original section 9.3 "Cocoa Memory Management Rules" (138th page) The author has long emphasized three rules:

(1) When you create an object using the new, alloc, or Copy method, the object's reference counter value is 1. When you no longer use the object, you are responsible for sending a release or Autorelease message to the object. In this way, the object will be destroyed at the end of its useful life.

(2) When you obtain an object by any other means, assume that the object has a reference counter value of 1 and has been set to auto-release, you do not need to do anything to ensure that the object is cleaned up. If you intend to have the object for a period of time, you need to keep (retain) it and make sure that it is released when the operation is complete.

(3) If you keep (retain) an object, you need to (eventually) release or release the object automatically. The Retain method must be kept equal to the number of times the release method is used.

My program is the iphone program, in order to reduce the memory footprint of the program, cocoa specifies the destruction time of the automatic free pool in the GUI application, creates an auto-free pool before the program starts processing the event, and destroys the auto-release pool after the event processing is finished. My Localfiles object creates an auto-release object at the beginning, completes the next event loop, and the auto-release pool is destroyed, the Localfiles object is freed, and the next event loop is entered when scrolling tableview. So the contents of the Localfiles object will not be found.

Nsmutablearray initialization Crash issue

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.