微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

获取运行时异常

如何解决获取运行时异常

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

    pEvent->bPersistent = YES;                              // setup timer structure
    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:(stRs232Timer*)pEvent length:sizeof(stRs232Timer*)];

    wTimerId = 99;
    pEvent->uPeriod = 51;
    myData = [NSData dataWithBytes:(stRs232Timer*)pEvent length:sizeof(stRs232Timer*)];
         [m_cAppIdMap setobject:myData forKey:[NSNumber numberWithUnsignedShort:wTimerId]];
    wTimerId = 96;
    pEvent->uPeriod = 52;
    myData = [NSData dataWithBytes:(stRs232Timer*)pEvent length:sizeof(stRs232Timer*)];
    [m_cAppIdMap setobject:myData forKey:[NSNumber numberWithUnsignedShort:wTimerId]];
    wTimerId = 97;
    pEvent->uPeriod = 53;
    myData = [NSData dataWithBytes:(stRs232Timer*)pEvent length:sizeof(stRs232Timer*)];
    [m_cAppIdMap setobject:myData forKey:[NSNumber numberWithUnsignedShort:wTimerId]];
    wTimerId = 98;
    pEvent->uPeriod = 54;
    myData = [NSData dataWithBytes:(stRs232Timer*)pEvent length:sizeof(stRs232Timer*)];
    [m_cAppIdMap setobject:myData forKey:[NSNumber numberWithUnsignedShort:wTimerId]];
    wTimerId = 95;
    pEvent->uPeriod = 55;
    myData = [NSData dataWithBytes:(stRs232Timer*)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 ChangeTimer:95 withPeriod:10];

    free(pEvent);
    return YES;
}

-(BOOL)ChangeTimer:(unsigned short)wTimerIds withPeriod:(uint8_t)uPeriod
{
 stRs232Timer*   pEvent;
    NSLog(@\"Into the changeTimer\");
    pEvent = (stRs232Timer*)[m_cAppIdMap objectForKey:[NSNumber numberWithUnsignedShort:wTimerIds]];
    NSLog(@\"The val is:%@\",pEvent);
    if(pEvent!=nil){
        pEvent->bStopped = NO;
        pEvent->uPeriod = uPeriod;
        NSLog(@\"completed\");
        }       

    return YES;
}

main.m
----------
int main (int argc,const char * argv[]) {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSArray1* arr = [[NSArray1 alloc]init];
    [arr createTimer];
    [pool drain];
    return 0;
}
我收到EXC_BAD_ACCESs运行时异常。为什么会这样。我没有访问任何释放的内存。 编辑:
2011-05-30 11:21:45.191 NSArray[1091:a0f] bPersistent:1
2011-05-30 11:21:45.194 NSArray[1091:a0f] wAppTimerId:95
2011-05-30 11:21:45.195 NSArray[1091:a0f] uPeriod:50
2011-05-30 11:21:45.195 NSArray[1091:a0f] bStopped:0
2011-05-30 11:21:45.196 NSArray[1091:a0f] *** __NSAutoreleasenopool(): Object 0x100110180 of class NSConcreteData autoreleased with no pool in place - just leaking
2011-05-30 11:21:45.196 NSArray[1091:a0f] *** __NSAutoreleasenopool(): Object 0x1001102f0 of class NSConcreteData autoreleased with no pool in place - just leaking
2011-05-30 11:21:45.197 NSArray[1091:a0f] *** __NSAutoreleasenopool(): Object 0x100110410 of class NSCFNumber autoreleased with no pool in place - just leaking
2011-05-30 11:21:45.197 NSArray[1091:a0f] *** __NSAutoreleasenopool(): Object 0x100110a70 of class NSConcreteData autoreleased with no pool in place - just leaking
2011-05-30 11:21:45.198 NSArray[1091:a0f] *** __NSAutoreleasenopool(): Object 0x100110ab0 of class NSCFNumber autoreleased with no pool in place - just leaking
2011-05-30 11:21:45.198 NSArray[1091:a0f] *** __NSAutoreleasenopool(): Object 0x100110ad0 of class NSConcreteData autoreleased with no pool in place - just leaking
2011-05-30 11:21:45.199 NSArray[1091:a0f] *** __NSAutoreleasenopool(): Object 0x100110b10 of class NSCFNumber autoreleased with no pool in place - just leaking
2011-05-30 11:21:45.199 NSArray[1091:a0f] *** __NSAutoreleasenopool(): Object 0x100110b30 of class NSConcreteData autoreleased with no pool in place - just leaking
2011-05-30 11:21:45.200 NSArray[1091:a0f] *** __NSAutoreleasenopool(): Object 0x100110b70 of class NSCFNumber autoreleased with no pool in place - just leaking
2011-05-30 11:21:45.201 NSArray[1091:a0f] *** __NSAutoreleasenopool(): Object 0x1001109e0 of class NSConcreteData autoreleased with no pool in place - just leaking
2011-05-30 11:21:45.201 NSArray[1091:a0f] *** __NSAutoreleasenopool(): Object 0x1001105d0 of class NSCFNumber autoreleased with no pool in place - just leaking
2011-05-30 11:21:45.202 NSArray[1091:a0f] The dictionary count Now is:5
2011-05-30 11:21:45.202 NSArray[1091:a0f] The dictionary values Now is:
2011-05-30 11:21:45.203 NSArray[1091:a0f] {
    98 = <5f000136 000a0000>;
    97 = <5f000135 000a0000>;
    96 = <5f000134 000a0000>;
    99 = <5f000133 000a0000>;
    95 = <5f000137 000a0000>;
}
2011-05-30 11:21:45.203 NSArray[1091:a0f] EventList:(
    <5f000137 000a0000>
)
2011-05-30 11:21:45.204 NSArray[1091:a0f] Into the changeTimer
2011-05-30 11:21:45.204 NSArray[1091:a0f] *** __NSAutoreleasenopool(): Object 0x100111130 of class NSCFNumber autoreleased with no pool in place - just leaking
2011-05-30 11:21:45.205 NSArray[1091:a0f] The val is:<5f000137 000a0000>
2011-05-30 11:21:45.205 NSArray[1091:a0f] completed
这是删除NSAutoreleasePool和[poolrain]时得到的输出。 如果我没有同时删除这两个消息,我将得到EXC_BAD_ACCESS作为运行时异常。调试时,我遇到了[池排水]异常(EXC_BAD_ACCESS);声明。 我不知道为什么会这样。     

解决方法

        看这行(和类似的):
NSData* myData = [NSData dataWithBytes:(stRs232Timer*)pEvent length:sizeof(stRs232Timer*)];
字节数等于指向stRs232Timer数据的指针的大小,例如对于32位系统,该指针的大小为4个字节,对于64位系统为8个字节,但不是stRs232Timer结构的大小。因此,您复制的字节不足。 强制转换也不是必需的,因此应如下所示:
NSData* myData = [NSData dataWithBytes: pEvent length: sizeof(stRs232Timer)];
顺便说一句,这个第一个实例在保存到字典中之前将被覆盖。 后来 在
ChangeTimer
中,应使用
NSData bytes
方法,而不是直接转换为
stRs232Timer
pEvent = (stRs232Timer*)[[m_cAppIdMap objectForKey:[NSNumber numberWithUnsignedShort:wTimerIds]] bytes];
这将为您提供指向数据的指针,将NSData直接转换为
stRs232Timer
是错误的。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?