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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:01:40+00:00 2026-05-16T12:01:40+00:00

I’m stuck with the following bit of code. NSString *gridRef = [[NSString alloc] initWithFormat:

  • 0

I’m stuck with the following bit of code.

NSString *gridRef = [[NSString alloc] initWithFormat: @"%@", [converter LatLongToOSGrid: latLong]];
NSLog(@"Grid Ref: %@", gridRef);
self.answerLabel.text = [[NSString alloc] initWithFormat: @"%@", gridRef];

When I log gridRef, it displays the correct result. However, the line setting answerLabel.text causes an EXC_BAD_ACCESS error and the program crashes. IB is connected to the correct label, what is the problem?

Thanks


I’ve updated the code as follows:

    - (IBAction)convertLatLong {
    NSArray *latLong = [[NSArray alloc] initWithObjects: latTextField.text, longTextField.text, nil];

    GridRefsConverter *converter = [[GridRefsConverter alloc] init];

    NSString *gridRef = [[NSString alloc] initWithFormat: @"%@", [converter LatLongToOSGrid: latLong]];
    NSLog(@"Grid Ref: %@", gridRef);
    NSLog(@"Label: %@", self.answerLabel.text);
    answerLabel.text = @"Yippy";
    self.answerLabel.text = gridRef;

    [gridRef release];
    [converter release];
    [latLong release];
}

answerLabel is initialised through @property @synthesize when the view controller is pushed onto the stack. (I don’t know how it gets init’d apart from it’s one of the magical things IB does for you. Or so I assume. I’ve used exactly the same method in other view controllers and have not had this issue.


I’ve found the culprits – the question is, how do I go about releasing them?

NSString *eString = [[NSString alloc] initWithFormat: @"%f", e];
NSString *nString = [[NSString alloc] initWithFormat: @"%f", n];
eString = [eString stringByPaddingToLength: (digits/2) withString: @"0" startingAtIndex: 0];
nString = [nString stringByPaddingToLength: (digits/2) withString: @"0" startingAtIndex: 0];
NSString *theGridRef = [letterPair stringByAppendingString: eString];
theGridRef = [theGridRef stringByAppendingString: nString];
[eString release];
[nString release];

return theGridRef;

and:

NSArray *gridRef = [[NSArray alloc] init];
gridRef = [gridRef arrayByAddingObject: [NSNumber numberWithDouble: E]];
gridRef = [gridRef arrayByAddingObject: [NSNumber numberWithDouble: N]];
gridRef = [gridRef arrayByAddingObject: [NSNumber numberWithInteger: 8]];

NSString *theGridRef = [[NSString alloc] initWithFormat: @"%@", [self gridRefNumberToLetter: gridRef]];

[gridRef release];
[theGridRef autorelease];

return theGridRef;

}

  • 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-16T12:01:41+00:00Added an answer on May 16, 2026 at 12:01 pm

    You should enable zombie detection by setting the environment variable NSZombieEnabled to YES, so you can see which object causes the bad access (don’t forget to remove this again when you found the bug).

    Also you can use Instruments to find the location where the object actually gets released. For this start a new Instruments session and use the “Allocations” instrument. In the instrument settings check “Enable NSZombie detection” and “Record reference counts”. When running the session you will break where the error occurs and you see a record of all retains/releases.

    One place where you can have a quick look if your object is incorrectly freed is in the -viewDidUnload method, where you should release the outlet and set it to nil. If you forget the latter and you access the outlet somehow, it will result in a EXC_BAD_ACCESS.

    Edited to match your update:

    The problem is that you are assigning eString (and nString) a new string which was alloc/init-ed. Then you override those in the next statements, because -stringByPaddingToLength: (as well as all the other -stringBy... methods) return a new and autoreleased string object. So you lost the reference to the old string which means that there is a memory leak. Additionally at the end you release the already autoreleased objects explicitly which causes your bad access.

    Instead you should create autoreleased strings from the beginning ([NSString stringWithFormat:…]) and don’t release them at the end.

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

Sidebar

Ask A Question

Stats

  • Questions 506k
  • Answers 506k
  • 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 From the OpenGL FAQ: 21.070 How do texture objects work?… May 16, 2026 at 3:45 pm
  • Editorial Team
    Editorial Team added an answer Those are instance methods, so you need to call them… May 16, 2026 at 3:45 pm
  • Editorial Team
    Editorial Team added an answer Use this instead. <% @post.step_names.each do |step| %> Step: <%=… May 16, 2026 at 3:45 pm

Trending Tags

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

Top Members

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I am trying to understand how to use SyndicationItem to display feed which is
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.