LinkedHashMap::put: object destroyed too soon?

E

enrique

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
 
L

Lee Fesperman

enrique said:
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.
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

enrique said:
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.
 
J

John C. Bollinger

enrique said:
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.
 
E

enrique

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
 
E

enrique

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
 
L

Lee Fesperman

enrique said:
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.
 
E

enrique

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
 
K

Kevin McMurtrie

enrique said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top