哪里漏了?
我在这段代码中存在内存泄漏。两周以来我就开始戒掉它,我开始生气了。 预先感谢您的帮助:)
+(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);
}
I have a memory leak in this code. I am busting it since 2 weeks and I am starting to be mad.
Thanks in advance for your help :)
+(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
message:
+(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
message:
+(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
message:
+(void) sendEvent:(AppleEvent *)theEvent
{
OSErr err = AESend(
theEvent, nil, kAENoReply, //kAEWaitReply
kAENormalPriority, kNoTimeOut,
nil, nil
);
if(err) { NSLog(@"Error sending: %i\n", err); }
}
Test
message:
+ (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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是您永远不会销毁AppleEvent *
对象。AECreateAppleEvent
的文档说:对此的一个线索是该函数中包含“Create”一词,其中(根据创建规则)意味着您负责处置所创建的内存。(猜猜我应该在回答之前阅读所有发布的代码)
因为看起来您正在正确处理东西,所以我会在您的代码上运行泄漏工具并识别实际泄漏的内容。
另外,正如 @DarkDust 指出的那样,您应该真正阅读 Cocoa 命名约定 文档。
My guess is that you're never destroying theAppleEvent *
object. The documentation forAECreateAppleEvent
says:A clue to this is that the function has the word "Create" in it, which (according to the Create Rule) means you are responsible for disposing of the memory created.(Guess I should read all the posted code before answering)
Since it appears you're properly disposing of stuff, I would run the Leaks instrument on your code and identify what is actually leaking.
Also, as @DarkDust points out, you should really read the Cocoa Naming Conventions documentation.