哪里漏了?

发布于 2024-09-25 21:37:56 字数 2524 浏览 1 评论 0原文

我在这段代码中存在内存泄漏。两周以来我就开始戒掉它,我开始生气了。 预先感谢您的帮助:)

+(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦醒灬来后我 2024-10-02 21:37:56

我的猜测是您永远不会销毁 AppleEvent * 对象。 AECreateAppleEvent 的文档说:

如果该函数成功返回,您的应用程序应在使用完该事件后调用 AEDisposeDesc 函数来处理生成的 Apple 事件。

对此的一个线索是该函数中包含“Create”一词,其中(根据创建规则)意味着您负责处置所创建的内存。

(猜猜我应该在回答之前阅读所有发布的代码)

因为看起来您正在正确处理东西,所以我会在您的代码上运行泄漏工具并识别实际泄漏的内容。

另外,正如 @DarkDust 指出的那样,您应该真正阅读 Cocoa 命名约定 文档。

My guess is that you're never destroying the AppleEvent * object. The documentation for AECreateAppleEvent says:

If the function returns successfully, your application should call the AEDisposeDesc function to dispose of the resulting Apple event after it has finished using it.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文