【问题标题】:C++ code crashed when using vector push_back in RELEASE configuration在 RELEASE 配置中使用向量 push_back 时 C++ 代码崩溃
【发布时间】:2012-11-15 08:37:33
【问题描述】:

当我在 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 个不同的 PlaneConfigs:

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

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

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

艾哈迈德

【问题讨论】:

  • 代码为什么要手动调用pivPlane的析构函数?那么,为什么PIVPlaneConfig 甚至还实现了显式析构函数呢?它所做的一切都是毫无价值的,因为它操作的对象在此之后立即停止存在。
  • 是的,不要手动调用析构函数。糟糕,糟糕。
  • 定义“崩溃”。有任何细节吗?你如何创建向量?最好看到一个测试用例,而不是只看到你决定相关的东西。你做了什么调试?在发布版本中,您仍然可以将跟踪数据写入日志。
  • 你的 init 函数中的 pivPlace 是什么?
  • 我删除了析构函数,不幸的是问题仍然存在。

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


【解决方案1】:

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

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

【讨论】:

  • 这是我使用过的:int FileIndexStart = 10, FileIndexStep = 1, FileIndexEnd = 30; PlaneConfigInit(FileIndexStart, FileIndexStep, FileIndexEnd, pivPlanes);
  • 打印输出如下:0 1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20。
  • 问题出在析构函数上。代码现在完美运行。
猜你喜欢
  • 2013-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-31
相关资源
最近更新 更多