Skip to content

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

@kabisigkeit

Description

@kabisigkeit

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions