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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:12:27+00:00 2026-05-11T09:12:27+00:00

I’m almost there understanding simple reference counting / memory management in Objective-C, however I’m

  • 0

I’m almost there understanding simple reference counting / memory management in Objective-C, however I’m having a difficult time with the following code. I’m releasing mutableDict (commented in the code below) and it’s causing detrimental behavior in my code. If I let the memory leak, it works as expected, but that’s clearly not the answer here. 😉 Would any of you more experienced folks be kind enough to point me in the right direction as how I can re-write any of this method to better handle my memory footprint? Mainly with how I’m managing NSMutableDictionary *mutableDict, as that is the big culprit here. I’d like to understand the problem, and not just copy/paste code — so some comments/feedback is ideal. Thanks all.

- (NSArray *)createArrayWithDictionaries:(NSString *)xmlDocument                                 withXPath:(NSString *)XPathStr {      NSError *theError = nil;     NSMutableArray *mutableArray = [[[NSMutableArray alloc] init] autorelease];     //NSMutableDictionary *mutableDict = [[NSMutableDictionary alloc] init];     CXMLDocument *theXMLDocument = [[[CXMLDocument alloc] initWithXMLString:xmlDocument options:0 error:&theError] retain];      NSArray *nodes = [theXMLDocument nodesForXPath:XPathStr error:&theError];     int i, j, cnt = [nodes count];     for(i=0; i < cnt; i++) {         CXMLElement *xmlElement = [nodes objectAtIndex:i];         if(nil != xmlElement) {             NSArray *attributes = [NSArray array];             attributes = [xmlElement attributes];             int attrCnt = [attributes count];             NSMutableDictionary *mutableDict = [[NSMutableDictionary alloc] init];             for(j = 0; j < attrCnt; j++) {                 if([[[attributes objectAtIndex:j] name] isKindOfClass:[NSString class]])                      [mutableDict setValue:[[attributes objectAtIndex:j] stringValue] forKey:[[attributes objectAtIndex:j] name]];                 else                      continue;             }             if(nil != mutableDict) {                 [mutableArray addObject:mutableDict];             }             [mutableDict release];  // This is causing bad things to happen.         }     }      return (NSArray *)mutableArray; } 
  • 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. 2026-05-11T09:12:27+00:00Added an answer on May 11, 2026 at 9:12 am

    Here’s an equivalent rewrite of your code:

    - (NSArray *)attributeDictionaries:(NSString *)xmlDocument withXPath:(NSString *)XPathStr {     NSError *theError = nil;     NSMutableArray *dictionaries = [NSMutableArray array];     CXMLDocument *theXMLDocument = [[CXMLDocument alloc] initWithXMLString:xmlDocument options:0 error:&theError];      NSArray *nodes = [theXMLDocument nodesForXPath:XPathStr error:&theError];      for (CXMLElement *xmlElement in nodes) {         NSArray *attributes = [xmlElement attributes];         NSMutableDictionary *attributeDictionary = [NSMutableDictionary dictionary];         for (CXMLNode *attribute in attributes) {             [attributeDictionary setObject:[attribute stringValue] forKey:[attribute name]];         }          [dictionaries addObject:attributeDictionary];     }      [theXMLDocument release];     return attributeDictionaries; } 

    Notice I only did reference counting on theXMLDocument. That’s because the arrays and dictionaries live beyond the scope of this method. The array and dictionary class methods create autoreleased instances of NSArray and NSMutableDictionary objects. If the caller doesn’t explicitly retain them, they’ll be automatically released on the next go-round of the application’s event loop.

    • I also removed code that was never going to be executed. The CXMLNode name method says it returns a string, so that test will always be true.
    • If mutableDict is nil, you have bigger problems. It’s better that it throws an exception than silently fail, so I did away with that test, too.
    • I also used the relatively new for enumeration syntax, which does away with your counter variables.
    • I renamed some variables and the method to be a little bit more Cocoa-ish. Cocoa is different from most languages in that it’s generally considered incorrect to use a verb like ‘create’ unless you specifically want to make the caller responsible for releasing whatever object you return.
    • You didn’t do anything with theError. You should either check it and report the error, or else pass in nil if you’re not going to check it. There’s no sense in making the app build an error object you’re not going to use.

    I hope this helps get you pointed in the right direction.

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

Sidebar

Ask A Question

Stats

  • Questions 181k
  • Answers 181k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer $("body").append(element); or $(element).appendTo("body"); May 12, 2026 at 4:15 pm
  • Editorial Team
    Editorial Team added an answer I found this explanation in the internet: Even with drag-and-drop… May 12, 2026 at 4:15 pm
  • Editorial Team
    Editorial Team added an answer Here is the google reader bundle for the blogs i… May 12, 2026 at 4:15 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.