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

Fix crash on GUI entity removal with levels #913

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 3 additions & 2 deletions include/ignition/gazebo/EntityComponentManager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ namespace ignition
/// the component.
/// \param[in] _data Data used to construct the component.
/// \return A pointer to the component that was created. nullptr is
/// returned if the component was not able to be created.
/// returned if the component was not able to be created. If _entity
/// does not exist, nullptr will be returned.
public: template<typename ComponentTypeT>
ComponentTypeT *CreateComponent(
const Entity _entity,
Expand Down Expand Up @@ -226,7 +227,7 @@ namespace ignition
/// \param[in] _default The value that should be used to construct
/// the component in case the component doesn't exist.
/// \return The component of the specified type assigned to the specified
/// entity.
/// entity. If _entity does not exist, nullptr is returned.
public: template<typename ComponentTypeT>
ComponentTypeT *ComponentDefault(Entity _entity,
const typename ComponentTypeT::Type &_default =
Expand Down
4 changes: 2 additions & 2 deletions src/EntityComponentManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ bool EntityComponentManager::CreateComponentImplementation(
<< "exist. This create component request will be ignored." << std::endl;
return false;
}

// if this is the first time this component type is being created, make sure
// the component type to be created is valid
if (!this->HasComponentType(_componentTypeId) &&
Expand Down Expand Up @@ -603,7 +603,7 @@ bool EntityComponentManager::CreateComponentImplementation(
switch (compAddResult)
{
case ComponentAdditionResult::FAILED_ADDITION:
ignwarn << "Attempt to create a component of type [" << _componentTypeId
ignerr << "Attempt to create a component of type [" << _componentTypeId
<< "] attached to entity [" << _entity << "] failed.\n";
return false;
case ComponentAdditionResult::NEW_ADDITION:
Expand Down
15 changes: 15 additions & 0 deletions src/EntityComponentManager_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,21 @@ TEST_P(EntityComponentManagerFixture, EntitiesAndComponents)
EXPECT_TRUE(manager.EntityHasComponentType(entity, IntComponent::typeId));
EXPECT_EQ(123, intComp->Data());

// Try to create/query a component from an entity that does not exist. nullptr
// should be returned since a component cannot be attached to a non-existent
// entity
EXPECT_FALSE(manager.HasEntity(kNullEntity));
EXPECT_EQ(nullptr, manager.CreateComponent<IntComponent>(kNullEntity,
IntComponent(123)));
EXPECT_EQ(nullptr, manager.ComponentDefault<IntComponent>(kNullEntity, 123));
EXPECT_EQ(nullptr, manager.Component<IntComponent>(kNullEntity));
EXPECT_FALSE(manager.ComponentData<IntComponent>(kNullEntity).has_value());
EXPECT_EQ(ComponentState::NoChange, manager.ComponentState(kNullEntity,
IntComponent::typeId));
// (make sure the entity wasn't implicitly created during the invalid
// component calls)
EXPECT_FALSE(manager.HasEntity(kNullEntity));

// Remove all entities
manager.RequestRemoveEntities();
EXPECT_EQ(3u, manager.EntityCount());
Expand Down