• 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: Garbage collection and reachable objects
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Garbage collection and reachable objects


  • Subject: Re: Garbage collection and reachable objects
  • From: Howard Moon <email@hidden>
  • Date: Thu, 2 Jul 2009 07:40:04 -0700


On Jul 2, 2009, at 6:56 AM, McLaughlin, Michael P. wrote:

Although I read the relevant documentation, I am still a bit unsure of what
is "reachable" by Xcode standards when garbage collection is *required*.


In particular, suppose (in a .mm source file)

1) I use a iterator, iter, to traverse an STL (non-Cocoa?) list, L.
2) I have a local struct, repeatedly overwritten, with one of its members
equal to such an iterator.
3) For some items in L, I save iter in the local struct and, later,
push_back (thus copy) this struct onto a more global STL list (or vector or
set or map).
4) I finish using iter to traverse L and iter goes out of (local) scope.


Are all saved instances of iter still reachable and thereby immune to
garbage collection?

Or, should I put a pointer in my struct and save the dereferenced iterator
as &(*it)? The latter seems kludgy and contrary to the intent but I've seen
errors that suggest that it might be necessary.


TIA for any help.


This isn't really an Xcode question, but a C++ question. In any case, a copy of an iterator is its own beast; it's in no way related to the original iterator. It's just like assigning the value of a pointer to another pointer. Just because the first pointer (or iterator) goes out of scope, that does not mean that the copy will be affected in any way. As long as the iterators are not invalidated, you're fine. (And I believe stl list iterators are fine in that respect, right?)


However, if you're getting errors, you might check to be sure that nothing is changing what they point to. I had a similar setup (at least it sounds similar), and what I did was change my original list to use pointers (to dynamically allocated objects) instead of objects. Then, in my other structures, I stored copies of the pointers extracted from the iterator. But I was able to do this only because I didn't really *need* iterators to the original list, just access to the data there. If you need the iterators themselves (in order to get the next/previous items, for example), then that obviously won't work for you.

Hope this helps,
	-Howard


_______________________________________________ Do not post admin requests to the list. They will be ignored. Xcode-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Garbage collection and reachable objects (From: "McLaughlin, Michael P." <email@hidden>)

  • Prev by Date: Garbage collection and reachable objects
  • Next by Date: Re: llvm link statistics
  • Previous by thread: Garbage collection and reachable objects
  • Next by thread: Disable auto-completion on return?
  • Index(es):
    • Date
    • Thread