I'm using VS2012 with default optimization settings (/O2), and this problem exists only in release mode.
I have some code that uses a michael_deque
(with the standard GC) and a pointer to (abstract) type T
.
When I try to push back a pointer to a type that is derived from T
, the application crashes while exiting the push_back()
function of michael_deque
.
The problem seems to depend precisely on this specific type T
, because writing a dummy class foo
, deriving from it in class bar
(and printing something in the constructor to avoid it being optimized away) and then pushing back new bar()
to michael_deque does not cause a crash.
The classT
in question is this:
class Task
{
public:
Task() : started(false), unfinishedTasks(1), taskID(++taskIDCounter) {};
Task(unsigned int parentID_) : started(false), unfinishedTasks(1), taskID(++taskIDCounter), parentID(parentID_)/*, taken(0)*/ {};
virtual ~Task() = 0 {};
virtual void execute() final
{
this->doActualWork();
unfinishedTasks--;
}
virtual void doActualWork() = 0;
public:
unsigned int taskID; //ID of this task
unsigned int parentID; //ID of the parent of this task
bool started;
std::atomic<unsigned int> unfinishedTasks; //Number of child tasks that are still unfinished
std::vector<unsigned int> dependencies; //list of IDs of all tasks that this task depends on
};
The error can be reproduced in a minimal program (if you happen to have an environment that can execute this in the same manner as I do, just put an std::atomic<unsigned int> taskIDCounter
somewhere where the Task class can see it) :
#include <cds/container/michael_deque.h>
#include "task.hpp"
class a : public Task
{
a()
{
std::cout<<"dummy print"<<std::endl;
}
virtual ~a()
{
}
virtual void doActualWork()
{
std::cout<<"whatever"<<std::endl;
}
};
int main()
{
cds::Initialize();
{
cds::gc::HP hpGC;
cds::gc::HP::thread_gc myThreadGC;
cds::container::MichaelDeque<cds::gc::HP,Task*> tasks;
tasks.push_back(new a()); //will crash at the end of push_back
}
cds::Terminate();
}
What could be the cause of this? Do I do something undefined in the class Task which causes problems in the optimization problems?