在RELEASE配置中使用向量push_back时,C++代码崩溃
C++ code crashed when using vector push_back in RELEASE configuration
当我在Visual Studio 2008的发行版配置中构建解决方案时,我在使用c++向量时遇到了问题。代码在调试配置中运行良好。我在网上搜索过,没有找到解决我所面临问题的解决方案。
这是对我的代码的解释。我定义了一个类如下。这个类存储飞机的一些参数,包括它在太空中的位置等。
class PIVPlaneConfig{
public:
int update(){
// Create the frame list for the PIV plane.
for ( int i = ListStart ;
i <= ListEnd;
i = i + ListStep){
FramesList.push_back(i);
}
return 0;
};
~PIVPlaneConfig(){
DirRaw = "";
DirProcessed = "";
FnamePreVel = "";
// Reset frames list
FramesList.clear();
};
std::string DirRaw;
std::string DirProcessed;
std::string FnamePreVel;
double pivScaleFactor;
double pivUnitFactorXY;
double pivUnitFactorVxVy;
Point2D planeOriginLocal;
Point3D planeOriginGlobal;
Point3D planeNormal;
bool CS;
int OutOfPlaneVelocity;
// Image Processing Configuration.
std::string FnamePreRawImage;
std::string FnameMaskImage;
int ElemShape;
int ElemShapeCols;
int ElemShapeRows;
Point2D ElemShapeAnchor;
int pivResolutionHorizontal;
int pivResolutionVertical;
// File listing.
int ListStart;
int ListStep;
int ListEnd;
std::vector < int > FramesList;
int nPlanes;
};
我有一个功能,在其中我配置12个不同的平面配置:
int PlaneConfigInit( int FileIndexStart, int FileIndexStep, int FileIndexEnd, vector < PIVPlaneConfig >& Planes )
每个PlaneConfig将在函数PlaneConfigInit中初始化如下。为了简单起见,我只带来了PLANE01的初始化。
double CatiaScalingFactor = 3.8 / 84.839;
PIVPlaneConfig Plane;
// PLANE01
pivPlane.DirRaw = "Y:\Rectangular\Sagittal_01_001";
pivPlane.DirProcessed = "Y:\Rectangular\Sagittal_01_001\Processed";
pivPlane.FnamePreVel = "Sagittal_01_";
pivPlane.FnamePreRawImage = "Sagittal_01_";
pivPlane.OutOfPlaneVelocity = FR3D_MISSING_OUT_OF_PLANE_W;
// File list.
pivPlane.ListStart = FileIndexStart;
pivPlane.ListStep = FileIndexStep;
pivPlane.ListEnd = FileIndexEnd;
pivPlane.planeOriginLocal.x = 0;
pivPlane.planeOriginLocal.y = 0;
pivPlane.planeOriginGlobal.x = -39.206 * CatiaScalingFactor;
pivPlane.planeOriginGlobal.y = 100.0 * CatiaScalingFactor;
pivPlane.planeOriginGlobal.z = -52.316 * CatiaScalingFactor;
// Plane unit normal vector.
pivPlane.planeNormal.x = 0;
pivPlane.planeNormal.y = 0;
pivPlane.planeNormal.z = 1.0;
pivPlane.CS = FR3D_CS_RECT;
pivPlane.update();
pivPlanes.push_back( pivPlane );
pivPlane.~PIVPlaneConfig();
我将上面的代码用于第二个平面,并继续这样做,直到所有12个平面(PLANE01、PLANE02、…、PLANE12)都初始化,所有这些都在函数PlaneConfigInit中。这在调试中非常有效,但在发行版中则不然。PLANE01的初始化是在没有崩溃的情况下完成的,但当它涉及到PLANE02时,它在我使用push_back()函数的类的update()函数处崩溃。
我希望我已经很好地解释了我的问题。如果需要更多信息,请告诉我。
如果有任何帮助,我将不胜感激。
Ahmad
只有在Release模式下才会发生的崩溃几乎总是由于未初始化的内存造成的。
调用PLANE02的函数时,FileIndexStart、FileIndexStep和FilIndexEnd设置为什么?此外,你能检查一下push_back在崩溃之前实际调用了多少次吗?(例如,在环路内使用std::cout<<(i - ListStart)<<std::endl;
)
相关文章:
- 推导 std::vector::back() 的返回类型
- vector.back() 和 vector[vector.size() - 1] 之间的区别?
- vector.push_back(vector.back()+1) 是未定义的行为吗?
- 线路抑制状态错误 C4703 可能未初始化的局部指针变量"back"已使用
- std::vector using back(), pop_back(), push_back(), 得到'double free or corruption'错误
- vector.back()和vector.end()有什么区别
- 列表大小为 1,但 front() 和 back() 不相等
- 返回C++中没有 back() 方法的容器的最后一个元素?
- 在非空 std::list 上使用 std::list.back<int>() 时"Segmentation Fault"
- 我们可以使用Back() - 值索引在其上执行向量插入
- 当我运行以下代码添加str.front() str.back时,它给了我200个,但为什么
- 链表回推操作中需要'back pointer'
- 为什么 std::string 的 back() 应该返回对字符的引用?
- VC++ 自动说明符假定矢量的引用限定符<bool>::back的引用限定符
- C: strtok value gives back null
- vector.back() 可以用来为向量的最后一个元素赋值吗?
- 在std::vector中实现back()
- 在push_front属性上有点混淆.intlist.push_front(2 * intlist.back())会做些
- 将对象转换为 char,然后"uncasting it back"
- std::vector back()的奇怪行为