Почему происходит сбой, если я освобождаю параметр NSIndexSet, переданный в insertSections: withRowAnimation: - PullRequest
0 голосов
/ 06 февраля 2012

Я написал некоторый код для пакетного удаления / вставки разделов и ячеек из / в табличное представление.Но я столкнулся с проблемой, которую не могу понять.Мои коды следующие:

NSMutableArray *deleteIndexPaths = [[NSMutableArray alloc] initWithCapacity:1];
NSMutableArray *insertIndexPaths = [[NSMutableArray alloc] initWithCapacity:1];
NSIndexSet *insertSections = [[NSIndexSet alloc] initWithIndex:kDeleteSection];
NSArray *insertDeleteIndexPaths = [[NSArray alloc] initWithObjects:[NSIndexPath indexPathForRow:0 inSection:kDeleteSection], nil];

/// init deleteIndexPaths and insertIndexPaths

[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];
[self.tableView insertSections:insertSections withRowAnimation:UITableViewRowAnimationRight];
[self.tableView insertRowsAtIndexPaths:insertDeleteIndexPaths withRowAnimation:UITableViewRowAnimationRight];
[self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];

[insertSections release];
[deleteIndexPaths release];
//[insertSections release]; // If uncommented it, program will crash.
[insertDeleteIndexPaths release];

В качестве комментария к коду, если я раскомментирую оператор [insertSections release]; , программа завершится сбоем.Зачем?Я не могу найти ответ.

Ответы [ 2 ]

3 голосов
/ 06 февраля 2012

Потому что вы выпускаете его дважды:

**[insertSections release];**
[deleteIndexPaths release];
**//[insertSections release]; // If uncommented it, program will crash.**
1 голос
/ 06 февраля 2012

Я собираюсь пойти с быстрым и простым ответом, основанным на предоставленном коде, вы создаете ситуацию переиздания. Когда вы звоните +alloc на NSIndexSet наверху, вы неявно утверждаете право собственности и нуждаетесь в смещающем вызове -release. В приведенном примере закомментированный вызов -release является вторым и приводит к отправке сообщения в экземпляр, который был освобожден.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...