释放分配的对象时出现问题

发布于 2024-11-29 10:20:49 字数 744 浏览 1 评论 0原文

[searchDict 版本] 发生崩溃。如果我切换最新两行的顺序,它仍然会在最新行上崩溃(现在 [searchArray 版本])。我对 Objective C 很陌生,我想我没有正确分配/释放...帮助? :)

NSMutableDictionary *searchDict = [[NSMutableDictionary alloc] init];
NSMutableArray *searchArray = [[NSMutableArray alloc] init];

for(int i = 0; i < 2; i++) { // Run two times ("Favorites" and "All")
    searchDict = [listOfItems objectAtIndex:i];
    searchArray = [searchDict objectForKey:@"Entries"];

    for (NSString *sTemp in searchArray) {
        NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
        if (titleResultsRange.length > 0)
            [listOfItemsDynamic addObject:sTemp];
    }
}

[searchArray release];
[searchDict release];

Crash occurs at [searchDict release]. If I switch order of the two latest lines it still crash on the latest line (now [searchArray release]). I'm quite new to Objective C and I guess I haven't got the alloc/release right... Help? :)

NSMutableDictionary *searchDict = [[NSMutableDictionary alloc] init];
NSMutableArray *searchArray = [[NSMutableArray alloc] init];

for(int i = 0; i < 2; i++) { // Run two times ("Favorites" and "All")
    searchDict = [listOfItems objectAtIndex:i];
    searchArray = [searchDict objectForKey:@"Entries"];

    for (NSString *sTemp in searchArray) {
        NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
        if (titleResultsRange.length > 0)
            [listOfItemsDynamic addObject:sTemp];
    }
}

[searchArray release];
[searchDict release];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆夏时光 2024-12-06 10:20:49

您分配空间并将其分配给变量:

NSMutableDictionary *searchDict = [[NSMutableDictionary alloc] init];
NSMutableArray *searchArray = [[NSMutableArray alloc] init];

但是然后您将非本地分配的数据分配给它们:

searchDict = [listOfItems objectAtIndex:i];
searchArray = [searchDict objectForKey:@"Entries"];

所以基本上,您不需要分配和释放。相反,做这样的事情:

NSMutableDictionary *searchDict; // Just declartion, no allocation / init
NSMutableArray *searchArray;     // Just declartion, no allocation / init

for(int i = 0; i < 2; i++) { // Run two times ("Favorites" and "All")
    searchDict = [listOfItems objectAtIndex:i];
    searchArray = [searchDict objectForKey:@"Entries"];

    for (NSString *sTemp in searchArray) {
        NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
        if (titleResultsRange.length > 0)
            [listOfItemsDynamic addObject:sTemp];
    }
}

// No release needed here

如果你熟悉 C,它类似于:

char *pChar;
pChar = malloc(15); // allocate memory and assign to pChar
pChar = "Hello";    // assign new address to pChar
free(pChar); // Error here ;)

You allocate space and assign it to the variables:

NSMutableDictionary *searchDict = [[NSMutableDictionary alloc] init];
NSMutableArray *searchArray = [[NSMutableArray alloc] init];

But then you assign non locally-allocated data to them:

searchDict = [listOfItems objectAtIndex:i];
searchArray = [searchDict objectForKey:@"Entries"];

So basically, you don't need to allocate and release. Instead do something like this:

NSMutableDictionary *searchDict; // Just declartion, no allocation / init
NSMutableArray *searchArray;     // Just declartion, no allocation / init

for(int i = 0; i < 2; i++) { // Run two times ("Favorites" and "All")
    searchDict = [listOfItems objectAtIndex:i];
    searchArray = [searchDict objectForKey:@"Entries"];

    for (NSString *sTemp in searchArray) {
        NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
        if (titleResultsRange.length > 0)
            [listOfItemsDynamic addObject:sTemp];
    }
}

// No release needed here

If you are familiar with C, it is similar to:

char *pChar;
pChar = malloc(15); // allocate memory and assign to pChar
pChar = "Hello";    // assign new address to pChar
free(pChar); // Error here ;)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文