You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Sebb (Created) (JIRA)" <ji...@apache.org> on 2012/03/08 03:58:57 UTC

[jira] [Created] (LANG-792) ToStringStyle registry may retain entries between invocations - is this a problem?

ToStringStyle registry may retain entries between invocations - is this a problem?
----------------------------------------------------------------------------------

                 Key: LANG-792
                 URL: https://issues.apache.org/jira/browse/LANG-792
             Project: Commons Lang
          Issue Type: Bug
            Reporter: Sebb


The class ToStringStyle maintains a registry in order to try to detect object cycles (introduced by LANG-69).

Multiple instances in the same thread share the same registry (it's a ThreadLocal - not clear why).
Entries can be left in the registry on return from calling various methods on the instance.
Is this a bug? Can object cycles cause problems across method calls?
If it is intended for the registered objects to remain across method calls, can objects from different instances interfere with each other?

The registry uses a WeakHashMap, so the entries should not cause problems for garbage collection.

One solution would be to use an instance field to hold the map instead of sharing them.
Would that use more memory, or be otherwise less efficient?

--
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

        

[jira] [Resolved] (LANG-792) ToStringStyle registry may retain entries between invocations - is this a problem?

Posted by "Sebb (Resolved) (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-792?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Sebb resolved LANG-792.
-----------------------

    Resolution: Not A Problem

Seems to be OK; have added comment to source code to say why ThreadLocal is needed, and updated tests.
                
> ToStringStyle registry may retain entries between invocations - is this a problem?
> ----------------------------------------------------------------------------------
>
>                 Key: LANG-792
>                 URL: https://issues.apache.org/jira/browse/LANG-792
>             Project: Commons Lang
>          Issue Type: Bug
>            Reporter: Sebb
>
> The class ToStringStyle maintains a registry in order to try to detect object cycles (introduced by LANG-69).
> Multiple instances in the same thread share the same registry (it's a ThreadLocal - not clear why).
> Entries can be left in the registry on return from calling various methods on the instance.
> Is this a bug? Can object cycles cause problems across method calls?
> If it is intended for the registered objects to remain across method calls, can objects from different instances interfere with each other?
> The registry uses a WeakHashMap, so the entries should not cause problems for garbage collection.
> One solution would be to use an instance field to hold the map instead of sharing them.
> Would that use more memory, or be otherwise less efficient?

--
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

        

[jira] [Commented] (LANG-792) ToStringStyle registry may retain entries between invocations - is this a problem?

Posted by "Gary D. Gregory (Commented) (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-792?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13225306#comment-13225306 ] 

Gary D. Gregory commented on LANG-792:
--------------------------------------

Howdy,

A thread local is used so that calling reflectionToString on the same object (and any of its referred objects) from multiple threads works (returns the same String).

If the registry was _not_ a thread local, then all threads would share the registry and that would lead to some threads not getting a proper toString of an object because another thread already registered it, erroneously thinking it was already visited.

For example for an object o that has one ivar x which itself has a y ivar pointing to "y":

Thread 1: registers x and does a deep toStrings on x
Thread 2: checks that x is registered, it is, so it uses the default toString for x (not the deep toString)
Thread 1: ungisters x
...

Thread 1 gets x[y["y"]] OK
Thread 2 gets x[y@123] BAD

Also, with a thread local, the registry does not need to be a thread-safe data structure.
                
> ToStringStyle registry may retain entries between invocations - is this a problem?
> ----------------------------------------------------------------------------------
>
>                 Key: LANG-792
>                 URL: https://issues.apache.org/jira/browse/LANG-792
>             Project: Commons Lang
>          Issue Type: Bug
>            Reporter: Sebb
>
> The class ToStringStyle maintains a registry in order to try to detect object cycles (introduced by LANG-69).
> Multiple instances in the same thread share the same registry (it's a ThreadLocal - not clear why).
> Entries can be left in the registry on return from calling various methods on the instance.
> Is this a bug? Can object cycles cause problems across method calls?
> If it is intended for the registered objects to remain across method calls, can objects from different instances interfere with each other?
> The registry uses a WeakHashMap, so the entries should not cause problems for garbage collection.
> One solution would be to use an instance field to hold the map instead of sharing them.
> Would that use more memory, or be otherwise less efficient?

--
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

        

[jira] [Commented] (LANG-792) ToStringStyle registry may retain entries between invocations - is this a problem?

Posted by "Sebb (Commented) (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-792?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13225361#comment-13225361 ] 

Sebb commented on LANG-792:
---------------------------

bq. If the registry was not a thread local, then all threads would share the registry

That is only true if the ToStringStyle (TSS) instance is shared between threads.

I had assumed that each ToStringBuilder would use its own TSS instance, but that is not the case.
Looking again at the code, I see that the ToStringStyle (TSS) classes seem to be intended to be used as singletons which are then shared between threads. 

That explains why the ThreadLocal is needed.

Also, I now realise that the earlier problem with the test cases failing on Gump is that some of them created a builder but then failed to use the toString() method on it. That left entries in the registry, and caused the subsequent test to fail if it checked that the registry was empty.

In normal use, the toString() method is required; the registry will then be cleaned up.
The test cases need to do so too; I'll fix them up again.
                
> ToStringStyle registry may retain entries between invocations - is this a problem?
> ----------------------------------------------------------------------------------
>
>                 Key: LANG-792
>                 URL: https://issues.apache.org/jira/browse/LANG-792
>             Project: Commons Lang
>          Issue Type: Bug
>            Reporter: Sebb
>
> The class ToStringStyle maintains a registry in order to try to detect object cycles (introduced by LANG-69).
> Multiple instances in the same thread share the same registry (it's a ThreadLocal - not clear why).
> Entries can be left in the registry on return from calling various methods on the instance.
> Is this a bug? Can object cycles cause problems across method calls?
> If it is intended for the registered objects to remain across method calls, can objects from different instances interfere with each other?
> The registry uses a WeakHashMap, so the entries should not cause problems for garbage collection.
> One solution would be to use an instance field to hold the map instead of sharing them.
> Would that use more memory, or be otherwise less efficient?

--
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