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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:49:24+00:00 2026-05-23T14:49:24+00:00

Just when I thought I had gotten the hang of Objective-C’s memory-management this error

  • 0

Just when I thought I had gotten the hang of Objective-C’s memory-management this error strikes me from nowhere…

Please consider the following code:

@implementation JglpNewsEntryParser


- (JglpNewsEntryParser *) initialize : (NSString *)content {
    self = [super init];
    if (self) {
        currentHeader = nil;
        currentText = nil;
        currentDate = nil;
        currentFullArticleUrl = nil;
        entries = [[NSMutableArray alloc] init];
        NSData *data = [content dataUsingEncoding: [NSString defaultCStringEncoding]];
        NSXMLParser *parser = [[[NSXMLParser alloc] initWithData:data] autorelease];
        [parser setDelegate: self];
        [parser parse];
    }
    return self;
}

- (void)parser: (NSXMLParser *)parser didStartElement: (NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName: (NSString *)qName attributes: (NSDictionary *)attributeDict {
    NSLog(@"Start!");
}

- (void)parser: (NSXMLParser *)parser didEndElement: (NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName: (NSString *)qName {
    NSLog(@"End!");
}

- (void)parser: (NSXMLParser *)parser foundCharacters: (NSString *)content {
    NSLog(@"Char!");
}

- (void)dealloc {
    [super dealloc];
    [entries release];
    entries = nil;
}

The class is used in my unit test in the following manner:

- (void) testConstruct {
    NSString *path = [[NSBundle bundleForClass:JglpNewsEntryParserTest.class] pathForResource: @"single-news-entry" ofType: @"html"];
    NSError *error = nil;
    NSString *data = [NSString stringWithContentsOfFile: path encoding: NSUTF8StringEncoding error: &error];
    JglpNewsEntryParser *parser = [[[JglpNewsEntryParser alloc] initialize: data] autorelease];
    STFail(@"");
}

After printing the “Start!”, “End!” and “Char!” messages once, since the text XML contains only one entry, the test fails as it is supposed to at STFail. However, I do receive the following memory error message afterwards:

malloc: *** error for object 0xedf434: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug

Constructing the data object in initialize seems set off the doomsday machine. If I uncomment it, the message disappears.

        // ...
        /*NSData *data = [content dataUsingEncoding: [NSString defaultCStringEncoding]];
        NSXMLParser *parser = [[[NSXMLParser alloc] initWithData:data] autorelease];
        [parser setDelegate: self];
        [parser parse];*/
    }
    return self;
}

Am I missing something when constructing the NSData object from the NSString?

Thanks for any suggestions and best regards
KC

  • 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-23T14:49:25+00:00Added an answer on May 23, 2026 at 2:49 pm

    Dealloc should do [super dealloc] after releasing all the instance variables:

    - (void)dealloc {
        [entries release];
        entries = nil;
        [super dealloc];
    }
    

    because it is dealloc in NSObject that actually frees the memory used by the object. When you set entries to nil, you are writing to a block of memory that has been freed already.


    Some other comments:

    It seems wrong to me that you are doing work in -init. I would recommend that you have a method (call it, say, -parse) that actually invokes the parser. At the moment you are performing quite complex ooperations on an object that isn’t guranteed to be fully initialised.

    Also, it’s best not to call your init method -initialize: to avoid confusion with +initialize which has a specific meaning in Cocoa. I’d call it -initWithContent:

    Also, your instance variables are initialised to nil before -init, so lines like

    ivar = nil;
    

    are pointless in init and you also need to release all your instance variables in dealloc.

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

Sidebar

Related Questions

I thought I had this figured out but it turns out I'm just deleting
Just when I thought I had my head wrapped around procs & lambdas this
This is just an idle thought I had when reading this other question: What
I am so frustrated. Just when I thought I had this figured out I
Just when I thought I had this one cinched, I realized that I can
I thought I had this one pretty good, but I just keep running into
Just when i thought I had everything figured out .. i got this problem.
Just wondering if you had any thoughts on this problem. I want to make
I probably just haven't thought this through, or perhaps I'm simply unaware of an
I just thought I had found my solution because the command works in 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.