在RELEASE配置中使用向量push_back时,C++代码崩溃

C++ code crashed when using vector push_back in RELEASE configuration

本文关键字:back C++ 崩溃 代码 push 配置 RELEASE 向量      更新时间:2023-10-16

当我在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;)