今天写了一个函数,使用了约2M的空间,下列函数中的静态常量max_audio_index_file_size=2000000,
void ReadAudioIndexFile()
{std::string fileName = audioFilePath + audioIndexFileName;
FILE* pFile = fopen( fileName.c_str(), "rb" );
if( NULL == pFile )
{
SSError << "Open Audio Index File Failed, FileName:" << fileName << END;
return;
}
char buffer[max_audio_index_file_size];
memset( buffer, 0, max_audio_index_file_size );
UINT32 readNum = fread( buffer, 1, max_audio_index_file_size, pFile );
fclose(pFile);
std::string strcontent = std::string( buffer, readNum );
delete buffer;
KKSG::AudioList alist;
if( alist.ParseFromString(strcontent) )
{
SSError << "Parse Audio Index File Failed, FileSize:" << readNum << END;
return;
}
for( INT32 i=0; i<alist.datalist_size(); i++ )
{
UINT32 time = alist.datalist(i).time();
UINT64 uid = alist.datalist(i).uid();
ReadOneFile( time, uid );
}
}
开始调试,每次断点走到ReadAudioIndexFile这个函数,进程就crash,报告为 未处理的异常:0x000000FD, stack overflow
后来发现这个是因为 char buffer[max_audio_index_file_size] 这个地方,在栈上申请了太多的内存,导致栈溢出,后来把该处
改为从堆上分配空间,就解决了: char* buffer = new char[max_audio_index_file_size];
重新编译,调试,断点走到此处,再也不会crash