开发者

Where is the leak?

开发者 https://www.devze.com 2023-01-19 00:51 出处:网络
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 :)

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*开发者_如何学C)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);
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消