2010-05-10 18 views
6

फेंकता मैं सच में यकीन है कि क्यों मेरे कोड एक EXC_BAD_ACCESS फेंक है नहीं कर रहा हूँ, मैं एप्पल के दस्तावेज में दिशा-निर्देशों का पालन किया है:अतुल्यकालिक NSURLConnection EXC_BAD_ACCESS

-(void)getMessages:(NSString*)stream{ 

    NSString* myURL = [NSString stringWithFormat:@"http://www.someurl.com"]; 

    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:myURL]]; 

    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
    if (theConnection) { 
     receivedData = [[NSMutableData data] retain]; 
    } else { 
     NSLog(@"Connection Failed!"); 
    } 

} 

और मेरे प्रतिनिधि तरीकों

#pragma mark NSURLConnection Delegate Methods 
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    // This method is called when the server has determined that it 
    // has enough information to create the NSURLResponse. 

    // It can be called multiple times, for example in the case of a 
    // redirect, so each time we reset the data. 

    // receivedData is an instance variable declared elsewhere. 
    [receivedData setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    // Append the new data to receivedData. 
    // receivedData is an instance variable declared elsewhere. 
    [receivedData appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection 
    didFailWithError:(NSError *)error 
{ 
    // release the connection, and the data object 
    [connection release]; 
    // receivedData is declared as a method instance elsewhere 
    [receivedData release]; 

    // inform the user 
    NSLog(@"Connection failed! Error - %@ %@", 
      [error localizedDescription], 
      [[error userInfo] objectForKey:NSErrorFailingURLStringKey]); 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    // do something with the data 
    // receivedData is declared as a method instance elsewhere 
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]); 

    // release the connection, and the data object 
    [connection release]; 
    [receivedData release]; 
} 

मैं didReceiveData पर एक EXC_BAD_ACCESS। भले ही उस विधि में एक एनएसएलॉग शामिल है, मुझे त्रुटि मिलती है।

नोट: receivedData आप इसे अंदर कोड की didRecieveData पर त्रुटि मिल रहे हैं, भले ही ऐसा लगता है कि आपका प्रतिनिधि की तरह मुक्त कर दिया कर दिया गया है मेरे हेडर फाइल

+0

आपकी 'प्राप्त डेटा' चर शुरू करने वाली रेखा में 'डेटा' क्या है? – Mark

उत्तर

6

उपयोग NSZombieEnabled तोड़ने बिंदु और: आप इस के लिए थोड़ा getMessages बदलने के लिए की आवश्यकता होगी।

यह भी जांच:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    if ([response expectedContentLength] < 0) 
    { 
     NSLog(@"Connection error"); 
      //here cancel your connection. 
      [connection cancel]; 
     return; 
    } 
} 
+0

ज़ोंबी ब्रेकपॉइंट कहता है: *** - [एनएससीओनक्रेटेडटेबलडेटा लंबाई]: संदेश को हटाए गए संदेश 0xd4af700 पर भेजा गया संदेश यह सुनिश्चित नहीं करता कि कैसे निदान किया जाए ... –

+0

जो कनेक्शन में हो सकता है DidFinishLoading विधि, उस विधि से कोड को हटाने का प्रयास करें।यदि वह समस्या है जो आपके प्राप्तडेटा वैरिएबल समस्या है ... – Mark

+0

मैंने कनेक्शन में सभी कोड को टिप्पणी की हैडिफिनिश लोडिंग, और ऐप अब और क्रैश नहीं होता है। प्राप्त डेटा के साथ आपको क्या लगता है गलत है? –

2

में एक NSMutableData * है?

मैं जांचता हूं कि कनेक्शन प्राप्त करने से पहले उस वस्तु को getMessages विधि शामिल नहीं किया जा रहा है (या ऑटोरेलेज्ड) जारी नहीं किया जा रहा है।


संपादित करें: शो नीचे टिप्पणी है कि मेरे ऊपर जवाब गलत है :)

समस्या recievedData चर में था - यह जल्दी जारी किया जा रहा था। मार्क उस ऑब्जेक्ट की डेलोक विधि में इसे रिलीज़ करने का सुझाव देता है जो कनेक्शन बनाता है ताकि वह इसके लिए सभी क्रेडिट का हकदार हो!

वहां देखने के लिए एक मामूली चीज़ है - यदि आप डेलोक विधि में प्राप्त रिसीवडेटा जारी करते हैं, तो आप स्मृति को रिसाव करेंगे यदि आप एक से अधिक बार प्राप्त करते हैं। चेक जो मुक्त कर दिया वस्तु है

... 
if (theConnection) { 
    [recievedData release]; // If we've been here before, make sure it's freed. 
    receivedData = [[NSMutableData data] retain]; 
} else { 
... 
+0

इसके अलावा, 'didRecieveData' यानी कोड की कोई पंक्ति नहीं, बस एक खाली विधि में कुछ भी करने का प्रयास करें, जो यह निर्धारित करेगा कि क्या प्रतिनिधि को जारी किया जा रहा है और कुछ और नहीं – Mark

+0

यदि भी किया गया है ReceiveData में कोड की कोई पंक्ति नहीं है, तो मुझे वही मिलता है त्रुटि। प्रतिनिधि स्वयं ही है। –

+0

'didReceiveResponse' के साथ भी ऐसा ही करें, देखें कि क्या – Mark

5

मैं एप्पल के दस्तावेज में दिशा-निर्देशों का पालन किया है:

कि सच नहीं है। निम्न में से दोनों में, आप नियमों को तोड़ने:

- (void)connection:(NSURLConnection *)connection 
didFailWithError:(NSError *)error 
{ 
    // release the connection, and the data object 
    [connection release]; 
    // receivedData is declared as a method instance elsewhere 
    [receivedData release]; 

    // inform the user 
    NSLog(@"Connection failed! Error - %@ %@", 
      [error localizedDescription], 
      [[error userInfo] objectForKey:NSErrorFailingURLStringKey]); 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    // do something with the data 
    // receivedData is declared as a method instance elsewhere 
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]); 

    // release the connection, and the data object 
    [connection release]; 
    [receivedData release]; 
} 

दोनों ही मामलों में, आप नहींalloc साथ connection वस्तु प्राप्त करते हैं, एक विधि new के साथ शुरुआत या copy हैं। इन विधियों में आपके पास connection नहीं है। आपको इन तरीकों से इसे जारी नहीं करना चाहिए।

ऐसा लगता है कि आप वहां थोड़ी डोडी भी रिलीज कर रहे हैं। मेरा सुझाव है कि आप इसे रिलीज़ करने के तुरंत बाद इंस्टेंस वैरिएबल को शून्य पर सेट करें।

[receivedData release]; 
receivedData = nil; 

इस तरह, यह गलती से एक बार से अधिक मोर जारी नहीं किया जाएगा।

1

जेरेमीपी पर टिप्पणी करते हुए, जहां वह कहता है कि "दोनों में से, आप नियम तोड़ते हैं": शीहान आलम ऐप्पल के कोड (वास्तव में, कट'एनपेस्ट) का पालन कर रहा है here मिला।

मैं भी जोड़ना चाहूंगा (और यह ऐसा कुछ है जिसे अच्छी तरह से here का उत्तर नहीं दिया गया था) कि 'बिल्ड और विश्लेषण' एनएसआरएल कनेक्शन पर एक "संभावित रिसाव" झुकाता है (जिसे "[NSRLConnection आवंटन" ] ")। लेकिन यदि कोई NSRLConnection पर एक [कनेक्शन कनेक्शन] डालता है, तो उसी विधि में, यह क्रैश हो जाएगा।

तो हम .. कुछ है कि स्मृति प्रबंधन के लिए 'नियम' अवहेलना करने लगता है, फिर भी काम करता है (AFAIK) और एप्पल के दस्तावेज में है

2

मैं जब डिवाइस के साथ डिबगिंग हालांकि वहाँ कोई समस्या नहीं था एक ही त्रुटि मिली सिमुलेशन में

receivedData = nil; 
0

मैं NSURLConnection पर अतुल्यकालिक फोन पर EXC_BAD_ACCESS हो गया है: के बाद रिहा receivedData समस्या हल निम्न कोड पंक्ति जोड़ना।

receivedData = [[NSMutableData data] retain];

करने के लिए एक बनाए रखने के कोड से http://www.sudzc.com

मैं जोड़ने के लिए की जरूरत उत्पन्न होता है और कॉलबैक तरीकों अब और बुरा पहुँच संकेत नहीं मिलता है।

  • अगर मैं जोड़ने

    if ([response expectedContentLength] < 0) { NSLog(@"Connection error"); //here cancel your connection. [connection cancel]; return; }

मेरे सारे webservices रद्द कर की तुलना में, नहीं तो पूरी तरह से काम करता है।

0

हालांकि यह पूर्ण प्रश्न का उत्तर नहीं देता है, इसलिए मैंने इस त्रुटि को दो बार चलाया है क्योंकि मैंने अनुरोध की HTTPBody को एनएसडीटा के बजाय एनएसएसटींग में सेट किया है। एक्सकोड ने मुझे चेतावनी देने की कोशिश की।