【发布时间】:2018-11-04 03:18:11
【问题描述】:
我正在使用 Borland C++ (6.0) Builder 编译一个大型 C++ 代码库。我注入了代码,这样当我执行一个场景时,每个被命中的方法都可以被记录下来。 出于这个原因,我保留了一个全局向量并前向导入了包含全局变量的文件。
具有全局变量的头文件(Global.h)-
#include <iostream>
#include <vector>
#include <string>
using namespace std;
extern vector<string> tracerVector;
以及实现文件,Global.cpp-
#include "Global.h"
vector<string> tracerVector;
我将头文件包含在我想要跟踪的所有文件中。
以下只是一个样品注入方法。
#include <vcl.h>
#pragma hdrstop
#include "MainWindow.h"
#include "Product.h"
#include "Global.h"
using namespace std;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TmainForm *mainForm;
//---------------------------------------------------------------------------
__fastcall TmainForm::TmainForm(TComponent* Owner): TForm(Owner)
{
TComponent *senderObj = (TComponent*)Owner;
std::string injection = ("Type:" + AnsiString(senderObj->ClassName()) + " Object:" + AnsiString(senderObj->Name)).c_str();
std::string finalStatement = (injection + " methodName:TmainForm").c_str();
if(!tracerVector.empty()){
if(tracerVector.back() != finalStatement){
tracerVector.push_back(finalStatement);
}
}
else if(tracerVector.empty()){
tracerVector.push_back(finalStatement);
}
}
当场景很小的时候就没有了。所需的方法,它工作正常,但如果没有。所需方法变大,我得到以下异常-
size_type size() const { return this->_M_finish - this->_M_start; }
我怀疑我的全局向量大小触发了这个异常。请建议我如何保持整个执行路径。当执行完成并且用户退出应用程序时,我需要将此全局向量的内容写入文件。
【问题讨论】:
-
听起来像是一个静态初始化订单惨败问题。没有minimal reproducible example,虽然我们不能说。
-
不确定我应该提供什么信息才能让我的问题更容易理解。能详细点吗?
-
请提供您遇到的实际错误。您只提供了标准库中的一行代码,而不是错误消息。
-
对不起,我应该说异常,而不是错误。我编辑了我的帖子,但我在帖子中提供的内容除外。
标签: c++ vector memory-management global-variables