• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Weak references and ConcurrentModificationException

 
Jane Jukowsky
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to maintain a weak list of dialogs, like this:



Every once in a while, I clear the old weak references from this list:



Problem is, I get an exception



I do understand what's happened... sort of... OK, maybe I don't... the garbage collection thread (or whatever) is clearing my weak reference as we are accessing it... However, since no other thread is modifying my list (only mutes its elements), how is this condition even detected? And what's the right way to maintain a weak collection of objects then?

 
Jane Jukowsky
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Turns out that weak references and multithreading have nothing to do with it; you simply can not delete an element of a collection while iterating through the same collection. Mod, feel free to delete.. or keep for a reference in case someone else encounters it.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jane Dodo wrote:Turns out that weak references and multithreading have nothing to do with it; you simply can not delete an element of a collection while iterating through the same collection. Mod, feel free to delete.. or keep for a reference in case someone else encounters it.

Actually you can, but you need to use the iterator to perform the deletion:

This also assumes that no other code is modifying the list while you do this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic