2

I am running a background thread to get data from a web service.

To get the settings needed for creating the URL I am using this method:

+(Settings *)getSettings
{
    Settings *settings = [[Settings alloc] init];
    NSString *path = [NSString stringWithFormat:@"Documents/Settings"];
    NSString *settingsPath = [NSHomeDirectory() stringByAppendingPathComponent:path];
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    if ([fileMgr fileExistsAtPath:settingsPath]) {
        NSDictionary *settingsDictionary = [NSDictionary dictionaryWithContentsOfFile:settingsPath];
        settings.username = [settingsDictionary objectForKey:@"username"];
        settings.module = [settingsDictionary objectForKey:@"module"];
        settings.websiteUrl = [settingsDictionary objectForKey:@"websiteUrl"];

    }else
    {
        settings.username =  @"";
        settings.module =  @"";
        settings.websiteUrl =  @"";
    }
    NSLog(@"Settings = u:%@ m:%@ w:%@",settings.username,settings.module,settings.websiteUrl);
    return settings;
}

This works fine in the debugger but when i run it on the device i get BAD_EXCESS on the NSLog or on any class that tries to use the data with the warning:

warning: Attempting to create USE_BLOCK_IN_FRAME variable with block that isn't in the frame.

This is the code works fine on the device if I step through the code.

Any ideas?

Edit: Backtrace -

#0  0x37a97eda in objc_retain ()
#1  0x37aa4be4 in objc_retainAutoreleasedReturnValue ()
#2  0x0000f84a in +[SettingData getSettings] (self=0x175e4, _cmd=0x11af9) at /Users/Jono/Dropbox/Uni/iOS5/Feedback At Tees/Feedback At Tees/SettingData.m:49
#3  0x0000fa86 in -[DataManager init] (self=0x3509e0, _cmd=0x30fd9f96) at /Users/Jono/Dropbox/Uni/iOS5/Feedback At Tees/Feedback At Tees/DataManager.m:19
#4  0x00003b46 in __35-[MasterViewController viewDidLoad]_block_invoke_0 (.block_descriptor=0x185f40) at /Users/Jono/Dropbox/Uni/iOS5/Feedback At Tees/Feedback At Tees/MasterViewController.m:70
#5  0x30b08d54 in _dispatch_call_block_and_release ()
#6  0x30b0b896 in _dispatch_worker_thread2 ()
#7  0x37a0e1ce in _pthread_wqthread ()
#8  0x37a0e0a4 in start_wqthread ()

I tried adding the following to the init method of settings and if i step through it is ok if i dont it crashes on the second NSLog with the error: message sent to deallocated instance 0x160d60

-(id)init
{
    if ((self=[super init]))
    {
        NSString *path = [NSString stringWithFormat:@"Documents/Settings"];
        NSString *settingsPath = [NSHomeDirectory() stringByAppendingPathComponent:path];
        NSFileManager *fileMgr = [NSFileManager defaultManager];
        if ([fileMgr fileExistsAtPath:settingsPath]) 
        {
            NSDictionary *settingsDictionary = [NSDictionary dictionaryWithContentsOfFile:settingsPath];
            self.username = [settingsDictionary objectForKey:@"username"];
            self.module = [settingsDictionary objectForKey:@"module"];
            self.websiteUrl = [settingsDictionary objectForKey:@"websiteUrl"];
            NSLog(@"Settings = u:%@ m:%@ w:%@",self.username,self.module,self.websiteUrl);
        }
        else
        {
            self.username =  @"";
            self.module =  @"";
            self.websiteUrl =  @"";
        }
    }
    NSLog(@"Settings = u:%@ m:%@ w:%@",self.username,self.module,self.websiteUrl);
    return self;
}

Screenshot: http://postimage.org/image/4vw6uq7pp/

Jonoh89
  • 35
  • 6

2 Answers2

2

First, the method should be retrieveSettings or downloadSettings or, just, settings, but not getSettings as get as a prefix has a very particular meaning regarding the argumentation of the method (pass by reference).

Secondly, there is nothing obviously wrong with that code (save for the leak of settings, but that won't cause this kind of a crash). (I see that ARC is enabled).

Finally, if you have a crash then post the backtrace. The error message you are seeing ("USE_BLOCK_IN_FRAME") indicates that the code you posted has nothing to do with the crash.


Odd crash, that. OK -- the warning is from the debugger and doesn't have anything to do with the actual error.

The scope boundary is often where the compiler will emit release and/or drain autorelease pools.

Which leads to the next question; how are

This is the code works fine on the device if I step through the code.

OK -- so, it doesn't work when you run at full speed and does work when running step-by-step. Smells like a concurrency crash. Can you post a screenshot of the actual crash? I.e. are you sure that particular backtrace is the backtrace of the crash and not where the debugger happened to be at the time of the crash?

(Yes, grasping at straws slightly -- this crash doesn't make sense with the evidence shown. I like puzzles.)


OK -- now we are getting somewhere. Sort of. It crashes outside the if's scope after successfully logging the same thing inside the if's scope.

How are username, module and websiteURL defined? Are they strong or, most appropriately, copy @property declarations?

bbum
  • 162,346
  • 23
  • 271
  • 359
  • Added what i think is the backtrace to my main question – Jonoh89 Apr 17 '12 at 17:03
  • The problem seems to be that no matter where I instantiate settings or try to set the values in the method above it is getting released but as I put in one of my comments I am using ARC. I used NSZombieEnabled and get [CFString retain]: message sent to deallocated instance anywhere I try to use values from settings. – Jonoh89 Apr 17 '12 at 19:17
  • First, rename the method because that is bothersome. Secondly, show the thing that calls the `settings` method. Finally, post that screenshot -- it is hard to imagine how the backtrace pasted could be the source of the crash. – bbum Apr 17 '12 at 19:19
  • With the NSLog in the method in my main post it is not even getting out of the init method – Jonoh89 Apr 17 '12 at 19:43
  • Important clue, though; the bogus BLOCK warning is gone. – bbum Apr 17 '12 at 20:27
  • only if NSZombieEnabled is on – Jonoh89 Apr 17 '12 at 20:28
  • It is fine I just set the properties with strings and comment out: NSString *path = [NSString stringWithFormat:@"Documents/Settings"]; to else statement. That code does work in another place in my code through through the same request – Jonoh89 Apr 17 '12 at 20:33
  • 1
    DONE! It was because they were assign in my settings class, changed the properties to copy and all works! Will have to read up on that as I dont understand the difference. I can go to sleep now haha so thanks a lot! – Jonoh89 Apr 17 '12 at 21:00
0

When I see EXC_BAD_ACCESS I think it's time to use NSZombieEnabled (or Zombies feature in Instruments if you can re-produce it on the simulator.

This thread has instructions on how to accomplish the task.

Community
  • 1
  • 1
DBD
  • 23,075
  • 12
  • 60
  • 84