[ 
https://issues.apache.org/jira/browse/SANDBOX-433?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13424548#comment-13424548
 ] 

Benedikt Ritter commented on SANDBOX-433:
-----------------------------------------

I had a look at the code and it appears that the problem results from our 
caching implementation in AccessibleObjectsRegistry and 
PropertyDescriptorRegistry. Both classes use something like:

{code:java}
private final Map<AccessibleObjectDescriptor, Reference<AO>> cache = 
   WeakHashMap<AccessibleObjectDescriptor, WeakReference<AO>>();
{code}

Now I'm not an expert when it comes to references, but judging from the JavaDoc 
of 
[WeakHashMap|http://docs.oracle.com/javase/6/docs/api/java/util/WeakHashMap.html]
 and 
[WeakReference|http://docs.oracle.com/javase/6/docs/api/java/lang/ref/WeakReference.html]
 we have created a cache that will delete hole entries as their keys expire 
from the point of view of the WeakHashMap. Also values can be cleaned up, when 
the JVM determines it. That leaves broken entries in the cache, that have a key 
but null as a value. Later on when we query for a key we will get a null 
reference resulting in an NullPointerException.

I have changed the implementation to use 
[SoftReferences|http://docs.oracle.com/javase/6/docs/api/java/lang/ref/SoftReference.html]
 and that works. However it may only work in my test scenario and throw an NPE 
when one calls a method more often (than 50.000 times).
As I said at the beginning, I'm no expert when it comes to java.lang.ref. I 
would say that there is no need for a reference at all. We could simply change 
the initialization to:

{code:java}
private final Map<AccessibleObjectDescriptor, AO> cache = 
   WeakHashMap<AccessibleObjectDescriptor, AO>();
{code}

The WeakHashMap will take care of cleaning up references that are not longer in 
use.
If nobody objects, I can create a patch for this.

Regards,
Benedikt
                
> [BeanUtils2] Setting properties or calling methods very often results in a 
> NullPointerException
> -----------------------------------------------------------------------------------------------
>
>                 Key: SANDBOX-433
>                 URL: https://issues.apache.org/jira/browse/SANDBOX-433
>             Project: Commons Sandbox
>          Issue Type: Bug
>          Components: BeanUtils2
>            Reporter: Benedikt Ritter
>         Attachments: SANDBOX-433-TestCase.patch
>
>
> If a property get accessed too often, a NullPointerException will be thrown. 
> The same is true for calling methods several times.
> In my configuration I'm seeing a NPE after about 27.000 calls.
> Calling constructors very often does seem to work.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: 
https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Reply via email to