views:

65

answers:

2

Whenever my method sets an object to onTouchEventReceiver, it gets lost when another method is called by another thread.

//In header
id onTouchEventReceiver;
SEL onTouchSelector;

- (void) setOnTouchSelector:(SEL)sel withObject:(id)obj
{
    NSLog(@"setting obj to %@",obj);
    onTouchSelector = sel;
    [self setOnTouchEventReceiver:obj];
    NSLog(@"====----- %@",onTouchEventReceiver); //That works
}

//Another thread calls this
- (void) touchEventReceived
{
    NSLog(@"firing a selector at %@ by %@",onTouchEventReceiver,self);

    //Why on earth does that happen?????

    if (onTouchEventReceiver != nil) //onTouchEventReceiver is (null)
    {
        [onTouchEventReceiver performSelector:onTouchSelector];
    }
}

The code produces the following:

2010-07-18 23:40:54.776 app[737:903] setting obj to <appCtl: 0x10fa00>
2010-07-18 23:40:54.787 app[737:903] ====----- <appCtl: 0x10fa00>

... after the screen was touched ...

Got touch event at coordinates 154 x 243 , mask : 2
2010-07-18 23:41:39.342 app[737:3b03] AALayer hit test passed : <AALayer: 0x110af0>
2010-07-18 23:41:39.348 app[737:3b03] firing a selector at (null) by <AALayer: 0x110af0>

Why does that happen? The code seems to be correct .

+2  A: 

The AALayer in your log message makes me think that it might be a subclass of CALayer. If this is the case, then you might easily have two separate objects. CALayers get duplicated for presentation by the rendering machinery. If this is the problem, you need to subclass -initWithLayer: to copy your extra ivars appropriately.

tjw
A: 

I think yo forgot the @property. Can you :

1/ Show me the property code

2/ Change this line to : NSLog(@"====----- %@",onTouchEventReceiver); //That works to NSLog(@"====----- %@", [self getOnTouchEventReceiver]); //That works

vodkhang