Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6081601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:11:40+00:00 2026-05-23T11:11:40+00:00

OK, I have a memory management problem that is driving me up a wall.

  • 0

OK, I have a memory management problem that is driving me up a wall. At one point I swear this worked with no problems, but now it’s leaking memory everywhere and I can’t figure out why.

To begin with I’m starting an NSTask and then running a loop while the task is running.

 NSTask *encodingTask = [[NSTask alloc] init];
            NSFileHandle *taskStdout = [NSFileHandle fileHandleForWritingAtPath:encodingOutput];
            [encodingTask setStandardOutput:taskStdout];
            [encodingTask setStandardError:taskStdout];
NSString argString = [NSString stingWithString: @"some arguments"];
[encodingTask setArguments:taskArgs];
            [encodingTask setLaunchPath:somePath];
            [encodingTask launch];

while ([encodingTask isRunning]){
                sleep(1);
                [self encodeProgressTimer];
            }

The encodeProgessTimer method is grabbing the last line from the stdOut and placing that in the menu bar:

- (void)encodeProgressTimer
{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"menuProgress"]) {
    // Read the last line
 NSString *fileData = [NSString stringWithContentsOfFile:encodingOutput encoding:NSASCIIStringEncoding error:nil];
    NSArray *lines = [fileData componentsSeparatedByString:@"\r"];
    NSString *lastLine = [lines objectAtIndex:[lines count] - 1];
    NSString *percent;
    NSString *eta;
    BOOL dataFound = NO;
    if ([lastLine length] == 71) {
        dataFound = YES;
        percentRange = (NSRange) {23,5};
        etaRange = (NSRange) {61,9};
        percent = [lastLine substringWithRange:percentRange];
        eta = [lastLine substringWithRange:etaRange];
    }
    else if ([lastLine length] == 72) {
        dataFound = YES;
        percentRange = (NSRange) {23,5};
        etaRange = (NSRange) {62,9};
        percent = [lastLine substringWithRange:percentRange];
        eta = [lastLine substringWithRange:etaRange];
    }
    else if ([lastLine length] == 70) {
        dataFound = YES;
        percentRange = (NSRange) {23,5};
        etaRange = (NSRange) {60,9};
        percent = [lastLine substringWithRange:percentRange];
        eta = [lastLine substringWithRange:etaRange];
    }

    if (dataFound) {
        NSMutableString *bottomStr = [[NSMutableString alloc] 
                                  initWithFormat:@"Encoding: %@%% - ETA %@", percent, eta];
                [appDelegate setMenuTop:topString andBottom:bottomStr];
        [bottomStr release];
    }

}

}

It’s my understanding that anything I’m not specifically allocating and initializing should be auto released when the method has completed, but that isn’t the case. Memory usage goes up exponentially every second when this is called. If I look at my memory allocations the number of living CFstings goes through the roof. If I turn of encodeProgressTimer my problems go away. I tried adding an autorelease pool to encodeProgressTimer which made memory usage very stable, however after 20 minutes or so of running I get a EXC_BAD_ACCESS. Turning on Zombies turns that into:

*** -[NSConcreteAttributedString _drawCenteredVerticallyInRect:scrollable:]: message sent to deallocated instance 0x2bc756e0

I actually went through and changed each variable declaration into it’s alloc/init counterpart and manually released them, but that didn’t solve the problem either. At this point I’m pretty stumped.

Also for the sake of completeness the [appDelegate setMenuTop: andBottom:] method looks like this:

-(void) setMenuTop: (NSString *) top andBottom: (NSString *) bottom
{  
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"menuProgress"]) {
    [statusItem setImage:nil];
    NSMutableParagraphStyle *lineHeight = [[NSMutableParagraphStyle alloc] init];
    [lineHeight setMaximumLineHeight:10.5];
    [lineHeight setLineBreakMode:NSLineBreakByTruncatingMiddle];
    OperationQueue *opQueue = [OperationQueue sharedQueue];
    NSString *sBuffer = [[NSMutableString alloc] initWithFormat: @"%@ (%i More)\n%@", top, [opQueue queueCount] - 1, bottom];
    attributes = [[NSDictionary alloc] initWithObjectsAndKeys:[NSFont menuFontOfSize:9], NSFontAttributeName, lineHeight, NSParagraphStyleAttributeName, nil];
    if (statusTitle)
        [statusTitle release];
    statusTitle = [[NSAttributedString alloc] initWithString: sBuffer  attributes: attributes];
    [statusItem setAttributedTitle: statusTitle];
    [lineHeight release];
    [sBuffer release];
    [attributes release];
    }

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:11:41+00:00Added an answer on May 23, 2026 at 11:11 am

    There will be loads of stuff in the autorelease pool, but you need to explicitly drain it for the memory to go away. Change your while loop as follows:

    while ([encodingTask isRunning]){
       NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
       sleep(1);
       [self encodeProgressTimer];
       [pool drain];
    }
    

    Other stuff: if you are running this on a thread, you can’t update user interface items directly. You need to use something like performSelectorOnMainThread: to actualy update the UI. If you are not running this on a thread, you need to rethink your design. The whole UI of your application will freeze while the loop is running.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this huge problem with memory management. The problem : I've got a
I have a C++ memory management doubt, that's (obviously) related to references and pointers.
i thought i getting the hang of Cocoa memory management, but apperently i have
This has been driving me nuts all day. I have a weird bug that
I have a problem regarding the memory management when adding objects to a NSMutableArray
I have a little problem with memory management in a Windows Service written in
Yes, I know that my problem is not the unique, but I have analysed
one thing that i always wondered, if i have a method like this: String
I have a memory management problem I can't get my head around on iOS.
here's a problem with memory management issue. Say i have a view A, it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.