LinkedHashMap::put: object destroyed too soon?

Discussion in 'Java' started by enrique, Apr 28, 2005.

  1. enrique

    enrique Guest

    With method LinkedHashMap::put, would

    myMap.put("aKey", someObject);

    still have a reference to the key "aKey" (and therefore, someObject) if
    myMap is returned by a function?

    My testing shows it is not, contrary to what I understand. Can anyone
    explain this behavior?

    Thanks for your insight.

    epp
    enrique, Apr 28, 2005
    #1
    1. Advertising

  2. enrique wrote:
    >
    > With method LinkedHashMap::put, would
    >
    > myMap.put("aKey", someObject);
    >
    > still have a reference to the key "aKey" (and therefore, someObject) if
    > myMap is returned by a function?


    Most definitely. However, they are called methods not functions.

    > My testing shows it is not, contrary to what I understand. Can anyone
    > explain this behavior?
    >
    > Thanks for your insight.


    It's a bug in your code or test, or a misunderstanding on your part. If you want to know
    which, post your code.

    --
    Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
    ==============================================================
    * The Ultimate DBMS is here!
    * FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)
    Lee Fesperman, Apr 28, 2005
    #2
    1. Advertising

  3. enrique wrote:
    > With method LinkedHashMap::put, would
    >
    > myMap.put("aKey", someObject);
    >
    > still have a reference to the key "aKey" (and therefore, someObject) if
    > myMap is returned by a function?
    >
    > My testing shows it is not, contrary to what I understand. Can anyone
    > explain this behavior?


    Your post is unclear and vague.

    Try posting some real compilable code that demonstrates your problem,
    and explain what you think should happen.

    --
    Daniel Sjöblom
    Remove _NOSPAM to reply by mail
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=, Apr 28, 2005
    #3
  4. enrique wrote:

    > With method LinkedHashMap::put, would
    >
    > myMap.put("aKey", someObject);
    >
    > still have a reference to the key "aKey" (and therefore, someObject) if
    > myMap is returned by a function?


    You would be writing in some language other than Java if a method could
    return an object. Java methods can return references to objects, but
    not objects themselves. Yes, I'm being a bit pedantic, but it serves
    the purpose of underscoring the fact that "returning myMap" from a
    method (by which you mean returning a reference to it) does absolutely
    nothing whatsoever to the Map itself. In particular, it has no effect
    at all on the contents of the Map.

    > My testing shows it is not, contrary to what I understand. Can anyone
    > explain this behavior?


    Certainly: either your test or the code under test is buggy. If you
    want a more specific analysis, you'll have to show us the code.

    --
    John Bollinger
    John C. Bollinger, Apr 28, 2005
    #4
  5. enrique

    enrique Guest

    This is a simplified version of what I'm troubleshooting. What is
    happening is that I am maintaining a LinkedHashMap that manages other
    LinkedHashMaps. The top-level map is created at global scope. Its
    children are created by another method. Grandchildren are created in
    yet another method...

    _topMap.put("aKey", makeAnotherMap(Vector
    argsForCreatingTheGrandKids));

    ....

    LinkedHashMap makeAnotherMap(Vector v)
    {
    LinkedHashMap map = new LinkedHashMap();
    for (int x = 0, y = files.size(); x < y; ++x)
    {
    String newKey = (String)v.get(x);
    map.put(newKey, makeYetAnotherMap(newKey));
    }
    return map;
    }

    The grand-kids (also LinkedHashMaps) have empty key sets by the time
    makeAnotherMap loses scope.

    Does anyone see anything here that would cause my objects to get
    destroyed by the time the call to makeAnotherMap returns?

    Thanks again.

    epp
    enrique, Apr 28, 2005
    #5
  6. enrique

    enrique Guest

    This is a simplified version of what I'm troubleshooting. What is
    happening is that I am maintaining a LinkedHashMap that manages other
    LinkedHashMaps. The top-level map is created at global scope. Its
    children are created by another method. Grandchildren are created in
    yet another method...

    _topMap.put("aKey", makeAnotherMap(Vector
    argsForCreatingTheGrandKids));

    ....

    LinkedHashMap makeAnotherMap(Vector v)
    {
    LinkedHashMap map = new LinkedHashMap();
    for (int x = 0, y = v.size(); x < y; ++x)
    {
    String newKey = (String)v.get(x);
    map.put(newKey, makeYetAnotherMap(newKey));
    }
    return map;

    }

    The grand-kids (also LinkedHashMaps) have empty key sets by the time
    makeAnotherMap loses scope.

    Does anyone see anything here that would cause my objects to get
    destroyed by the time the call to makeAnotherMap returns?

    Thanks again.

    epp
    enrique, Apr 29, 2005
    #6
  7. enrique wrote:
    >
    > This is a simplified version of what I'm troubleshooting. What is
    > happening is that I am maintaining a LinkedHashMap that manages other
    > LinkedHashMaps. The top-level map is created at global scope. Its
    > children are created by another method. Grandchildren are created in
    > yet another method...
    >
    > _topMap.put("aKey", makeAnotherMap(Vector
    > argsForCreatingTheGrandKids));
    >
    > ...
    >
    > LinkedHashMap makeAnotherMap(Vector v)
    > {
    > LinkedHashMap map = new LinkedHashMap();
    > for (int x = 0, y = v.size(); x < y; ++x)
    > {
    > String newKey = (String)v.get(x);
    > map.put(newKey, makeYetAnotherMap(newKey));
    > }
    > return map;
    >
    > }
    >
    > The grand-kids (also LinkedHashMaps) have empty key sets by the time
    > makeAnotherMap loses scope.
    >
    > Does anyone see anything here that would cause my objects to get
    > destroyed by the time the call to makeAnotherMap returns?
    >
    > Thanks again.


    You just made two postings of code with a subtle difference. You're probably obscuring
    the actual problem by 'simplifying'. Post your actual code (cut & paste).

    BTW, according to your description the makeYetAnotherMap() method is were the problem
    is. The code you posted here looks ok. You seem to be confusing yourself.

    --
    Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
    ==============================================================
    * The Ultimate DBMS is here!
    * FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)
    Lee Fesperman, Apr 29, 2005
    #7
  8. enrique

    enrique Guest

    Great, that's helpful feedback on the code that I did post. At least
    one other person is okay with the basic algorithm. I primarily need to
    verify that the references to objects created were being maintained as
    I hoped.

    I'll review the suspect method (that I didn't post here).

    epp
    enrique, Apr 29, 2005
    #8
  9. In article <>,
    "enrique" <> wrote:

    > With method LinkedHashMap::put, would
    >
    > myMap.put("aKey", someObject);
    >
    > still have a reference to the key "aKey" (and therefore, someObject) if
    > myMap is returned by a function?
    >
    > My testing shows it is not, contrary to what I understand. Can anyone
    > explain this behavior?
    >
    > Thanks for your insight.
    >
    > epp


    Post a complete and runnable demonstration. There is no way to have a
    reference to a garbage collected object unless you use JNI improperly.
    Kevin McMurtrie, Apr 29, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Hunter Ross

    Session times out too soon...

    James Hunter Ross, Oct 13, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    2,974
    JIMCO Software
    Oct 29, 2005
  2. Hans
    Replies:
    5
    Views:
    1,374
  3. John Kotuby

    Sessions seem to time out too soon

    John Kotuby, Aug 28, 2008, in forum: ASP .Net
    Replies:
    3
    Views:
    662
    Anthony Jones
    Aug 28, 2008
  4. AAaron123
    Replies:
    2
    Views:
    516
    AAaron123
    Feb 15, 2009
  5. Plamen Doykov

    Sessions expire too soon

    Plamen Doykov, Jun 21, 2004, in forum: ASP .Net Security
    Replies:
    1
    Views:
    171
    Plamen Doykov
    Jun 25, 2004
Loading...

Share This Page