我正在使用PCL和OSG库做一个简单的代码,但我对我的对象的布尔值有一个奇怪的行为。
这是我的班级定义:
class BulletCallback : public osg::NodeCallback
{
public:
bool found_cube;
BulletCallback() {
found_cube = false;
}
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
};
class BulletTransfAndCallBack
{
public:
int start;
BulletCallback* updateCallBack_btc;
osg::ref_ptr<osg::PositionAttitudeTransform> bulletTransf;
BulletTransfAndCallBack(BulletCallback* callback, osg::ref_ptr<osg::PositionAttitudeTransform> transf ){
updateCallBack_btc = callback;
bulletTransf = transf;
time_t currentTime;
time(¤tTime);
start = currentTime;
}
};
std::list< BulletTransfAndCallBack* > BulletTransfAndCallBackList;
int main(int argsc, char** argsv){
std::list< BulletTransfAndCallBack* >::iterator i = BulletTransfAndCallBackList.begin();
while (i != BulletTransfAndCallBackList.end())
{
bool removed = false;
bool found_c = (*i)->updateCallBack_btc->found_cube;
//this variable "found_c" has values like 123, 265... and so on, values larger than 1, I don't understand, it was supposed to be 1 or 0
cout<<"Found_Cube? ["<<distance(BulletTransfAndCallBackList.begin(), i)<<"] "<<found_c<<endl;
if(found_c == true ){
time_t currentTime;
time(¤tTime);
int msec = currentTime - ((*i)->start);
if(msec > 10){
camera2->removeChild((*i)->bulletTransf);
i = BulletTransfAndCallBackList.erase(i);
removed = true;
}
}
if(removed == false){
++i;
}
}
}
变量&#34; found_c&#34;在我的Main方法中使用的值有123,265 ......等等,值大于1,我不明白,它应该是1或0。
但在方法&#34;运算符&#34; BulletCallback类我只看到1或0的值。
有人能帮助我吗?这是什么?
提前致谢!
编辑:当我向列表添加新的BulletTransfAndCallback时,值开始变化,但是我使用了新的BulletCallback ......这里是代码:
mtx.lock();
BulletCallback* bulletCB = new BulletCallback();
BulletTransfAndCallBack* btc = new BulletTransfAndCallBack(bulletCB, bulletTransf );
BulletTransfAndCallBackList.push_back( btc );
static_cast<osg::Node*>(bulletTransf)->setUpdateCallback( bulletCB );
mtx.unlock();
EDIT2:所以当我添加新子弹并附加UpdateCallback时,我已经展示了两个类定义,Main方法..以及代码片段...
所以另一段代码就是这个:
虚空方法&#34;运算符&#34; BulletCallback的内容是:
void BulletCallback::operator()(osg::Node* node, osg::NodeVisitor* nv)
{
osg::PositionAttitudeTransform* bulletTransf = static_cast<osg::PositionAttitudeTransform*>(node);
bool btc_found = false;
BulletCallback* updateCallBack_btc_this;
BulletTransfAndCallBack* btc;
mtx.lock();
std::list< BulletTransfAndCallBack* >::const_iterator iterator;
for (iterator = BulletTransfAndCallBackList.begin(); iterator != BulletTransfAndCallBackList.end() && btc_found == false; ++iterator) {
if((*iterator)->bulletTransf == bulletTransf ){
btc_found = true;
updateCallBack_btc_this = (*iterator)->updateCallBack_btc;
btc = (*iterator);
}
}
mtx.unlock();
mtx2.lock();
osg::Vec3 v = bulletTransf->getPosition();
pcl::PointXYZRGBA ballNeighborPoint;
//verificar se bateu em algum cubo virtual
if(updateCallBack_btc_this->found_cube == false){
osg::ref_ptr<osg::PositionAttitudeTransform> cubeTransf;
osg::Vec3 cubePos;
std::list< osg::ref_ptr<osg::PositionAttitudeTransform> >::const_iterator iterator;
for (iterator = cubesList.begin(); iterator != cubesList.end() && updateCallBack_btc_this->found_cube == false; ++iterator) {
cubePos = (*iterator)->getPosition();
if(abs(v.x()-cubePos.x()) < 5 && abs(v.y()-cubePos.y()) < 5 && abs(v.z()-cubePos.z()) < 5){
updateCallBack_btc_this->found_cube = true;
camera2->removeChild( (*iterator) );
cubeTransf = (*iterator);
cout << "Cubo destruído! Boa!" << endl;
}
}
if(cubeTransf != NULL)
cubesList.remove(cubeTransf);
if(updateCallBack_btc_this->found_cube == true)
checkWinning();
}
std::vector<int> search_indexes;
std::vector<float> search_radiuses;
ballNeighborPoint.x = -v.x()/100.f;
ballNeighborPoint.z = - (v.y()-20)/100.0f;
ballNeighborPoint.y = -v.z()/100.0f;
kdtree->radiusSearch (ballNeighborPoint, 0.05, search_indexes, search_radiuses);
if (search_indexes.size() == 0 && updateCallBack_btc_this->found_cube == false){
v.y() -= 1;
bulletTransf->setPosition(v);
}else{
ballNeighborPoint.x = -v.x()/100.f;
ballNeighborPoint.z = -v.y()/100.0f;
ballNeighborPoint.y = -v.z()/100.0f;
kdtree->radiusSearch (ballNeighborPoint, 0.05, search_indexes, search_radiuses);
if (search_indexes.size() == 0){
v.z() -= 1;
bulletTransf->setPosition(v);
}else{
node->removeUpdateCallback(updateCallBack_btc_this);
//BulletTransfAndCallBackList.remove(btc);
//camera2->removeChild(node);
}
}
if(v.y() < -300){
node->removeUpdateCallback(updateCallBack_btc_this);
//BulletTransfAndCallBackList.remove(btc);
//camera2->removeChild(node);
}
mtx2.unlock();
//cout << "T: " << BulletTransfAndCallBackList.size() << endl;
cout << "CCC: " << updateCallBack_btc_this->found_cube << endl;
}
我没有更多涉及此布尔变量的代码..
答案 0 :(得分:0)
问题解决了!
问题是这个电话:
node->removeUpdateCallback(updateCallBack_btc_this);
它删除了在场景中调用updateCallBack,除此之外它还清除updateCallBack_btc_this使用的内存,我的布尔变量就在那里。
这种方法是自动清理对象,C ++不应该这样做..为什么很多人需要花费数小时才能找到bug。像我一样..当没有BUG时,它只是一个清理对象使用的内存的方法-.-