Objective C - Memory management question?

后端 未结 4 1228
-上瘾入骨i
-上瘾入骨i 2021-01-28 21:35

I know that when i have a property for an object which is set to retain or copy i must do the following.

Object *o = [[Object alloc] init];
self.myObject = o;
[o         


        
相关标签:
4条回答
  • 2021-01-28 21:50

    What you can do is ensure you release the previously allocated object before assigning the variable holding it to a new instance (be it a retained version of another object, or a copy of it).

    If you don't define a property for it, don't use the dot syntax.

    0 讨论(0)
  • 2021-01-28 21:59

    You don't need to have any properties. Obj-C didn't even have them at all until a couple of years ago with Obj-C 2. Properties are a convenience - they give you some memory management without you writing boilerplate code (the access methods are synthesized for you), and you can use dot notation, which some people are more familiar with than the usual Obj-C syntax.

    Having said that properties take care of memory management for you, that's only partially true. You still need to set all properties to nil in your dealloc method. So that really doesn't buy you much, since without properties you'd still need to release each object in dealloc.

    The big benefit is that properties handle reference counting for you when assigning objects from one place to another. You don't have to write the getters and setters manually. For this reason, I like properties. Plus, clang is gaining the ability to have properties where you only need the @property line, no need for a backing variable, and no need for an @synthesize line. One-line properties are a win in my book. (OK, two lines, you need to add a line to dealloc too :)

    0 讨论(0)
  • 2021-01-28 22:06

    Because Jacob already answered the part of without property. I will answer the left

    Can i do the following without having a leak?

    myObject = [[Object alloc] init];

    You have to release the myObject somewhere, maybe in the delegate method, or maybe after you finish using it, but remember to release the object, especially before you lose the object reference

    Is it better to define a property for every variable I have in every class? Why??

    It depends on your coding convention, define all properties will help you with memory management a lot, but it comes with burden and sometimes your team doesn't want to define instance variable, property and synthesize for every variable, which means quite a lot of codes. So, discussing with your team to see what is your weakness and strength. If you are weak at memory management, I recommend to define for every

    0 讨论(0)
  • 2021-01-28 22:07

    Yes, you can set an instance variable that way and it's totally fine — in fact, that's the preferred way to do it in init methods (because setters might have side effects). Just remember to release the old object (if any) in the variable before setting a new one or you'll leak. And whether you use properties or set the ivar directly, you need to release the object in dealloc.

    0 讨论(0)
提交回复
热议问题