2011-07-13 112 views
2

我的iPhone应用程序崩溃时,我加入到viewDidLoad中的方法在我的主视图控制器:iPhone应用程序崩溃 - viewDidLoad中

#import "dbQuestionGetterViewController.h" 
#import "dbConnector.h"; 

@implementation dbQuestionGetterViewController 
@synthesize questions; 

-(void)viewDidLoad{ 
    //code to initialise view 
    NSDictionary* arr = [dbConnector getQuestions:2 from:@"http://dev.speechlink.co.uk/David/get_questions.php"]; 
    questions = arr; 
    [arr release]; 
    [super viewDidLoad]; 
} 

我打电话从dbConnector类的静态方法,但它崩溃之前,甚至装..

在dbConnector的方法:

//method to 
+(NSDictionary*)getQuestions:(NSInteger)sectionId from: (NSString*) url{ 
    //connect to database given by url 
    //NSError  *error = nil; 
    //NSURLResponse *response = nil; 
    NSMutableString* myRequestString = [[NSMutableString string]initWithFormat:@"section=%@", sectionId]; 
    NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]]; 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: url]]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; 
    [request setHTTPMethod: @"POST"]; 
    //post section 
    [request setHTTPBody: myRequestData]; 

    //store them in the dictionary 
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]]; 
    NSString *json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
    NSDictionary *questions = [json objectFromJSONString]; 
    [json release]; 

    [request release]; 
    return [questions autorelease]; 
} 

我到底做错了什么?

+1

你从我在你的其他问题留下的答案得到的代码。请接受它,如果它帮助你。我花了不少时间。 :/ –

+1

你做错了很多事情。将[super viewDidLoad]调用放置在方法的顶部,供初学者使用。您还将自动释放对象传递回您的方法,将其分配给另一个变量,然后释放它。当你做'questions = arr','[arr release]'时,你的问题对象指向同样的内存位置,这个位置现在是垃圾。也看看你的'myRequestString'初始化,你没有正确地启动它。 – Rog

回答

1

首先,你没有做任何事情,此代码:

NSMutableString* myRequestString = [[NSMutableString string]initWithFormat:@"section=%@", sectionId]; 
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]]; 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: url]]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; 
[request setHTTPMethod: @"POST"]; 
//post section 
[request setHTTPBody: myRequestData]; 

删除它。

其次,questions已经自动发布。

NSDictionary *questions = [json objectFromJSONString]; // < autoreleased 

因此,简单地做return questions;应该工作。

这也意味着你应该在不是的任何情况下释放这个返回值。因此,摆脱这一点:

[arr release]; 
+0

前几行是我用来将数据发布到数据库的内容。该程序在进行更改后仍然崩溃... – user559142

+0

您是否忽略了我稍后在答案中所说的内容?那是你的问题。 –

+0

我做了这些改变,它仍然崩溃... – user559142

1

您正在发布getQuestions返回的arr对象。由于您已将此对象标记为autorelease,因此您无需自行明确释放它。从viewDidLoad删除以下行,你应该设置:

[arr release]; 

此外,您myRequestString的嫌疑。你正在调用string类的方法,它返回一个完全分配和初始化的字符串,但你打电话initWithFormat,这通常是你只是alloc -ed的字符串。将该行替换为:

[[NSMutableString alloc]initWithFormat:@"section=%@", sectionId] 

然后在您的[request release]之后立即释放它。

+0

我已删除[arr发布],它仍然崩溃。 – user559142

+0

您可以发布堆栈跟踪吗? – highlycaffeinated

+0

原谅我的无知,但我该怎么做:S – user559142

1

从静态方法返回的NSDictionary是autoreleased。但是你在viewDidLoad方法中释放它。