티스토리 뷰

카테고리 없음

[objective-c] NSMutableArray 파괴

필살기쓰세요 2020. 12. 9. 16:49

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.

-------------------

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];
-------------------

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).

-------------------

목록을 재설정하려고 할 때만 누출이 발생하면 방금 해제하려고 한 다른 객체를 사용하는 누군가 / 다른 사람이있는 것입니다.

-------------------

여기에 훌륭한 답변을 제공하기에 충분한 정보가 없습니다. NSMutableArray여전히 할당되어 있지만 비어 있고 객체가 없지만 이전에 배열에 있던 객체는 앱의 해당 지점에서 할당 해제되어야하지만 여전히 할당된다는 말입니까?

이 경우 배열이 비어있을 수 있지만 개체가 전송 된 할당 해제 메시지를 처리하지 않는 경우 개체는 여전히 메모리에 있지만 아무것도 참조하지 않습니다.

나는 당신을 돕기 위해 말하고 싶습니다 .MallocDebug가 정확히 무엇을 유출하고 있다고 말하는지 정확히 알고 싶습니다. 또한 @Elfred는 @property배열에 대한 메서드를 확인하는 데 좋은 조언을 제공합니다 . 실제로 유지하거나 복사해야합니다.



출처
https://stackoverflow.com/questions/180024
댓글
공지사항
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31