Mac OS X 程序提供 EXEC_BAD_ACCESS
在我的模块之一中,我需要维护基于事件的机制。逻辑是:
发送事件:
-(void)addEvent:(EventData *)pData{
[self enQueueEvent:pData];
[[NSNotificationCenter defaultCenter]
postNotificationName:EVENT_NAME
object:nil ];
}
-(void)enQueueEvent:(EventData *)pData{
[pEventLock lock];
[self.pEventArray insertObject:(NSObject *)pData atIndex:0];
[pEventLock unlock];
}
收到事件时:
-(void)EventHandler: (NSNotification *) notification
{
[self log:@"event Handled"];
EventData *pData = [self deQueueEvent];
if(pData){
switch(pData->eModuleId){
case UI_EVENT:{
[UIController HandleUICallBack:(EventType)pData->eType LParam:pData->lParam WParam:pData->wParam];
}
}
pData->lParam = 0x00;
pData->wParam = 0x00;
free(pData);
// [pData release];
//[self removeProcessedEvent];
}
}
-(EventData *)deQueueEvent{
[pEventLock lock];
NSObject *pData = [self.pEventArray lastObject];
[self.pEventArray removeLastObject];
[pEventLock unlock];
return (EventData *)pData;
}
>>>>>>> Adding Header File Declaration >>>>>>>>>>>>>>>>>>>
typedef struct __eventData{
ModuleId eModuleId;
EventType eType;
void *lParam;
void *wParam;
}EventData;
@interface CommEventHandler : NSObject {
NSMutableArray *pEventArray;
bool shouldStartTimer;
int timerValue;
NSLock *pEventLock;
}
@property(nonatomic,retain)NSMutableArray *pEventArray;
@property(nonatomic,retain)NSLock *pEventLock;
<<<<<<<<<<<<<<<< End of Header File <<<<<<<<<<<<
我相信这很容易理解。现在发生的事情是,当我在 10.6 | 中运行程序时应用程序名称 |调试| i386
它运行得很好,但是当我在 10.6 | 中运行该程序时应用程序名称 |调试| X86_64
将节点插入事件数组时出错。谁能告诉我如何调试?我检查了所有内存方面,但没有找到任何东西。
In one of my modules I need to maintain event-based mechanism. The logic is:
To send the event:
-(void)addEvent:(EventData *)pData{
[self enQueueEvent:pData];
[[NSNotificationCenter defaultCenter]
postNotificationName:EVENT_NAME
object:nil ];
}
-(void)enQueueEvent:(EventData *)pData{
[pEventLock lock];
[self.pEventArray insertObject:(NSObject *)pData atIndex:0];
[pEventLock unlock];
}
When event received:
-(void)EventHandler: (NSNotification *) notification
{
[self log:@"event Handled"];
EventData *pData = [self deQueueEvent];
if(pData){
switch(pData->eModuleId){
case UI_EVENT:{
[UIController HandleUICallBack:(EventType)pData->eType LParam:pData->lParam WParam:pData->wParam];
}
}
pData->lParam = 0x00;
pData->wParam = 0x00;
free(pData);
// [pData release];
//[self removeProcessedEvent];
}
}
-(EventData *)deQueueEvent{
[pEventLock lock];
NSObject *pData = [self.pEventArray lastObject];
[self.pEventArray removeLastObject];
[pEventLock unlock];
return (EventData *)pData;
}
>>>>>>> Adding Header File Declaration >>>>>>>>>>>>>>>>>>>
typedef struct __eventData{
ModuleId eModuleId;
EventType eType;
void *lParam;
void *wParam;
}EventData;
@interface CommEventHandler : NSObject {
NSMutableArray *pEventArray;
bool shouldStartTimer;
int timerValue;
NSLock *pEventLock;
}
@property(nonatomic,retain)NSMutableArray *pEventArray;
@property(nonatomic,retain)NSLock *pEventLock;
<<<<<<<<<<<<<<<< End of Header File <<<<<<<<<<<<
I believe this is easy to understand. Now what is happening is that when I run the program in 10.6 | App Name | Debug | i386
it runs perfectly fine, but when I run the program in 10.6 | App Name | Debug | X86_64
it faults while inserting a node into the Event array. Can anyone give me a clue how to debug ? I checked all memory aspects, but failed to find anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
CommEventData
与EventData
相同,则两者都不是 Objective-C 类。因此,您不能将CommEventData *
类型的值添加到NSMutableArray
中,因为NSMutableArray
需要一个 Objective-C 对象 — 特别是一个 Objective-响应-retain
和-release
的 C 对象。如果添加的不是对象,无论(NSObject *)
类型转换如何,这都会崩溃。如果将
EventData
和CommEventData
转换为 Objective-C 类,则可以使用NSMutableArray
。否则,如果您想要存储指向非 Objective-C 对象的值的指针(例如指向
结构
的指针),请考虑使用NSPointerArray
。请注意,由于 NSPointerArray 允许任意(指向)值,因此它不会获取或释放其元素的所有权。If
CommEventData
is the same asEventData
, then both aren’t Objective-C classes. As such, you cannot add values of typeCommEventData *
to anNSMutableArray
becauseNSMutableArray
expects an Objective-C object — in particular, an Objective-C object that responds to both-retain
and-release
. This will crash if what’s being added is not an object regardless of the(NSObject *)
typecast.If you convert
EventData
andCommEventData
to Objective-C classes, you can useNSMutableArray
.Otherwise, if you want to store pointers to values that are not Objective-C objects (e.g. pointers to
structs
), consider usingNSPointerArray
instead. Note that sinceNSPointerArray
allows arbitrary (pointers to) values, it doesn’t take ownership of nor release its elements.