NSMutableArray destruction

前端 未结 5 2051
广开言路
广开言路 2021-02-02 16:48

I have an array NSMutableArray with happy objects. These objects viciously turn on (leak) me whenever I try to clear the array of all the objects and repopulate it.

相关标签:
5条回答
  • 2021-02-02 16:57

    what does your @property declaration look like? are you synthesizing the accessors? If so, you need @property(retain). I'm assuming that when you say the objects are turning on you, you're referring to a core dump (EXC\_BAD\_ACCESS).

    0 讨论(0)
  • 2021-02-02 17:01

    If the leak only occurs when you try to reset the list, you have someone/something else using those other objects that you just tried to release.

    0 讨论(0)
  • 2021-02-02 17:04

    Are you referring to the objects in the array leaking?

    How are you adding objects to the array? The array will retain them, so you need to autorelease or release them after you've added them to the array. Otherwise after the array is released the objects will still be retained (leaked).

    MyEvent *event = [[MyEvent alloc] initWithEventInfo:info];
    [self.eventList addObject:event];
    [event release];
    
    MyEvent *otherEvent = [[[MyEvent alloc] initWithEventInfo:otherInfo] autorelease];
    [self.eventList addObject:otherEvent];
    
    0 讨论(0)
  • 2021-02-02 17:05

    How did you create the objects that are leaking? If you did something like this:

    - (void)addObjectsToArray {
    
        [list addObject:[[MyClass alloc] init];
    
        OtherClass *anotherObject = [[OtherClass alloc] init];
        [list addObject:anotherObject];
    }
    

    then you will leak two objects when list is deallocated.

    You should replace any such code with:

    - (void)addObjectsToArray {
    
        MyClass *myObject = [[MyClass alloc] init];
        [list addObject:myObject];
        [myObject release];
    
        OtherClass *anotherObject = [[OtherClass alloc] init];
        [list addObject:anotherObject];
        [anotherObject release];
    }
    

    In more detail:

    If you follow the first pattern, you've created two objects which, according to the Cocoa memory management rules you own. It's your responsibility to relinquish ownership. If you don't, the object will never be deallocated and you'll see a leak.

    You don't see a leak immediately, though, because you pass the objects to the array, which also takes ownership of them. The leak will only be recognised when you remove the objects from the array or when the array itself is deallocated. When either of those events occurs, the array relinquishes ownership of the objects and they'll be left "live" in your application, but you won't have any references to them.

    0 讨论(0)
  • 2021-02-02 17:17

    I'd say I don't really have enough Information here to give you a great answer. Are you saying that the NSMutableArray is still allocated, but empty and doesn't have any objects in it but that the objects once previously in the array are still allocated even though they should be dealloc'd at that point in the app?

    If that's the case the array could possibly be empty, but your objects aren't handling the dealloc message sent to them, in which case the objects are still around in memory but not referenced by anything.

    I'd say to help you I'd like to know exactly what MallocDebug exactly says is being leaked. Also @Elfred is giving out some good advice in checking your @property method for the array, really it should be retain or copy.

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