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

Fixed memory leak in LocationMap::remove #251

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/internals/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,29 @@ impl LocationMap {

/// Removes an entity from the location map.
pub fn remove(&mut self, entity: Entity) -> Option<EntityLocation> {
use std::collections::hash_map::Entry;

let block = entity.0.get() / BLOCK_SIZE;
let idx = (entity.0.get() % BLOCK_SIZE) as usize;
if let Some(loc) = self.blocks.get_mut(&block).and_then(|v| v.get_mut(idx)) {
let original = loc.take();
if original.is_some() {
self.len -= 1;

let r = if let Entry::Occupied(mut block_entry) = self.blocks.entry(block) {
let block_sloice = block_entry.get_mut();

let removed = block_sloice.get_mut(idx).and_then(Option::take);

if removed.is_some() && block_sloice.iter().all(Option::is_none) {
block_entry.remove();
}
original

removed
} else {
None
};

if r.is_some() {
self.len -= 1;
}

r
}
}