Question

I'm doing an iOS app that crawl my youtube subscription's videos. I have a problem when I want to navigate to see the next videos at the third time.

for these, I need to collect the start-index (NSNumber *youtubeStart) to add it the number of videos displayed (int const maxVideos).

For that, I have in my VideosViewController.h

@interface VideosViewController : UIViewController {
    NSNumber *youtubeStart;
}

@property (nonatomic, retain) NSNumber *youtubeStart;

Then in VideosViewController.m

@synthesize youtubeStart;
static int const maxVideos = 6;

And a method that do

- (void) navigateVideos:(id)sender
{
    int navigate = 0;
    int start = [youtubeStart intValue];

    if(sender == bt_prev)
    {
        if(start >= maxVideos)
        {
            start -= maxVideos;
            navigate = 1;
        }
    }

    if(sender == bt_next)
    {
        start += maxVideos;
        navigate = 1;
    }

    if (navigate > 0)
    {   
        youtubeStart = [NSNumber numberWithInt:start];
        NSString *url = [NSString
             stringWithFormat:@"%@&start-index=%i&max-results=%i"
             , myUrl, [youtubeStart intValue], maxVideos];
        [self LoadOAuthUrl:url];
    }
}

When I "TouchUpInside" one of the buttons "bt_prev" or "bt_next" it call "navigateVideos". I can press bt_prev or bt_next as many times as I want and it works but if I press bt_next 3 times in a row, youtubeStart become out of scope. It seems to not be able to go further than the third page.

Could you help me to understand why and how to handle it.

My others synthesized variables are not out of scope.

Thanks a lot for your help.

EDIT: I run the application on the ipad simulator from x-code. The application just crash with the EXC_BAD_ACCESS error. youtubeStart appear to be "out of scope" in the debugger just before "int start = [youtubeStart intValue];" and that's why it crash. When it works fine, youtubeStart is not out of scope.

EDIT 2: The stacktrace is always missing. I just got this in the debugger console:

Program received signal:  “EXC_BAD_ACCESS”.

And this in the debugger with a red arrow:

0x0134d0b0  <+0036>  cmp    ecx,DWORD PTR [eax]
Was it helpful?

Solution

This problem is caused by you not using properties properly. You should always use self.youtubeStart when you assign a new value to the youtubeStart property. This is so that the object can be retained by your property. When you just assign directly to the iVar the actual NSNumber object is not retained, which means by the time you access it later (like when you call start = [youtubeStart intValue]) the object may have been auto-released.

I'm guessing that the 'out of scope' error really means that the object has been deallocated.

Here's what I would recommend...

Remove this code at the @interface in VideosViewController.h:

{
    NSNumber *youtubeStart;
}

And change your @synthesize to this:

@synthesize youtubeStart = _youtubeStart;

Then you will get some compiler errors/warnings where you directly access youtubeStart from within your code. Fix these by changing the access to self.youtubeStart.

This is a really good practice for using properties because it makes sure you don't ever accidentally use the iVar directly. If you do want to use the iVar directly, you can use _youtubeStart (this iVar is created automatically by the @synthesize line). However I recommend not using it at all unless you fully understand how the accessors and retain etc. works.

Also, if you struggle with this stuff I highly recommend looking into ARC :)

OTHER TIPS

You failed to comply with Cocoa memory management rule. Your correct code should be

self.youtubeStart = [NSNumber numberWithInt:start];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top