accessing variables from a thread

Discussion in 'iOS Programming' started by irun5k, Sep 19, 2008.

  1. irun5k macrumors 6502

    Jan 14, 2005
    I'm having significant trouble accessing variables from inside a new thread.

    I have config data I need to access. It is stored as a "static NSString *myString" at the top of my .m file outside of any function. The value of myString is set in applicationDidFinishLaunching.

    My thread can't read myString, nor can it get to myString via a class getter function that returns myString.

    If I try to NSLog what I have from inside my executing thread, myString reports as "CALayer: 0x451f50" Clearly, I'm missing some aspect of NSThread programming. Does it run in some protected memory space or something and not have access to the "outside world"?

    Many thanks.
  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    When you see a global object that is supposed to be of a particular type to be of another type the usual explanation is that your code has failed to retain the object correctly. The object is created as autoreleased, and then assigned to a global (also an ivar). The object is then released when the autorelease pool is drained. The memory location then is reused and another object is placed there. So your string seems like it has transformed into a CALayer.

    Are you creating your string with a convenience method?
  3. irun5k thread starter macrumors 6502

    Jan 14, 2005
    Dude, thanks a million. Problem fixed in mere seconds. I was of course using a convenience method so the variable was getting autoreleased.

    Apparently I'm not yet mentally trained to think about autorelease when debugging code. I'm still intrigued by the concept that you have to be proactive and retain something or else a variable might have it's address space reclaimed. But then in the other case you have to remember to release your variables or they'll stick around beyond their welcome.

    Anyway, thanks again. I owe you one.

Share This Page