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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:25:48+00:00 2026-05-24T23:25:48+00:00

In an IPhone app I made a constants file to hold my global variables:

  • 0

In an IPhone app I made a constants file to hold my global variables:

MyConstants.h
extern NSMutableString * MY_GATEWAY;


MyConstants.m
NSMutableString * MY_GATEWAY;

In my app delegate I have imported MyConstants.h, and can successfully append to MY_GATEWAY and initialize my global variable with a url like this:

 MY_GATEWAY = [NSMutableString stringWithString:MY_PROTOCOL];
[MY_GATEWAY appendString:MY_HOST];

From my app delegate, after initializing MY_GATEWAY, I launch a new thread. My thread class also imports MyConstants.h and in the main loop of that thread I try to read MY_GATEWAY but the value is garbage:

2011-08-13 22:23:47.246 MyProject[930:5c03] da.lproj

Should I be able to read this variable from a secondary thread? If so, am I doing something wrong?

  • 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-24T23:25:49+00:00Added an answer on May 24, 2026 at 11:25 pm

    Your mistake is actually a classic memory allocation error. This:

    MY_GATEWAY = [NSMutableString stringWithString:MY_PROTOCOL];
    

    Creates an autoreleased string and sets MY_GATEWAY to point to it. You don’t actually own the string, all you’re guaranteed is that it’ll last at least as long as the call stack. So autoreleased objects are typically used to return results from functions or to create temporary objects without having explicitly to worry about ownership.

    By the time your second thread gets around to accessing MY_GATEWAY, the original string has been deallocated and just by coincidence some other object has been put at the same address.

    What you want to do is:

    MY_GATEWAY = [[NSMutableString alloc] initWithString:MY_PROTOCOL];
    

    Any call with new, alloc, retain or create in the name gives you an owning reference. When you have an owning reference, you know for certain that the object will hang around until you explicitly release it.

    The official guide to the rules governing memory management is here. This blog post is also quite useful.

    If you’re not into worrying about memory management, Apple have announced that the version of the tools for iOS 5 will be able to handle most of these issues for you. The specifics are under NDA but if you sign in to Apple’s developer site with your developer programme account then you can get betas of the next releases of the tools and relevant documentation. Look out for references to Automatic Reference Counting (or ARC).

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

Sidebar

Related Questions

I have made an app that implements the iPhone's camera. When the user finishes
I have made an iPhone app with localized versions. It mostly works fine, but
I got a iPhone app design made in Photoshop. I converted the PSD file
I have made the mistake when starting the coding an iPhone App of not
So I made my iPhone app universal, it created a new mainWindow xib file
I have made an iPhone app building with SDK 3, expecting it to run
I have an iPhone app that is using CoreData. I recently made some minor
Up until yesterday I had a perfectly working app on my iPhone. I made
Can an iPhone app be made to contain another app? I am not talking
Here is how my current iPhone app is made: UINavigationControllerA [viewA1 viewA2 viewA3] viewA3

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.