此代码中有内存泄漏。自从2周以来我一直在破坏它,我开始疯了。 在此先感谢您的帮助:)
+(void) makeEvent:(int) event:(AppleEvent *)theEvent
{
int sig = 'dock';
OSErr err;
AEAddressDesc targetDesc;
targetDesc.descriptorType = typeNull;
targetDesc.dataHandle = nil;
err = AECreateDesc(
typeApplSignature,
&sig, sizeof(int),
&targetDesc
);
if(err) { NSLog(@"Error creating descriptor: %i\n", err); }
err = AECreateAppleEvent(
'DKoP', event,
&targetDesc,
kAutoGenerateReturnID, kAnyTransactionID,
theEvent
);
if(err) { NSLog(@"Error creating event: %i\n", err); }
AEDisposeDesc(&targetDesc);
targetDesc.descriptorType = typeNull;
targetDesc.dataHandle = nil;
}
addIntParm
消息:
+(void) addIntParm:(int) parm: (int) keyword: (AppleEvent *)theEvent
{
OSErr err = AEPutParamPtr(
theEvent, keyword,
typeSInt32, &parm, sizeof(parm)
);
if(err) { NSLog(@"Error setting parameter: %i\n", err); }
}
addFloatParm
消息:
+(void) addFloatParm:(float) parm: (int) keyword: (AppleEvent *)theEvent
{
OSErr err = AEPutParamPtr(
theEvent, keyword,
typeIEEE32BitFloatingPoint, &parm, sizeof(parm)
);
if(err) { NSLog(@"Error setting parameter: %i\n", err); }
}
sendEvent
消息:
+(void) sendEvent:(AppleEvent *)theEvent
{
OSErr err = AESend(
theEvent, nil, kAENoReply, //kAEWaitReply
kAENormalPriority, kNoTimeOut,
nil, nil
);
if(err) { NSLog(@"Error sending: %i\n", err); }
}
Test
消息:
+ (void) Test:(int)wid:(int)w:(int)h:(void*)points
{
AppleEvent theEvent;
[self makeEvent:'warp' :&theEvent];
[self addIntParm:wid :'wnid' :&theEvent];
[self addIntParm:w :'wwrp' :&theEvent];
[self addIntParm:h :'hwrp' :&theEvent];
[self addDataParm:points :sizeof(float)*4*h*w :'pots' :&theEvent];
[self sendEvent:&theEvent];
AEDisposeDesc(&theEvent);
}
答案 0 :(得分:1)
我的猜测是你永远不会破坏 AppleEvent *
对象。 AECreateAppleEvent
的文档说:
如果函数成功返回,则应用程序应该在完成使用后调用AEDisposeDesc函数来处理生成的Apple事件。
这个问题的一个线索是函数中有“Create”一词,(according to the Create Rule)表示你负责处理创建的内存。
(猜猜我应该在回答之前阅读所有发布的代码)
由于看起来你正在妥善处理这些东西,我会在你的代码上运行Leaks工具并确定实际泄漏的
。另外,正如@DarkDust指出的那样,你应该真正阅读Cocoa Naming Conventions文档。