0
我一直在调试我的程序几个小时,每次运行它时都会收到错误EXC_BAD_ACCESS
。
我按照此tutorial中的步骤操作,但没有任何结果。
NSMutableArray“EXC_BAD_ACCESS”
我会尝试发布一些代码和我的发现,希望有人能帮我解决我的问题。
-(void)grabData{
listOfItems=[[NSMutableArray alloc] init];
NSString *XMLPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"list.xml"];
NSData *XMLData = [NSData dataWithContentsOfFile:XMLPath];
CXMLDocument *rssParser = [[[CXMLDocument alloc] initWithData:XMLData options:0 error:nil] autorelease];
NSArray *buttons=[rssParser nodesForXPath:@"//item" error:nil];
for (CXMLElement *node in buttons) {
int counter;
if([[[node attributeForName:@"type"] stringValue] isEqualToString:@"photobutton"]){
itListCell *itl=[[itListCell alloc] initWithNibName:@"itListCell" bundle:nil];
itl.rows=0;
itl.iw=0.0;
itl.ih=0.0;
[email protected]"";
[email protected]"";
[email protected]"";
[email protected]"";
[email protected]"";
for(counter = 0; counter < [node childCount]; counter++) {
if([[[node childAtIndex:counter] name] isEqualToString:@"text"]){
NSString * value = [[[node childAtIndex:counter] stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([value length] != 0){
itl.text=[[node childAtIndex:counter] stringValue];
}
}
else{
if([[[node childAtIndex:counter] name] isEqualToString:@"uiid"]){
itl.uiid=[[node childAtIndex:counter] stringValue];
}
else{
if([[[node childAtIndex:counter] name] isEqualToString:@"rows"]){
itl.rows=[[[node childAtIndex:counter] stringValue] intValue];
}
else{
if([[[node childAtIndex:counter] name] isEqualToString:@"icon"]){
itl.iUrl=[[node childAtIndex:counter] stringValue];
CXMLElement *n=(CXMLElement*)[node childAtIndex:counter];
NSString *resString=[[n attributeForName:@"resize"] stringValue];
NSArray *resArray= [resString componentsSeparatedByString: @","];
itl.iw=[[resArray objectAtIndex:0] floatValue];
itl.ih=[[resArray objectAtIndex:1] floatValue];
}
else{
if([[[node childAtIndex:counter] name] isEqualToString:@"action"]){
itl.action=[[((CXMLElement*)[node childAtIndex:counter]) attributeForName:@"name"] stringValue];
NSArray *getaUrl=[node elementsForName:@"action"];
for(CXMLElement *n in getaUrl){
for(int c = 0; c < [n childCount]; c++) {
if([[[n childAtIndex:c] name] isEqualToString:@"url"]){
itl.aUrl=[[n childAtIndex:c] stringValue];
}
}
}
}
}
}
}
}
}
[listOfItems addObject:itl];
[itl release];
}
}
}
- (void)dealloc {
[listOfItems release];
[scroll release];
[super dealloc];
}
看来NSMutableArray *listOfItems
是造成这个问题。
如果我在grabData
函数中将保留添加到其分配中,则不会再出现错误。
如果我从dealloc
函数中删除[listOfItems release];
,则不会再发生错误。
但最有意思的是,如果我删除grabData
函数的最后一行,即[itl release];
不会再出现错误。
我被卡住了,找不到解决方案。如果有人能帮我解决这个问题,我将非常感激。
编辑: 的要求,这是的itListCell
//
// itListCell.h
// TemplatesTest
//
// Created by foobyte on 6/30/11.
// Copyright 2011 FOO. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "Constants.h"
#import "imageDelegate.h"
@interface itListCell : UIViewController {
UIImageView *i;
UILabel *l;
BOOL imageDidLoad;
CGFloat width;
CGFloat maxCalculatedHeight;
CGSize strSize;
//properties
int rows;
CGFloat iw;
CGFloat ih;
NSString *uiid;
NSString *text;
NSString *action;
NSString *aUrl;
NSString *iUrl;
}
@property (nonatomic, retain) UIImageView *i;
@property (nonatomic, retain) UILabel *l;
@property (nonatomic, retain) NSString *uiid;
@property (nonatomic, retain) NSString *text;
@property (nonatomic, retain) NSString *action;
@property (nonatomic, retain) NSString *aUrl;
@property (nonatomic, retain) NSString *iUrl;
@property (nonatomic) int rows;
@property (nonatomic) CGFloat iw;
@property (nonatomic) CGFloat ih;
-(void)setwidth:(CGFloat)w;
-(CGFloat)getMaxHeight;
-(UIImage *)downloadIcon:(NSString *)s;
@end
h和这里是使用对象listOfItems
功能:
-(void) setupPage{
[scroll setCanCancelContentTouches:NO];
scroll.indicatorStyle=UIScrollViewIndicatorStyleWhite;
scroll.clipsToBounds=YES;
scroll.scrollEnabled=YES;
scroll.pagingEnabled=NO;
[scroll setBackgroundColor:[UIColor blackColor]];
CGFloat y=0.0;
CGFloat cy=0.0;
int count=[listOfItems count];
for(int i=0;i<count;i++){
[((itListCell *)[listOfItems objectAtIndex:i]) setwidth:self.view.frame.size.width];
CGFloat h=[((itListCell *)[listOfItems objectAtIndex:i]) getMaxHeight];
((itListCell *)[listOfItems objectAtIndex:i]).view.frame=CGRectMake(lITX, y, self.view.frame.size.width-lITM, h);
[scroll addSubview:((itListCell *)[listOfItems objectAtIndex:i]).view];
y+=h;
if(i >= 2)
cy+=h;
}
[scroll setContentSize:CGSizeMake(scroll.frame.size.width, cy+[scroll bounds].size.height)];
}
它看起来像是你的一个'itl'对象(或者它的一个实例变量/声明的属性)被过度释放。 – 2011-07-06 10:25:15
如果您使用XCode 4,command-shift-B是您的朋友。 ;) –
你可以发布'itListCell'的.h文件 - 你应该为uuid,text,action,aUrl和iURL属性设置'copy'或'retain'。 – deanWombourne