MSVS 2010,Windows 7
我正在使用一个API来访问相机的功能。
下面的函数显示一个框架并保存它。
void DisplayThread::OnBufferDisplay( PvBuffer *aBuffer )
{
mDisplayWnd->Display( *aBuffer ); //displaying frame
//Now let us try to save the frame with name of the form %Y%m%d%H%M%S.bmp
system("mkdir D:\\ABCD" );
struct tm *tm;
int count;
time_t t;
char str_time[20];
t = time(NULL);
tm = localtime(&t);
strftime(str_time, sizeof(str_time), "%Y%m%d%H%M%S.bmp", tm); //name of the frame
char name[1000]; //sufficient space
sprintf(name,"%s",str_time);
char path[]="D:\\ABCD";
strcat(path,name); //path =path+"\\"+name;
// char* str=(char*)(void*)Marshal::StringToHGlobalAnsi(path);
PvString lFilename( path );
PvString lCompleteFileName( lFilename );
PvBufferWriter lBufferWriter; //The following function saves image
PvResult lResult = lBufferWriter.Store( aBuffer, lCompleteFileName, PvBufferFormatBMP );
}保存的bmp文件的名称为%Y%m%d%H%M%S.bmp格式。
该程序构建得非常好,即使显示是正确的,但会弹出以下错误消息:

似乎变量“name”的内存分配出现了问题。
但是我已经分配了足够的空间,即使这样,我也会得到这个错误。
为什么会发生这种事?
请告诉我,如果需要更多的信息,以调试这一点。
注意:由lBufferWriter.Store()返回的值为“OK”(指示缓冲区/帧写入成功),但没有保存任何文件。我想这是因为运行时检查失败了。
请帮帮忙。
发布于 2014-11-05 07:04:06
您的path[]数组大小为8,连接后它太小,无法保持字符串。由于这个路径变量在堆栈上,它正在破坏您的堆栈。因此,您的缓冲区应该足够大,以保存您想要放入的数据。
在您的情况下,只需将行更改为:
char path[1024]="D:\\ABCD";https://stackoverflow.com/questions/26750602
复制相似问题