从自定义对象获取数据期间出现EXC_BAD_ACCESS错误

时间:2011-07-31 11:53:02

标签: iphone nsmutablearray

我在Array中存储了自定义对象数据。我从函数中的自定义对象数组中获取数据。当我第一次调用函数它工作正常但是当我一次又一次调用它时,我得到了EXC_BAD_ACCESS。

这是功能细节。

-(void) facebookDisplayFunction:(int)atIndex {


FacebookWallData *wall = (FacebookWallData *)[facebook_wallDataArray objectAtIndex:atIndex];


NSString *friendID= wall.actor_id;
NSString *linkFetch= wall.permalink;
NSString* postID=wall.postId;

NSNumber *countNumber;

NSString *friendName=@"";
NSString* profileThumImage=@"";

for(int i=0; i< [facebook_LikesArray count];i++) {

    FacebookLikes* countValues=[[FacebookLikes alloc]init];
    countValues=[facebook_LikesArray objectAtIndex:i];

 //   NSLog(@" postId_wall %@  LikePostId = %@",postID,countValues.PostID);
    if([postID isEqualToString:countValues.PostID]) {
        countNumber=countValues.Count;

        if(countNumber>0) 
            friendID=countValues.Friends;

        [countValues release];
        break;
    }

    [countValues release];
}


for(int i=0;i< [facebook_FreindsArray count];i++) {

    FacebookFreinds* friendsRecord=[[FacebookFreinds alloc]init];
    friendsRecord=[facebook_FreindsArray objectAtIndex:i];

    if([friendID isEqualToString:friendsRecord.UID]) {
        friendName=friendsRecord.name;
        profileThumImage=friendsRecord.pic_smal;
        [friendsRecord release];
        break;
    }
    [friendsRecord release];
 }

// Adding values in table //





[imageData addObject:@"facebook.png"]; 
[tableList addObject:wall.messages];
[profileUserName addObject:friendName];
[linksOfFacebookData addObject:linkFetch];
[RetweetAndLikeData addObject:@"5"];
[favedProfileThumb addObject:profileThumImage];
[twitterPostID addObject:@""];
[eachPostUID addObject:friendID];

  [wall release];

}

我在这里呼唤功能。    [self facebookDisplayFunction:0];    [self facebookDisplayFunction:0]; //这里有EXC_BAD_ACCESS错误。

2 个答案:

答案 0 :(得分:0)

如果您收到错误:

 [self facebookDisplayFunction:0];

在我看来,self指向的对象很可能已被释放。所以,问题不在facebookDisplayFunction ...

您是否可以查看如何创建self指向的对象,或者在需要更多帮助时发布代码?

答案 1 :(得分:0)

为什么要分配像FacebookLikes* countValues=[[FacebookLikes alloc]init]这样的对象,然后使用此代码countValues=[facebook_LikesArray objectAtIndex:i]为此变量分配数组中的实例,稍后使用此[countValues release]将其释放?你不知道自己在做什么。

尝试更改此内容:

FacebookLikes* countValues=[[FacebookLikes alloc]init];
countValues=[facebook_LikesArray objectAtIndex:i];

到这个

FacebookLikes* countValues = [facebook_LikesArray objectAtIndex:i];

并删除所有出现的[countValues release]。对第二个for循环中的friendsRecord执行相同操作。另外,[wall release]是什么?删除它!

您不应该分配任何这些对象,因为您实际上是从该数组中获取它们,而不是创建新实例。这只会在代码中造成泄漏。您不应释放任何这些对象,因为它们由数组保留,并且它们负责在从阵列中删除它们或者在销毁/取消分配数组之后释放它们。请rtfm

相关问题