Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > LinkedHashMap::put: object destroyed too soon?

Reply
Thread Tools

LinkedHashMap::put: object destroyed too soon?

 
 
enrique
Guest
Posts: n/a
 
      04-28-2005
With method LinkedHashMap:ut, 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

 
Reply With Quote
 
 
 
 
Lee Fesperman
Guest
Posts: n/a
 
      04-28-2005
enrique wrote:
>
> With method LinkedHashMap:ut, 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)
 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Daniel_Sj=F6blom?=
Guest
Posts: n/a
 
      04-28-2005
enrique wrote:
> With method LinkedHashMap:ut, 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
 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      04-28-2005
enrique wrote:

> With method LinkedHashMap:ut, 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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
enrique
Guest
Posts: n/a
 
      04-28-2005
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

 
Reply With Quote
 
enrique
Guest
Posts: n/a
 
      04-28-2005
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

 
Reply With Quote
 
Lee Fesperman
Guest
Posts: n/a
 
      04-28-2005
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)
 
Reply With Quote
 
enrique
Guest
Posts: n/a
 
      04-29-2005
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

 
Reply With Quote
 
Kevin McMurtrie
Guest
Posts: n/a
 
      04-29-2005
In article <(E-Mail Removed) .com>,
"enrique" <(E-Mail Removed)> wrote:

> With method LinkedHashMap:ut, 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I know if a CORBA object's destroyed? Raga Java 0 12-11-2006 02:42 PM
Accessing static data member through a method of destroyed object Ioannis Vranos C++ 9 05-16-2005 08:01 PM
Enumerate object is destroyed by casting? Nick Jacobson Python 2 08-25-2004 09:25 PM
Is this object destroyed? Anon Email C++ 2 12-08-2003 12:51 AM
when is object destroyed? Charles Herman C++ 2 11-06-2003 09:30 PM



Advertisments