IPhone: memory management issues

I'm new to iPhone development, and I read a lot before jumping into my first app. All this reading made me realize that proper memory management is very important, so I want to do everything right.

I just finished my first application and started testing it for leaks. There were a lot :) I changed the code to fix the leaks and then started to get BAD_ACCESS exceptions. Now to my questions:

  • I have a UITextView object, let's call it "utv". I saw that his text field is defined as follows:

    @property (non-nuclear, copy) NSString * text;

If I write the following line of code:

utv.text = [NSString stringWithString:@"Blabla"]; 

I should not worry about the autostart pool freeing the line, right? Because he uses a copy?

  • What if it was defined as

    @property (non-atomic, persistent) NSString * text;

    Should I still not worry about the auto-detection pool, because holding increased the link count by 1?

  • Can I find out when the autorealease pool will issue a string that I create using stringWithString instead of initWithString?

Thanks! Eli

+4
source share
5 answers

In my opinion, you answered your question very well. If the property was retain , the new value will be sent to retain in its synthesized installation method, so you should not worry about that :)

Below are the differences in code between setters to help you understand how they relate to memory:

 // assign property = newValue; // retain if (property != newValue) { [property release]; property = [newValue retain]; } // copy if (property != newValue) { [property release]; property = [newValue copy]; } 
+3
source

Do not think about memory management from the point of view of abstracts pools or keep an account. Think about it in terms of whether you own the property or not. If you own it, you are responsible for his release.

Both of these lines create stored properties:

 @property (nonatomic, copy) NSString *text; @property (nonatomic, retain) NSString *otherText; 

Copy simply means saving a copy, not the original. Both of these properties are memory managed, so you donโ€™t have to worry about saving or releasing under normal use.

 self.text = string1; // this retains a copy of string1 self.text = string2 // this releases the copy of string1 and retains the copy of string2 self.text = nil; // this releases the copy of string2 

You have self.text and self.otherText, so you need to free them in dealloc.

If you do not select, not new, copy or save the object, you do not own it. If you do not own it, but you need to guarantee that it will adhere to the end of the launch cycle, claim ownership, retaining it.

+1
source

You're right. You should not apply to an autocomplete pool when the property is set as save or copy. You will not know when NSAutoreleasePool will release your string, but you can assume that you can use it in the same method that is declared freely. It is assumed that each thread has its own NSAutoreleasePool , which can be depleted at any time, but is usually depleted at the beginning of each cycle of the cycle.

0
source

Everything that you save needs to be freed. You cannot depend on when autorelease releases something in the pool.

This question has been asked before. Here is a simple answer

http://cocoadevcentral.com/d/learn_objectivec/

0
source

This is a good read for memory management.
Hope this helps someone.

0
source

Source: https://habr.com/ru/post/1347269/


All Articles