类变量类型发生变化

发布于 2024-07-27 03:52:01 字数 2749 浏览 6 评论 0原文

因此,在我的视图控制器中,我运行代码来填充 Customer(自定义类)对象的 NSArray。 此自定义类包含另一个名为“地址”的自定义类的对象(客户有帐单地址和送货地址)。 在视图控制器中,当选择列表中的客户时,它会向新的视图控制器传递一个客户对象,如下所示:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    InfoViewController *customerinfoViewController = [[InfoViewController alloc] initWithStyle:UITableViewStyleGrouped andCustomer:[[[customers objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] retain]];
    [self.navigationController pushViewController:customerinfoViewController animated:YES];
    [customerinfoViewController release];
}

我第一次在运行应用程序时访问此视图控制器时,它工作正常。 然而,当我重新访问视图控制器时,发生了一些有趣的事情。 应用程序崩溃,无法识别的选择器被发送到实例 0x00。 使用 xCode 中的鼠标悬停调试功能,我发现客户的 ShipAddress 变量的第一个对象的类型从 NSString 更改为 NSIndexPath。 客户的 billAddress 对象不会发生这种情况。 有人知道这里发生了什么事吗? 看来我可能遇到内存管理问题,但在我将代码分解跟踪所有保留和释放之前,我肯定希望对此进行确认......

编辑:更多信息在这里。 使用以下代码,我在类级别有一个 NSMutableArray 。 在循环的每次迭代中,我都会循环遍历 XML 中的节点(效果很好)。 每次检测到一个新字母作为名称的第一个字母时,我都会创建一个新的子数组并将客户添加到其中,从而用检测到的字母表中每个字母的客户子数组填充我的类级别 NSMutableArray(客户)。 我的问题是关于循环客户对象的保留和释放。 Clang Static说客户存在过度保留的错误,但是当我按照Clang修复它时,循环崩溃了。 是什么赋予了? 相关代码如下:

DDXMLDocument *rootDoc = [[[DDXMLDocument alloc] initWithData:xmlData options:0 error:nil] autorelease];
NSArray *elems = [rootDoc nodesForXPath:@"QBXML/QBXMLMsgsRs/CustomerQueryRs/CustomerRet" error:nil];
DDXMLNode *node;
sectionTitles = [[[NSMutableArray alloc] initWithCapacity:1] retain]; // Letters for UITableView section titles
NSMutableArray *subArray;
NSString *lastchar = @"A";
NSString *testchar; 
int indexCount = -1;
customers = [[[NSMutableArray alloc] initWithCapacity:[elems count]] retain];
Customer *newCust;
for (int i = 0; i < [elems count]; i++) {
    node = [elems objectAtIndex:i];
    newCust  = [[Customer alloc] initWithCustomerRetNode:node];
    testchar = [[newCust fullName] substringToIndex:1];
    if (i == 0 || ![[testchar uppercaseString] isEqualToString:lastchar]) {
        [sectionTitles addObject:testchar];
        lastchar = testchar;
        indexCount++;
        subArray = [[NSMutableArray alloc] initWithCapacity:1];
        [customers addObject:subArray];
        [subArray release];
        [[customers lastObject] addObject:[newCust retain]];
    }
    else {
        [[customers lastObject] addObject:[newCust retain]];
    }
    [newCust release];
}

注意:此代码在大多数情况下都有效,但 clang 不喜欢它。

编辑:Customer 类中的地址是这样分配的(现在在 Clang 修复后不起作用)

...
else if ([tempname isEqualToString:@"BillAddress"])
  billAddress = [billAddress initWithAddressNode:tempnode];
else if ([tempname isEqualToString:@"ShipAddress"])
  shipAddress = [shipAddress initWithAddressNode:tempnode];
...

So in my view controller, I run code to populate an NSArray of Customer (custom class) objects. This custom class has objects that are of ANOTHER custom class called Address (a customer has a billing address and a shipping address). In the view controller when a customer in the list is selected, it passes a new view controller a customer object, like so:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    InfoViewController *customerinfoViewController = [[InfoViewController alloc] initWithStyle:UITableViewStyleGrouped andCustomer:[[[customers objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] retain]];
    [self.navigationController pushViewController:customerinfoViewController animated:YES];
    [customerinfoViewController release];
}

The first time I visit this view controller while running the application, it works fine. However, when I revisit the view controller, something interesting happens. The application crashes, with unrecognized selector sent to instance 0x00whatever. Using the mouseover debugging feature in xCode, I am finding that the first object of the customer's shipAddress variable has its type changed from NSString to NSIndexPath. This does not happen to the customer's billAddress object. Anyone have any idea what is going on here? It seems like I may be having memory management issues but I would definitely like a confirmation on this before I tear my code apart tracking down all the retains and releases....

EDIT: More information here. with the following code, I have an NSMutableArray at the class level. At each iteration of the loop, I am looping through nodes in XML (which works fine). Every time a new letter is detected as the first letter of the name, I create a new subarray and add the customer to it, thus filling my class-level NSMutableArray (customers) with subArrays of customers for each letter of the alphabet detected. My question is about the retains and releases of the cycling customer object. Clang Static says there is an over-retaining error on the customer, but when I fix it according to Clang, the loop crashes. what gives? Related code below:

DDXMLDocument *rootDoc = [[[DDXMLDocument alloc] initWithData:xmlData options:0 error:nil] autorelease];
NSArray *elems = [rootDoc nodesForXPath:@"QBXML/QBXMLMsgsRs/CustomerQueryRs/CustomerRet" error:nil];
DDXMLNode *node;
sectionTitles = [[[NSMutableArray alloc] initWithCapacity:1] retain]; // Letters for UITableView section titles
NSMutableArray *subArray;
NSString *lastchar = @"A";
NSString *testchar; 
int indexCount = -1;
customers = [[[NSMutableArray alloc] initWithCapacity:[elems count]] retain];
Customer *newCust;
for (int i = 0; i < [elems count]; i++) {
    node = [elems objectAtIndex:i];
    newCust  = [[Customer alloc] initWithCustomerRetNode:node];
    testchar = [[newCust fullName] substringToIndex:1];
    if (i == 0 || ![[testchar uppercaseString] isEqualToString:lastchar]) {
        [sectionTitles addObject:testchar];
        lastchar = testchar;
        indexCount++;
        subArray = [[NSMutableArray alloc] initWithCapacity:1];
        [customers addObject:subArray];
        [subArray release];
        [[customers lastObject] addObject:[newCust retain]];
    }
    else {
        [[customers lastObject] addObject:[newCust retain]];
    }
    [newCust release];
}

NOTE: this code works for the most part, but clang doesn't like it.

EDIT: Addresses in the Customer class are assigned like so (which now does not work after Clang fixes)

...
else if ([tempname isEqualToString:@"BillAddress"])
  billAddress = [billAddress initWithAddressNode:tempnode];
else if ([tempname isEqualToString:@"ShipAddress"])
  shipAddress = [shipAddress initWithAddressNode:tempnode];
...

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

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

发布评论

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

评论(1

深海里的那抹蓝 2024-08-03 03:52:01

听起来您遇到了过度释放问题,所以是的,内存管理,您可能过度释放了存储对象的数组。但无法从代码片段中真正看出。 您必须仔细查看代码并找到源代码。 另外,使用 Clang Static Analyzer 可能会对您有所帮助。

It sounds like you are having a over release issue, so yes memory management, you might be overreleasing that array you are storing your objects in.Cant really tell from the snippet of code though. Youll have to go and look through the code and find the source. Also using Clang Static Analyzer might be of help to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文