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

The Archive Base Latest Questions

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

Consider the 2 following methods of reading a string from a file: NSString *path

  • 0

Consider the 2 following methods of reading a string from a file:

NSString *path = [[NSBundle mainBundle] pathForResource:@'foo' ofType:@'txt']; NSString *string = [NSString stringWithContentsOfFile:path encoding:NSASCIIStringEncoding error:NULL]; 

NSString *path = [[NSBundle mainBundle] pathForResource:@'foo' ofType:@'txt']; NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:path]; NSData *data = [file readDataToEndOfFile]; NSString *string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; [file closeFile]; 

I would prefer to rely on method #1, but it behaves strangely when used in the following context:

NSString *string; // CLASS VARIABLE (void) setupView {   string = ...; // LOADING THE STRING } (void) drawView {  ...;  // USING THE STRING } 

In short, it’s an OpenGL-ES drawing-loop based on a NSTimer. The problem is that the string is accessible only at the first frame. At the next frame, the IPhone simulator (2.2) is crashing when trying to access the string.

Probably ‘something in my code, or in the OpenGL-ES code I’m using’ one would say… But how to explain the strange fact that if I use method #2 to load the string, everything works as intended?

Any clues?

  • 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-11T03:27:25+00:00Added an answer on May 11, 2026 at 3:27 am

    In Cocoa, you only own an object if you create it by using alloc, new, copy or retain. Whenever you don’t own an object, you have no guarantees about that object outside the local scope in which you use it. In your example, since you didn’t create the new string with any of the above mentioned methods, you have no guarantee that it will be around later if you store it.

    In order to take ownership of an object (i.e., that you want it to stick around so that you can use it later) that you don’t already own (i.e., you didn’t create it using one of the previously mentioned methods), you must send that object a retain message. For example, if you rewrite your code as shown below, you will own the string and you won’t have to worry about using it later:

    // option 1 NSString *path = [[NSBundle mainBundle] pathForResource:@'foo' ofType:@'txt']; NSString *string = [[NSString alloc] initWithContentsOfFile:path encoding:NSASCIIStringEncoding error:NULL];  // option 2 NSString *path = [[NSBundle mainBundle] pathForResource:@'foo' ofType:@'txt']; NSString *string = [NSString stringWithContentsOfFile:path encoding:NSASCIIStringEncoding error:NULL]; [string retain]; 

    Option 1 is okay because you specifically create the string with the alloc message. You now own the string object until you relinquish ownership at some other time. This is probably the best option for you, given your particular circumstances (i.e., you don’t really want to use the convenience constructor because you want the string object to hang around to be used later).

    Option 2 is okay because you specifically take ownership of the string by sending it the retain. In your case, this probably isn’t the best option because there is an alloc/init option available already and that’s usually easier to read.

    When you are done with an object and you want to relinquish ownership of the object, you send that object the release message. It’s important to remember that in Cocoa, sending an object a release message doesn’t mean that you’re destroying it, just that you’re relinquishing ownership.

    Cocoa memory management is designed around the idea of ownership of objects. Although it may be a little confusing at first, once you wrap your head around it, it makes it a lot easier to program in the environment without introducing memory errors, leaks or other bugs. The conveniences methods (like stringWithString) are really designed to be used when you want to create an object that you’re really only using for a brief period of time and within the scope of a single function or program block. If you plan to keep an object around past that scope, using the alloc/init or new methods to construct the object is preferred.

    For more information, please read the Memory Management Programming Guide for Cocoa. It really is considered required reading for Cocoa developers. Also, it usually takes reading it, experimenting, and reading it a few more times to really grasp, especially if you have a lot of experience with other models of memory management.

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

Sidebar

Ask A Question

Stats

  • Questions 212k
  • Answers 213k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer NewList = CurrentList -- Element when Element is a list… May 12, 2026 at 10:28 pm
  • Editorial Team
    Editorial Team added an answer Something like this should work well, I suppose: public static… May 12, 2026 at 10:28 pm
  • Editorial Team
    Editorial Team added an answer This vim plugin seems to do what you are after,… May 12, 2026 at 10:28 pm

Related Questions

Consider the following .Net ASMX web service with two web methods. using System; using
Consider the following class: public class MyIntSet { private List<int> _list = new List<int>();
I have a question about how python treats the methods passed to sorted(). Consider
Lots of developers think that testing private methods is a bad idea. However, all

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.