NSTimer won't stop

Discussion in 'iOS Development' started by boopyman, May 3, 2010.

  1. boopyman

    boopyman Member

    Joined:
    Mar 30, 2009
    Messages:
    188
    Likes Received:
    0
    Device:
    4G iPod touch
    Well, I'm still working on my SBSettings toggle, for which I've already resolved several problems. This latest one I find interesting, for the solutions I found on Internet don't affect the issue.
    I pretty much have an NSTimer that fires repeatedly, and even by invalidating it, releasing it, and setting it to nil, I can't stop it. Here's my code:

    [OBJC]@interface SomeView : UIView {
    NSTimer *loop;
    }

    - (void) run;
    - (void) stop;
    - (void) start;

    @end

    @implementation SomeView

    - (void) start {
    loop = [[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector

    Please Register or Log in to view images

    selector(run) userInfo:nil repeats:YES] retain];
    }

    - (void) run {

    NSLog(@"That's one!");

    }

    - (void) stop {

    [loop invalidate];
    [loop release];
    loop = nil;

    }

    @end

    void setState(BOOL Enable)
    {

    if (Enable == NO) {


    SomeView* View = [[SomeView alloc] init];
    [View stop];
    [View release];



    }
    }
    [/OBJC]

    So if you got through all of that code (I'm sorry it's a bit long), do you have any idea why this doesn't work? I found out on Internet that I have to retain the timer and then invalidate, but that doesn't help...
    Thanks in advance!

    PS: I know the stop method is being called, because I've tried it with an NSLog in it.
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    I'd suggest you brush up on memory management first.

    What you posted leaks every time -start is called (after the first time). You should check to see if the object in loop is nil. If it isn't, then take care of releasing the object. Only when the loop ivar is nil should you be setting any object to it.

    Your timer creating is also flawed. You are creating an autoreleased object and retaining it. Although not technically "wrong," and in some cases unavoidable, here, not only is it not ideal, it makes no sense.

    What you are attempting to invalidate and release in stop likely doesn't even exist.



    If you were making just a little app, this wouldn't be such a big deal, BUT since you are using an SBToggle that plugs into SBSettings, which in turn plugs into SpringBoard, memory management is crucial. (I even go so far to say that your memory management when working with anything that plugs into SpringBoard is more important than what you're actually doing in all cases.) You'd be be much, much better off not creating the loop ivar and working with the timer locally.



    EDIT: We also have [OBJC] tags that you can use instead of
    Code:
     tags to keep your code pretty and easy to read.  You'll catch more fish with an easier to read post.
     : )
  3. bddckr

    bddckr Active Member

    Joined:
    Dec 2, 2007
    Messages:
    1,434
    Likes Received:
    18
    Device:
    iPhone 4 (Black)
    And in addition to what Skylar said: You don't want to allocate an UIView just to use a timer...
  4. boopyman

    boopyman Member

    Joined:
    Mar 30, 2009
    Messages:
    188
    Likes Received:
    0
    Device:
    4G iPod touch
    I think I got what you mean for the timer creating: I should be doing this:
    [OBJC] loop = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector

    Please Register or Log in to view images

    selector(run) userInfo:nil repeats:YES];
    [/OBJC]

    When you say I should check if loop is nil, could I do something like this? Sorry if it's entirely wrong, memory management is not my talent at all...

    [OBJC]
    - (void) start {

    if (loop != nil) {

    loop = nil;
    }

    else if (loop == nil) {

    loop = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector

    Please Register or Log in to view images

    selector(run) userInfo:nil repeats:YES];



    }
    [/OBJC]

    I haven't tried doing this, but it seems I could take out the whole -stop funtion with this if statement. I'm almost sure it's wrong, so I didn't test it in SpringBoard.

    Thanks for the help!
  5. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Start with these two links. Then try correctly making a few basic apps, remaining fully aware of memory management. Make sure there are no leaks, etc. Once you have a handle on Objective-C programming (and not to sound insulting, but from your posts, you don't), and are able to make an efficient non leaking application, then you should get into the world of making things such as SBToggles (and not a moment before).

    Your previous coding experience is moot if you were not required to manage memory. Objective-C has no garbage collection, etc, and demands that you be absolutely on top of things and know exactly what each line of your code is doing.

    http://stackoverflow.com/questions/1053592/objective-c-class-vs-instance-methods
    http://www.lugaru.com/man/Scope.of.Variables.html

Share This Page