Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BodyPositionCache's get()-Method might return null #7

Open
kabisigkeit opened this issue Oct 6, 2018 · 0 comments
Open

BodyPositionCache's get()-Method might return null #7

kabisigkeit opened this issue Oct 6, 2018 · 0 comments

Comments

@kabisigkeit
Copy link

method "TransitionBuffer get( EntityId id, int size )"
doesnt provide the WeakReference with the ReferenceQueue, hence the while loop condition will never be true. When using the ReferenceQueue, the WeakReference to the buffer might get GCed though before that buffer is returned, meaning the method could potentially return null (could return null currently too, just really unlikely). this might be a fix:

protected synchronized TransitionBuffer get( EntityId id, int size ) {
// See if we've already got one
WeakReference<TransitionBuffer> result = map.get(id);
TransitionBuffer buffer;
// If result is null, we definitely need to create a new one
// if its not null, we assign result.get() before doing the condition check
// since even when result.get() is not null, we might get null from a following result.get() call
if( result == null || (buffer = result.get()) == null ) {
// Need to create a new one
buffer = PositionTransition.createBuffer(size);
map.put(id, new WeakReference<>(buffer, refs));
}
// Clean out any dead references to keep our map from growing and growing
Reference toRemove;
while( (toRemove = refs.poll()) != null ) {
map.values().remove(toRemove);
}
return buffer;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant