在 RELEASE 配置中使用 vector Push_back 时,C++ 代码崩溃

标签 c++ vector crash release-mode push-back

当我通过 Visual Studio 2008 在发布配置中构建解决方案时,使用 C++ vector 时遇到问题。代码在调试配置中运行良好。我在网上搜索过,没有找到解决我遇到的问题的解决方案。

这是我的代码的解释。我定义了一个类如下。该类存储飞机的一些参数,包括其在空间中的位置等。

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 个不同 PlaneConfig 的函数:

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() 函数处崩溃。

我希望我已经很好地解释了我的问题。如果需要更多信息,请告诉我。

如果有任何帮助,我将不胜感激。

艾哈迈德

最佳答案

仅在 Release模式下发生的崩溃几乎总是由于未初始化的内存造成的。

调用 PLANE02 函数时,FileIndexStart、FileIndexStep 和 FilIndexEnd 设置为多少?另外,你能检查一下在崩溃之前push_back实际调用了多少次吗? (例如,在循环内使用 std::cout<<(i - ListStart)<<std::endl;)

关于在 RELEASE 配置中使用 vector Push_back 时,C++ 代码崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13393901/

相关文章:

c++ - SDL_FillRect 改变值

c++ - Windows 特定类型和 native 类型 C++

c++ - Vectors 上的数组下标运算符

c++ - 为什么当程序进入主函数时这个 std::vector 被清除?

macos - 在Mac OS X上的pthread_specific()中崩溃

c++ ServerSocket()、FD_CLOEXEC、fork() 和 execl()

c++ - 斐波那契数列 : Elements do not make sense when array size > 28

矢量查找的 R 习语

visual-studio-2008 - 是什么原因可能导致VS2008崩溃,然后将其带上explorer.exe?

c - C中的二叉树崩溃