帮小弟我看看这段为啥存在资源泄露
帮我看看这段为啥存在资源泄露
我使用AddLog写日志
根据日志记录 仅执行了test0 和test1
也就是说在执行到test1后就return了
但是就是这样的程序 它每秒执行一次
大约10秒 就会泄露4K的内存
不知为啥
------解决方案--------------------
不要反复open打开数据库连接,可以用成员变量保存,初始化的时候打开,程序退出的时候close
------解决方案--------------------
ADO有这么一个内在的Bug.
------解决方案--------------------
从代码优化的角度来讲,循环体内部的功能应尽量简单,以减小程序的时间复杂度。换句话说,只把必要的代码置于循环体内部。
所以即便这不是BUG,也不提倡将数据库的打开、关闭放在循环体内。
------解决方案--------------------
请教一下肿么看内存泄露
------解决方案--------------------
用内存检测工具VLD,
- C/C++ code
bool GetRecord::GetNext() { Error=0; _variant_t RecordAffected; try { hr=RecordSet.CreateInstance("ADODB.Recordset"); AddLog("test0");//写日志 RecordSet->Open("waitjudgeEx",_variant_t((IDispatch *)Connection, true),ADODB::adOpenDynamic,ADODB::adLockPessimistic,ADODB::adCmdStoredProc); if(RecordSet->adoEOF) { RecordSet->Close(); RecordSet.Release(); RecordSet = NULL; AddLog("test1"); return false;//没有记录 } AddLog("test2"); SubmitID=(char*)(_bstr_t)RecordSet->GetCollect("提交号"); ProblemID=(char*)(_bstr_t)RecordSet->GetCollect("题号"); TotalTimeLimit=(long)RecordSet->GetCollect("zsx"); CaseTimeLimit=(long)RecordSet->GetCollect("时限"); MemoryLimit=(long)RecordSet->GetCollect("内存限制"); MemoryLimit*=1024; Language=(short)RecordSet->GetCollect("语言"); strcpy(SourceCode,(char*)(_bstr_t)RecordSet->GetCollect("源码")); SourceFolder=WorkDir+SubmitID; SourceFile=SourceFolder+"\\Main."+LanguageExtention[Language]; memset(&stPipe,0,sizeof(stPipe)); stPipe.bInheritHandle=TRUE; stPipe.lpSecurityDescriptor=NULL; stPipe.nLength=sizeof(SECURITY_ATTRIBUTES); CreateDirectory(SourceFolder.c_str(),&stPipe); HANDLE hSourceFile=CreateFile(SourceFile.c_str(), GENERIC_WRITE, 0, &stPipe, CREATE_NEW, 80, 0); ULONG temp; WriteFile(hSourceFile,SourceCode,strlen(SourceCode),&temp,0); CloseHandle(hSourceFile); RecordSet->Close(); RecordSet.Release(); } catch(_com_error e) //异常处理 { char s[256]; sprintf(s,"%s",(char*)(e.Description())); throw s; } return true; }
我使用AddLog写日志
根据日志记录 仅执行了test0 和test1
也就是说在执行到test1后就return了
但是就是这样的程序 它每秒执行一次
大约10秒 就会泄露4K的内存
不知为啥
------解决方案--------------------
不要反复open打开数据库连接,可以用成员变量保存,初始化的时候打开,程序退出的时候close
------解决方案--------------------
ADO有这么一个内在的Bug.
------解决方案--------------------
从代码优化的角度来讲,循环体内部的功能应尽量简单,以减小程序的时间复杂度。换句话说,只把必要的代码置于循环体内部。
所以即便这不是BUG,也不提倡将数据库的打开、关闭放在循环体内。
------解决方案--------------------
请教一下肿么看内存泄露
------解决方案--------------------
用内存检测工具VLD,