Memory leak problem and i need help #1

前端 未结 5 682
北海茫月
北海茫月 2021-01-23 08:34

I am very new at this and seems to have a leak in this piece of code that i cannot fix:

The Instruments shows on this line with a 100%:

NSMutableA

相关标签:
5条回答
  • 2021-01-23 08:46

    Since I believe the code from picciano will fix the issue of the openingsposter, here a small explanation why it should fix the issue.

    If you give a property the retain attribute, it will create an accessor method that looks somewhat like this (simplified):

    @property (nonatomic, retain) NSValue *value;
    
    - (void)setValue:(NSValue *)aValue {
        value = [aValue retain];
    }
    

    Only when the retainCount reaches 0 an object is released, using retain, alloc and copy increases the retainCount. Remember: only when using the accessor method the retain actually happens (besides using alloc, retain and copy directly). The accessor method is usually called when using one of the following methods:

    // the 2 most obvious ways to call the accessor methods ...
    object.value = someValue;
    [object setValue:someValue];
    

    You created a retain property in your code, yet you didn't use the accessor method, so the object was never retained.

    // no accessor used here ...
    managedObjectContext = [(FamQuiz_R0_1AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    

    If you would release it from this point on, it would cause a crash, since the retainCount would actually become -1 at some point (since it never got to 1 in the first place). Therefore you should set the property like this:

    // the dot-notation syntax to make use of the accessor method ...
    self.managedObjectContext = [(FamQuiz_R0_1AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    

    or (in my opinion preferably):

    // making use of the accessor method directly, which is very unambiguous ...
    NSManagedObjectContext *context = [(FamQuiz_R0_1AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    [self setManagedObjectContext:context];
    

    This way you can be sure the retain actually happens.

    The second notation to accessor setters is in my opinion superior and I consider it good habit to use it for setting properties whenever possible. Read more about people who share this opinion and their reasoning on the following sites:

    • Cocoa Is My Girlfriend
    • The Big Nerd Ranch
    0 讨论(0)
  • 2021-01-23 08:52

    It seams that you are returning the object only inside the if statement. Meaning that if the if statement is false you will not autorelease the array. Or maybe you didn't paste the entire method. Let me know. Instruments is sometimes tricky.

    0 讨论(0)
  • Instruments is telling you were the leaked object was allocated, not where it was necessarily leaked.

    While you may not be autoreleasing the array in all cases on return from that method, you might also be retaining it somewhere else and not balancing that retain with a release.

    0 讨论(0)
  • 2021-01-23 09:09

    I am assuming you set the property managedObjectContext to "retain". Change the line to this (include "self" so that it gets retained):

    if (self.managedObjectContext == nil) { self.managedObjectContext = [(FamQuiz_R0_1AppDelegate *)
                                                           [[UIApplication sharedApplication] delegate] managedObjectContext]; }
    

    Then add your release back in.

    0 讨论(0)
  • 2021-01-23 09:10

    This is a dupe of your other question Memory leak problem and i need help #1

    When i did release i got into trouble, of course. I did try to change the names on the three and do release so there was unique names but that did not work.

    Changing the names across three different files? That won't do anything and it indicates that you haven't entirely wrapped your head around objects, pointers, and memory management.

    The Objective-C and Memory Management guides will help.

    Could this be the reason for the leak i have in this .m file?

    Nope -- as I answered in the other question, the leak is most likely because you retain the object that is returned by that method and then don't release it anywhere.

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