c++ 交易平台API请求扫描器订阅失败,错误为std::bad_alloc

sirbozc5  于 2023-02-01  发布在  其他
关注(0)|答案(1)|浏览(120)

我在C++实现中使用交易平台API。当我运行一个市场扫描器时,我得到了一个糟糕的内存分配异常。代码被设计为连接并启动两个市场扫描器。
驱动程序

const unsigned MAX_ATTEMPTS = 50;
    const unsigned SLEEP_TIME = 10;

int main(int argc, char** argv)
{
 const char* host = argc > 1 ? argv[1] : "";
 //string host = "";
 int port = argc > 2 ? atoi(argv[2]) : 0;
 if (port <= 0)
 port = 4002;
 string connectOptions = "";
 int clientId = 0;

 unsigned attempt = 0;
 printf("Start of C++ Socket Client Test %u\n", attempt);

 Strategy client;
 EClientSocket* socketpointer = client.m_pClient;

 for (;;) {
 ++attempt;
 printf("Attempt %u of %u\n", attempt, MAX_ATTEMPTS);


 client.connect(host, port, clientId);

 while (client.isConnected())
 {
 client.mainLoop();
 }
 if (attempt >= MAX_ATTEMPTS) {
 printf("Sending Message to Admin");
 //add code to message me in case;
 break;
 }

 printf("Sleeping %u seconds before next attempt\n", SLEEP_TIME);
 std::this_thread::sleep_for(std::chrono::seconds(SLEEP_TIME));
 }
 //…
}

大部分逻辑都是用Strategy来处理的:

Strategy::Strategy()
{
 m_osSignal = new EReaderOSSignal(2000);
 m_pClient = new EClientSocket(this, m_osSignal);

 m_sleepDeadline = 0;
 m_orderId = 0;
 m_pReader = 0;
 m_extraAuth = false;

 Singleton = this;
 TagPrice = "Close";
 TagFloat = "Float";
 TagVolume = "Volume";

 StockInstrument = "STK";
 USLocation = "STK.US";

}


bool Strategy::connect(const char *host, int port, int clientId)
{

 Logger::logToSystemAndMaster("Connecting to client with Host:" + string(host) +
 " and port:" + to_string(port) + " and clientId:" + to_string(clientId) );

 printf("Connecting to %s:%d clientId:%d\n", !(host && *host) ? "127.0.0.1" : host, port, clientId);

 //! [connect]
 bool bRes = m_pClient->eConnect(host, port, clientId, m_extraAuth);
 //! [connect]

 if (bRes) {
 printf("Connected to %s:%d clientId:%d\n", host, port, clientId);
 Logger::logToSystemAndMaster("Connected to client with Host:" + string(host) +
 " and port:" + to_string(port) + " and clientId:" + to_string(clientId));

 //! [ereader]
 m_pReader = new EReader(m_pClient, m_osSignal);
 m_pReader->start();
 //! [ereader]
 StartMarketScanners();
 }
 else
 {

 cout << "Failed to connect to client with Host:" + string(host) +
 " and port:" + to_string(port) + " and clientId:" + to_string(clientId) + "\n";

 Logger::logToSystemAndMaster("Failed to connect to client with Host:" + string(host) +
 " and port:" + to_string(port) + " and clientId:" + to_string(clientId));

 }

 return bRes;
}
void Strategy::StartMarketScanners()
{
 cout << "scanning\n";
 RunGapAndGoScan();
 RunHaltScan();
}

void Strategy::RunGapAndGoScan()
{
 m_pClient->reqScannerParameters();

 TagValueSPtr priceFilter(new TagValue(TagPrice, to_string(MinimumPrice)));// .ToString()));
 TagValueSPtr volumeFilter(new TagValue(TagVolume, to_string(MinimumVolume)));
 TagValueSPtr marketCapFilter(new TagValue(TagFloat, to_string(MinimumFloat)));

 TagValueListSPtr GapAndGoFilters(new TagValueList());
 GapAndGoFilters->push_back(priceFilter);
 GapAndGoFilters->push_back(volumeFilter);
 GapAndGoFilters->push_back(marketCapFilter);
 gapAndGoSubscriptionId = GetNextAccountReqId();
 //m_pClient->reqScannerSubscription(gapAndGoSubscriptionId, GapAndGo(), TagValueListSPtr(), GapAndGoFilters); // requires TWS v973+

 ScannerSubscription scanSub;
 scanSub.instrument = "STK";
 scanSub.locationCode = "STK.US.MAJOR";
 scanSub.scanCode = "HOT_BY_VOLUME";

 cout << "running subscription once\n";
 m_pClient->reqScannerSubscription(7001, scanSub, TagValueListSPtr(), GapAndGoFilters);
 cout << "ran subscription once\n";
}

ScannerSubscription Strategy::GapAndGo()
{
 ScannerSubscription scanSub;
 scanSub.instrument = "STK";
 scanSub.locationCode = "STK.US";// "STK.US.MAJOR";

 return scanSub;
}

void Strategy::RunHaltScan()
{
 m_pClient->reqScannerParameters();

 TagValueListSPtr TagValues(new TagValueList());

 HaltScanSubscriptionId = GetNextAccountReqId();

 m_pClient->reqScannerSubscription(HaltScanSubscriptionId, HaltSubscription(), TagValueListSPtr(), TagValues); 
}

ScannerSubscription Strategy::HaltSubscription()
{
 ScannerSubscription haltSubscription;
 haltSubscription.instrument = "STK";
 haltSubscription.locationCode = "STK.US";
 haltSubscription.scanCode = "Halted";

 return haltSubscription;
}

策略. h:

//…
public:
 //EClientSocket * const m_pClient;
 EClientSocket* m_pClient;
 EReaderOSSignal * m_osSignal;
 EReader * m_pReader;
//…

我在运行reqScannerSubscription()时遇到了bad_alloc异常。我是这个API的新手,在调用m_pClient和m_pReader函数时遇到了很多异常。

dgiusagp

dgiusagp1#

这个问题最终通过更改生成设置得到了解决。几个生成文件没有包括在内,必须更新项目设置以包括其他目录。如果您发现badAlloc消息,请考虑您的源代码可能没有问题,但可能需要包括一些意外的配置文件,如其他目录。

相关问题