在connectionDidFinishLoading中接收错误,EXC_BAD_ACCESS

发布于 2024-12-15 05:16:49 字数 3096 浏览 5 评论 0原文

我在 NSURLConnection 委托方法 connectionDidFinishLoading 执行时收到错误。有趣的是,它可以在模拟器上运行,但在物理设备上会崩溃。更有趣的是,只有当这一系列操作完成后,它才会崩溃,“

  • 运行应用程序
  • 显示推文!” (太棒了)
  • 按主页按钮
  • 双击主页按钮
  • 强制退出应用程序
  • 再次打开应用程序
  • 崩溃了!!!! ( :( )…继续崩溃,直到重新启动手机!

错误日志 :

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0xa0000008
Crashed Thread:  0

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   libobjc.A.dylib                 0x321befbc 0x321bb000 + 16316
1   Tittle-Tattle                   0x0002cf10 -[MapViewController connectionDidFinishLoading:] (MapViewController.m:108)
2   Foundation                      0x3316bc32 0x330a5000 + 814130
3   Foundation                      0x330c36e2 0x330a5000 + 124642
4   Foundation                      0x330c36ac 0x330a5000 + 124588
5   Foundation                      0x330c35ce 0x330a5000 + 124366
6   CFNetwork                       0x35e7689e 0x35e67000 + 63646
7   CFNetwork                       0x35e6b53e 0x35e67000 + 17726
8   CFNetwork                       0x35e6b632 0x35e67000 + 17970
9   CFNetwork                       0x35e6b23c 0x35e67000 + 16956
10  CFNetwork                       0x35e6b172 0x35e67000 + 16754
11  CoreFoundation                  0x34176afc 0x340e9000 + 580348
12  CoreFoundation                  0x341762c8 0x340e9000 + 578248
13  CoreFoundation                  0x3417506e 0x340e9000 + 573550
14  CoreFoundation                  0x340f84d6 0x340e9000 + 62678
15  CoreFoundation                  0x340f839e 0x340e9000 + 62366
16  GraphicsServices                0x3254dfc6 0x3254a000 + 16326
17  UIKit                           0x3734e73c 0x3731d000 + 202556
18  Tittle-Tattle                   0x000200e0 main (main.m:16)
19  Tittle-Tattle                   0x00020084 start + 32

代码 :

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    [urlConnection cancel];
    [urlConnection release];
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

    //Since we got new fresh data we shall put it in LatestLocationData entity in CoreData
    [self insertLastKnownLocationDataIntoCoreDataWith:responseString];

    //Test purpose only, See what we have got in CoreData
    [self fetchLastKnownLocationDataFromCoreData];

    NSDictionary *results = [responseString JSONValue];
    placesNearBy = [results objectForKey:@"results"];

    [responseString release];

    [self dropNearByPlacesAnnotationsFrom:placesNearBy];

}

问题:这可能是什么原因?

类似问题(不是我提出的!)之前曾问过,但没有人回答该问题:应用程序未在 iOS 5 中运行


到目前为止我的理解是,仅 EXE_BAD_ACCESS当您尝试访问尚未分配的内存地址或之前已分配但现在已释放的

内存地址时会发生这种情况:

嘿Firoze,这就是我的初始化方式。 NSURL连接

urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

I have been getting errors while NSURLConnection delegate method connectionDidFinishLoading executes. Interestingly it works on simulator but it crashes on physical device. More interestingly it crashes only when this sequence of operation is done,

  • Run App
  • Shows Tweet! (Awesome)
  • Press Home button
  • Double click Home button
  • Force quit app
  • Again open app
  • CRASHED!!!!! ( :( )…Keep crashing until you restart your phone!

ERROR LOG :

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0xa0000008
Crashed Thread:  0

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   libobjc.A.dylib                 0x321befbc 0x321bb000 + 16316
1   Tittle-Tattle                   0x0002cf10 -[MapViewController connectionDidFinishLoading:] (MapViewController.m:108)
2   Foundation                      0x3316bc32 0x330a5000 + 814130
3   Foundation                      0x330c36e2 0x330a5000 + 124642
4   Foundation                      0x330c36ac 0x330a5000 + 124588
5   Foundation                      0x330c35ce 0x330a5000 + 124366
6   CFNetwork                       0x35e7689e 0x35e67000 + 63646
7   CFNetwork                       0x35e6b53e 0x35e67000 + 17726
8   CFNetwork                       0x35e6b632 0x35e67000 + 17970
9   CFNetwork                       0x35e6b23c 0x35e67000 + 16956
10  CFNetwork                       0x35e6b172 0x35e67000 + 16754
11  CoreFoundation                  0x34176afc 0x340e9000 + 580348
12  CoreFoundation                  0x341762c8 0x340e9000 + 578248
13  CoreFoundation                  0x3417506e 0x340e9000 + 573550
14  CoreFoundation                  0x340f84d6 0x340e9000 + 62678
15  CoreFoundation                  0x340f839e 0x340e9000 + 62366
16  GraphicsServices                0x3254dfc6 0x3254a000 + 16326
17  UIKit                           0x3734e73c 0x3731d000 + 202556
18  Tittle-Tattle                   0x000200e0 main (main.m:16)
19  Tittle-Tattle                   0x00020084 start + 32

CODE :

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    [urlConnection cancel];
    [urlConnection release];
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

    //Since we got new fresh data we shall put it in LatestLocationData entity in CoreData
    [self insertLastKnownLocationDataIntoCoreDataWith:responseString];

    //Test purpose only, See what we have got in CoreData
    [self fetchLastKnownLocationDataFromCoreData];

    NSDictionary *results = [responseString JSONValue];
    placesNearBy = [results objectForKey:@"results"];

    [responseString release];

    [self dropNearByPlacesAnnotationsFrom:placesNearBy];

}

Question : What could be the possible reason for this?

Similar Question(Not by me!) previously asked but no one replied on that question : Application not running in iOS 5


My understanding so far is, EXE_BAD_ACCESS only happens when you try to access memory address which hasn't been allocated, Or previously allocated but now it's been released.

EDIT AFTER RESPONSE IN COMMENT :

Hey Firoze, This is how I am init NSURLConnection

urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

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

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

发布评论

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

评论(1

浮生未歇 2024-12-22 05:16:49

看看您的评论,我建议您对所有 ivars 使用 @property 声明。它们将减轻您必须执行的所有手动内存管理工作,这可能就是您的问题所在。

简单示例

YourClass.h

@interface YourClass
@property (nonatomic, retain) NSURLConnection *urlConnection;
// More ivars

// Method declations
@end

YourClass.m

@interface YourClass

@synthesize urlConnection = _urlConnection;

// The method where you instantiate urlConnection
{
    NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:request
                                                                     delegate:self];
    self.urlConnection = urlConnection;
    [urlConnection release]; urlConnection = nil;

    // Do whatever else you do here
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
    [self.urlConnection cancel];
    self.urlConnection = nil;     <- This takes care of releasing the ivar and setting it to nil so there is no dangerous hanging pointer

    // ... the rest of your method
}

[urlConnection cancel];
[urlConnection release];
// You need to clean up after yourself with any ivars you make
- (void)dealloc
{
    [_urlConnection release]; 
    // Release any other ivars
    [super dealloc];
}
@end

Looking at your comments I would suggest you use @property declarations for all of your ivars. They will alleviate all of the manual memory management you are having to do, which is probably where your problem lies.

Quick example

YourClass.h

@interface YourClass
@property (nonatomic, retain) NSURLConnection *urlConnection;
// More ivars

// Method declations
@end

YourClass.m

@interface YourClass

@synthesize urlConnection = _urlConnection;

// The method where you instantiate urlConnection
{
    NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:request
                                                                     delegate:self];
    self.urlConnection = urlConnection;
    [urlConnection release]; urlConnection = nil;

    // Do whatever else you do here
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
    [self.urlConnection cancel];
    self.urlConnection = nil;     <- This takes care of releasing the ivar and setting it to nil so there is no dangerous hanging pointer

    // ... the rest of your method
}

[urlConnection cancel];
[urlConnection release];
// You need to clean up after yourself with any ivars you make
- (void)dealloc
{
    [_urlConnection release]; 
    // Release any other ivars
    [super dealloc];
}
@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文