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 6562067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:39:55+00:00 2026-05-25T13:39:55+00:00

I have the following code: int start = [html rangeOfString:@class=WordSection1>].location + 24; int end

  • 0

I have the following code:

    int start = [html rangeOfString:@"class=WordSection1>"].location + 24;
    int end = [html rangeOfString:@"<div class=\"endofsections\">"].location;
    self.parts = [[NSMutableArray alloc] init];

    NSString* startHtml = [html substringToIndex:start - 1];
    NSString* mainHtml = [html substringWithRange:NSMakeRange(start - 1, end - start - 1)];
    NSString* endHtml = [html substringFromIndex:end];
    // !! At this point we have the string in memory twice
    [html release];

    [self.parts addObject: startHtml];

    NSArray *splitHtml = [mainHtml componentsSeparatedByString:@"<p class=NumberedParagraph>"];
    //[mainHtml release]; <-- this causes bad access errors. Does the split do a copy or does it just create a new set of pointers but use the same memory?

    for(NSString* part in splitHtml){
        if (first){
            [self.parts addObject: part];
            first = NO;
        } else {
            [self.parts addObject: [NSString stringWithFormat:@"<p class=NumberedParagraph>%@", part]];
        }
     }

    [self.parts addObject:endHtml];

The issue with this is that html is about 20Mb. I split it in to startHtml, mainHtml and endHtml. After splitting it I then release html. However prior to this release all 4 NSStrings are in memory so the app is using an extra 40Mb or so.

I then split mainHtml and assign the substrings to an NSArray called splitHtml, this again means that they are stored in memory twice. I try to release mainHtml but this causes an EXC_BAD_ACCESS error.

Is there any way to get around this object being stored in memory twice before being released issue?

I plan to replace the for loop with a while loop that removes the processed NSStrings from splitHtml. The loop condition will be satisfied when splitHtml is empty. This is so that as the parts array consumes more memory the splitHtml array consumes less memory. Do I need to release each NSString or can I just remove it and have the array consume less memory as a whole?

Thanks,

Joe

  • 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-25T13:39:56+00:00Added an answer on May 25, 2026 at 1:39 pm

    Well.. you can’t release mainHtml because it is created as an autorelease object, so release will get called after your function is done and it will crash if the object is already released by then.

    You could try to create an extra function that splits the string and returns the array, perhaps with an own autorelease pool that you release after the function is run to make sure the strings are released.

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

Sidebar

Related Questions

Suppose I have following code package memoryleak; public class MemoryLeak { public static int
I have the following code: #include <iostream> using namespace std; class testing{ int test()
Let's say we have the following c++ code: int var1; __asm { mov var1,
I have the following code: Some functions: A::A(int i_a) {cout<<int Ctor\n;} //conversion constructor void
I have the following code in my controller: public ActionResult Details(int id) { var
I have the following fields: Inventory control (16 byte record) Product ID code (int
If I have the following code: MyType<int> anInstance = new MyType<int>(); Type type =
i have the following code: #include <stdio.h> int main(void) { float a[4] __attribute__((aligned(0x1000))) =
I have the following code: #include <string.h> int main(void) { char *buffer = NULL,
I have seen the following code: [DefaultValue(100)] [Description(Some descriptive field here)] public int MyProperty{...}

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.