C++11 range-based for loop weird behaviour

Everything about the development of Urho3D.

C++11 range-based for loop weird behaviour

PostPosted by sabotage3d » 22 Dec 2016, 00:05

Hi,
I am getting some weird behaviour with range-based for loop in C++11 with Urho3D containers. It looks like undefined behaviour as sometimes I am getting a crash and sometimes I am not. I hope I am not doing it completely wrong. As far as I remember this should work with STL containers and smart pointers.
This is a simple example:

Code: Select all
SharedPtr<ParticleEmitter> particleEmitter1_;
SharedPtr<ParticleEmitter> particleEmitter2_;

particleEmitter1_ = particleNode_->CreateComponent<ParticleEmitter>();
particleEmitter1_->SetEffect(particleEffect1_);

particleEmitter2_ = particleNode_->CreateComponent<ParticleEmitter>();
particleEmitter2_->SetEffect(particleEffect2_);

Vector<SharedPtr<ParticleEmitter>> particleEmitters_;
particleEmitters_.Push(particleEmitter1_);
particleEmitters_.Push(particleEmitter2_);

for (auto particleEmitter : particleEmitters_)
{
   particleEmitter->SetEmit(false);
}
User avatar
sabotage3d
Have many posts
Have many posts
 
Posts: 515
Joined: 25 Oct 2014, 13:26

Re: C++11 range-based for loop weird behaviour

PostPosted by cadaver » 22 Dec 2016, 09:01

Need to investigate. I can't think of any existing/known reason why this should produce crashes.
User avatar
cadaver
Urho3D author
Urho3D author
 
Posts: 1802
Joined: 16 Jan 2014, 14:52
Location: Finland

Re: C++11 range-based for loop weird behaviour

PostPosted by cadaver » 22 Dec 2016, 10:32

In a quick test on VS2015, I didn't see anything bad happening. What compiler are you on?
User avatar
cadaver
Urho3D author
Urho3D author
 
Posts: 1802
Joined: 16 Jan 2014, 14:52
Location: Finland

Re: C++11 range-based for loop weird behaviour

PostPosted by S.L.C » 22 Dec 2016, 12:22

Have you tried different variants like reference, const reference, maybe even dropping the auto:
Code: Select all
for (auto & particleEmitter : particleEmitters_)

Code: Select all
for (const auto & particleEmitter : particleEmitters_)

Code: Select all
for (SharedPtr<ParticleEmitter> & particleEmitter : particleEmitters_)

Code: Select all
for (const SharedPtr<ParticleEmitter> & particleEmitter : particleEmitters_)


Maybe the compiler sees you're fetching a copy with each iteration and tries to optimize some things.

In the auto version, try a "typeid(particleEmitter).name()" to see if some implicit conversion doesn't happen there. Can't be paranoid enough :D
User avatar
S.L.C
New user
New user
 
Posts: 14
Joined: 24 Dec 2014, 21:08

Re: C++11 range-based for loop weird behaviour

PostPosted by sabotage3d » 22 Dec 2016, 21:57

Sorry for the delay I am on OSX 10.10.5 with Clang version 4.0.0. I couldn't get it to crash from yesterday after I updated Qt Creator. If it happens again I will try to narrow the possibilities. I am on Urho3D 1.6 on a merge from these commits 1017e82 e3b9e87.
User avatar
sabotage3d
Have many posts
Have many posts
 
Posts: 515
Joined: 25 Oct 2014, 13:26


Return to Developer Talk

Who is online

Users browsing this forum: No registered users and 0 guests

cron