• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSString becoming invalid
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSString becoming invalid


  • Subject: Re: NSString becoming invalid
  • From: Michael Ash <email@hidden>
  • Date: Thu, 11 Jun 2009 11:29:33 -0400

On Thu, Jun 11, 2009 at 9:53 AM, McLaughlin, Michael P.<email@hidden> wrote:
> In an Xcode (3.1.2) Cocoa document app targeting 10.5, I read an NSString
> from an XML file and store it, eventually, in a C++ struct as
>
> NSString *fname;
>
> This works most of the time but, sporadically, I get a BAD_ACCESS crash with
> an indication that fname has become invalid.  The only explanation I have
> found for this is that fname was not sufficiently retained and has been
> garbage-collected.
>
> Note: All my C++ files have a .mm extension and garbage collection is
> *required*.  Thus, I have no retain/release calls at all.
>
> Is there something more I need to do or is there some other reason why this
> might be happening?

The collector does not watch all memory, but only the bits that it has
been informed will hold object pointers. This includes the stack,
global variables, and parts of the heap that were allocated from the
collector's pool. Heap memory also needs a write barrier when
assigning.

Thus, you must allocate this struct using NSAllocateCollectable and
pass the appropriate options to get scanned memory if you are going to
store an ObjC object pointer in that struct. If you store a pointer to
this struct in the heap, that memory must ALSO come from a collectable
area and the pointer must be declared as __strong.

As an alternative, you can CFRetain the NSString, which will remove it
from being eligible for collection. You will have to balance this with
a CFRelease when you're finished with it, of course.

As another alternative, make this struct an ObjC object instead, and
all the details will be handled for you.

Mike
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >NSString becoming invalid (From: "McLaughlin, Michael P." <email@hidden>)

  • Prev by Date: NSSearchField that shows search results
  • Next by Date: Re: Doc kind from extension
  • Previous by thread: NSString becoming invalid
  • Next by thread: NSSearchField that shows search results
  • Index(es):
    • Date
    • Thread