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

  • SEARCH
  • Home
  • 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 1108607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:06:37+00:00 2026-05-17T02:06:37+00:00

I have a program that reads a huge text file (line by line) and

  • 0

I have a program that reads a huge text file (line by line) and does some string operations with each line before writing the line into a database.

The program needed more and more memory so I figured that I might need to release the strings that I use. But it did not help. So I have put together the following code to test out what actually happens. With some trial and error I found out that when I do the drain on the autorelease pool it works.

I would like to know what I do. So I ask:

  • Why is the release not releasing memory?
  • Is there a better way to do this?

Here is my test program

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

int cnt = 0;

while (cnt < 1000000000) {
    NSMutableString *teststr = [[NSMutableString alloc] init];

    teststr = [NSString stringWithString:@"Dummy string just for the demo"];

    cnt++;
    if (cnt % 1000000 == 0) {
        printf("cnt=%i\n",cnt);
    }

    [teststr release];


//      [pool drain];                      // It works when I do this 
//      [[NSAutoreleasePool alloc] init];  // and this

}

[pool drain];
return 0;
}

EDIT: Based on the answers so far I have looked on my original program and changed the test program:

//teststr = [NSString stringWithString:@"Dummy string just for the demo"];

[teststr appendString:@"Dummy string just for the demo"];

Does this also create a new string? Because still I have the memory problem. My routine works in way that I append the string with something but maybe start with an empty string at the beginning.

  • 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-17T02:06:38+00:00Added an answer on May 17, 2026 at 2:06 am
    NSMutableString *teststr = [[NSMutableString alloc] init];
    

    This allocates a mutable string….

    teststr = [NSString stringWithString:@"Dummy string just for the demo"];
    

    and then this overrides the teststr variable with an autoreleased string. The allocated mutable string is now inaccessible, but still having a retain count of +1, so it will be leaked.

    [teststr release];
    

    This will only release the autoreleased string, causing double-free error in the future.


    If you want a manually-managed mutable string, you should use

    NSMutableString* teststr = [[NSMutableString alloc] initWithString:@"Dummy string just for the demo"];
    ...
    [teststr release];
    

    and don’t assign to teststr directly, before it is released or the ownership is transferred.

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

Sidebar

Related Questions

I have a program that reads in a file and does some parsing to
I have a program that reads data from a file line-by-line. I would like
I have a program that reads one image file, makes some changes on that
I have a small Java program that reads list of IPs from text file
I have a huge line-separated text file and I want to make some calculations
I have a program that reads from a txt file line by line splits
I have a program that reads from a file that grabs 4 bytes from
I have a program that reads input from a file. I am trying to
I have a program that reads a file, treat it and put the results
I want to have a program that reads metadata from an MP3 file. My

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.