简体   繁体   English

CPP-未处理的异常std :: bad_alloc

[英]CPP - Unhandled exception std::bad_alloc

Dear StackOverflow'ers, 亲爱的StackOverflow'ers,

I've been getting into coding with C++ and I took a project where I read information out of a 4D SQL database into MySQL syntax .sql files which in turn get executed by an MySQL server. 我一直在使用C ++进行编码,我参加了一个项目,在该项目中,我从4D SQL数据库中将信息读取到MySQL语法.sql文件中,该文件又由MySQL服务器执行。 I'm running into the following problem; 我遇到了以下问题; if I run the CreateSQL function with one table and then exit the programme, it runs fine. 如果我用一个表运行CreateSQL函数,然后退出程序,则它运行良好。

If I loop the CreateSQL function to create SQL from all tables, it fails with a std::bad_alloc error. 如果我循环执行CreateSQL函数以从所有表创建SQL,它将失败,并显示std :: bad_alloc错误。

Since I'm pretty new with C++, I was hoping if some of the more experienced C++ programmers could point me in the direction where this error could occur. 由于我对C ++还是很陌生,所以我希望一些经验丰富的C ++程序员是否可以指出发生错误的方向。 My (unexperienced) guess would be incorrect freeing of variables or the timing of this, as follows: 我的(经验不足的)猜测可能是错误地释放了变量或错误地释放了时间,如下所示:

SQLFreeHandle( SQL_HANDLE_STMT, hStmt ) ;
SQLFreeHandle( SQL_HANDLE_DBC, hConn ) ;
SQLFreeHandle( SQL_HANDLE_ENV, hEnv ) ;

Any help would be greatly appreciated. 任何帮助将不胜感激。

The full source code is below: 完整的源代码如下:

Source code 源代码

EDIT: Following François' advice from the comments: 编辑:从评论弗朗索瓦的建议:

for( int i = 1 ; i <= numRows ; i++ )
{
// Datatypes
// SQLGetData

char buf[256];
SQLINTEGER numBytes ;
newFile << createInsert(table);
for( int j = 1 ;
  j <= numCols ;
  j++ )
{

  retCode = SQLGetData(

    hStmt,
    j,           // COLUMN NUMBER of the data to get
    SQL_C_CHAR,  // the data type that you expect to receive
    buf,         // the place to put the data that you expect to receive
    255,         // the size in bytes of buf (-1 for null terminator)
    &numBytes    // size in bytes of data returned

  ) ;

    if( CHECK( retCode, "SqlGetData", false ) )
    {
        retCode2 = SQLDescribeColA( hStmt, j, colName, 255, &colNameLen, &dataType, &columnSize, &numDecimalDigits, &allowsNullValues ) ;
        if( CHECK( retCode2, "SQLDescribeCol" ) )
        {
            //cout << dataType << endl;
            if(dataType != 91) {
                newFile << "'" << removeSlashes(removeSpecials(buf)) << "'";
            }
            else if (dataType == 91) {
                newFile << "date_format(str_to_date('" << fixDate(buf) << "', '%d-%m-%Y'),'%Y-%m-%d')";
            }
        }
    //Sleep(50);
    }
    if(j != numCols) {
        newFile << ",";
    }

}
newFile << ");\n";
cout << "Regel #" << i <<  " van tabel " << table << " is verwerkt." << endl;

retCode = SQLFetch( hStmt ) ;
if( !SQL_SUCCEEDED( retCode ) )
{
  cout << "Tabel "+table+" is verwerkt." << endl;
  printf( "Regel %d is de laatste regel.\n", i ) ;
}
}

std::bad_alloc is thrown by new when it fails to allocate memory, usually because of memory exhaustion which usually indicates a memory leak somewhere in your program. std::bad_alloc无法分配内存时,它会被new抛出,通常是由于内存耗尽,通常表明程序中某处发生了内存泄漏。

Your functions createInsert and createSelect are both littered by dynamic allocation that you do not delete : 您的函数createInsertcreateSelect都被不delete动态分配所createSelect

char * array = new char[array_size];

Instead of dynamically allocating like this, you should use std::string and operator>> to extract from an ifstream and steer away from any dynamic allocation, there is always a better approach than manual allocation. 与其像这样动态分配,不如使用std::stringoperator>>ifstream提取并避免任何动态分配,总有一种比手动分配更好的方法。


Side note, while(!file.eof()) is always bad. 旁注, while(!file.eof())总是不好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM