开发者

getting the value from dictionary

开发者 https://www.devze.com 2023-03-08 07:35 出处:网络
stRs232Timer* pEvent; pEvent = (stRs232Timer*)[m_cAppIdMap objectForKey:[NSNumber numberWithUnsignedShort:wTimerIds]];
stRs232Timer* pEvent;
pEvent = (stRs232Timer*)[m_cAppIdMap objectForKey:[NSNumber numberWithUnsignedShort:wTimerIds]];

I am getting EXC_BAD_ACCESS error.Why is it so?

EDITED:

-(BOOL)createTimer
{
    stRs232Timer*   pEvent = malloc(sizeof(stRs232Timer));

    pEvent->bPersistent = YES;                              // setup timer structure
    //pEvent->pStack      = pStack;
    pEvent->wAppTimerId = 95;
    pEvent->uPeriod     = 50;
    pEvent->bStopped    = NO;
    pEvent->uExpirationTime = 10;

    NSLog(@"bPersistent:%d",pEvent->bPersistent);
    NSLog(@"wAppTimerId:%d",pEvent->wAppTimerId);
    NSLog(@"uPeriod:%d",pEvent->uPeriod);
    NSLog(@"bStopped:%d",pEvent->bStopped);


    NSData* myData = [NSData dataWithBytes:(void*)pEvent length:sizeof(stRs232Timer*)];

    wTimerId = 99;
    pEvent->uPeriod = 51;
    myData = [NSData dataWithBytes:(void*)pEvent length:sizeof(stRs232Timer*)];
    [m_cAppIdMap setObject:myData forKey:[NSNumber numberWithUnsignedShort:wTimerId]];
    wTimerId = 96;
    pEvent->uPeriod = 52;
    myData = [NSData dataWithBytes:(void*)pEvent length:sizeof(stRs232Timer*)];
    [m_cAppIdMap setObject:myData forKey:[NSNumber numberWithUnsignedShort:wTimerId]];
    wTimerId = 97;
    pEvent->uPeriod = 53;
    myData = [NSData dataWithBytes:(void*)pEvent length:sizeof(stRs232Timer*)];
    [m_cAppIdMap setObject:myData forKey:[NSNumber numberWithUnsignedShort:wTimerId]];
    wTimerId = 98;
    pEvent->uPeriod = 54;
    myData = [NSData dataWithBytes:(void*)pEvent length:sizeof(stRs232Timer*)];
    [m_cAppIdMap setObject:myData forKey:[NSNumber numberWithUnsignedShort:wTimerI开发者_如何转开发d]];
    wTimerId = 95;
    pEvent->uPeriod = 55;
    myData = [NSData dataWithBytes:(void*)pEvent length:sizeof(stRs232Timer*)];
    [m_cAppIdMap setObject:myData forKey:[NSNumber numberWithUnsignedShort:wTimerId]];

    NSLog(@"The dictionary count now is:%d",[m_cAppIdMap count]);
    NSLog(@"The dictionary values now is:");
    NSLog(@"%@",m_cAppIdMap);


    [m_cPendingEventList addObject:myData];
    NSLog(@"EventList:%@",m_cPendingEventList);

    [self StopTimer:99];

return YES;
}

-(BOOL)StopTimer:(unsigned short)wTimerIds
{
    NSLog(@"Into the StopTimer!!");
    stRs232Timer* pEvent;
    BOOL bReturn = NO;
    pEvent = (stRs232Timer*)[m_cAppIdMap objectForKey:[NSNumber numberWithUnsignedShort:wTimerIds]];

    if ([theLock tryLock]) {
        int cnts = [m_cAppIdMap count];
        NSLog(@"The count is:%d",cnts);
           if(pEvent!=nil)
            {
                pEvent->bStopped = YES;
                [m_cAppIdMap removeObjectForKey:[NSNumber numberWithUnsignedShort:wTimerIds]];
                bReturn = YES;
            }
    }
    NSLog(@"My Dictionary value now is:%@",m_cAppIdMap);
    [theLock unlock];
    return bReturn;
}

I debugged the code to see where its causing this exception.[m_cAppIdMap removeObjectForKey:[NSNumber numberWithUnsignedShort:wTimerIds]]; inside the stopTimer.It comes out.

stRs232Timer is a pointer to the structure that is stored onto the NSDictionary as an NSObject using NSData.


Are you sure it's that code that's crashing? If wTimerIds is an unsigned short, then the dictionary will return nil if the key doesn't exist. Even if the dictionary is not defined, sending a message to a nil object is valid and so would not cause a crash.

Therefore, I think either: 1. The crash is elsewhere 2. wTimerIds is not an unsigned short


m_cAppIdMap may have been released prematurely. You can run the Profiler (use the Zombies tool) and if that is the case you will get an error saying that a message has been sent to a released object when it tries to call objectForKey

0

精彩评论

暂无评论...
验证码 换一张
取 消