访问数据库出现内存泄露问题

wangwolue 2004-04-29 08:15:28
以下这段代码有内存泄露,我找了几天,就是没有方法解决。希望有高手能帮忙。

这个函数运行每2000次左右会吃掉1M左右内存。

void CViewUsers::RunSQL(CString sql,int nPages, int iUserAction, int nType)
{
_RecordsetPtr m_pRecordset;
_ConnectionPtr m_pConnection;

HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=sysDB.mdb","","",adModeUnknown);///连接数据库
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
return;
}

//m_list_data 是 CListCtrl m_list_data;

//清空列表框的内容
m_list_data.DeleteAllItems();
while(m_list_data.DeleteColumn(0))
{}

m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open(_bstr_t(sql),_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);

_variant_t var;
CString temp;

try
{
//查看用户
m_list_data.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);//
m_list_data.InsertColumn(0,_T("ID"),LVCFMT_IMAGE|LVCFMT_LEFT);
m_list_data.InsertColumn(1,_T("姓名"));
m_list_data.InsertColumn(2,_T("性别"));

int j;
for(j=0;j<3;j++)
{
m_list_data.SetColumnWidth(j ,90);
}
int nCount=0;

m_pRecordset->MoveFirst();

while(!m_pRecordset->adoEOF)
{
nCount++;
m_pRecordset->MoveNext();
}
m_pRecordset->MoveFirst();

while(!m_pRecordset->adoEOF)
{
temp="";
var=m_pRecordset->GetCollect("userID");
if(var.vt != VT_NULL)
temp=(LPCSTR)_bstr_t(var);
int nItem = m_list_data.InsertItem(m_list_data.GetItemCount(),temp);

temp="";
var=m_pRecordset->GetCollect("userName");
if(var.vt != VT_NULL)
temp=(LPCSTR)_bstr_t(var);
m_list_data.SetItemText(nItem,1,temp);

temp="";
var=m_pRecordset->GetCollect("Sex");
if(var.vt != VT_NULL)
temp=(LPCSTR)_bstr_t(var);
m_list_data.SetItemText(nItem,2,temp);

m_pRecordset->MoveNext();

}

//关闭记录集
m_pRecordset->Close();
m_pConnection->Close();
m_pRecordset=NULL;
m_pConnection= NULL;
}
catch(_com_error e)///捕捉异常
{
AfxMessageBox("读取数据库失败!");///显示错误信息
}


}
...全文
112 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Kudeet 2004-04-29
  • 打赏
  • 举报
回复
帮顶
iverson7909 2004-04-29
  • 打赏
  • 举报
回复
没有release();
showjancn 2004-04-29
  • 打赏
  • 举报
回复
收藏!
只因MSFT!!
UDX协议 2004-04-29
  • 打赏
  • 举报
回复
我建议你先把界面显示,和异常处理的地方,注释到,然后就会很快定位的。这样,根本看不清,大家没有调试环境。
FengYuanMSFT 2004-04-29
  • 打赏
  • 举报
回复
Gradually remove code from your program, until the leak stops. Then the last piece of code is causing problem.

For example, remove the following first:

temp="";
var=m_pRecordset->GetCollect("Sex");
if(var.vt != VT_NULL)
temp=(LPCSTR)_bstr_t(var);
m_list_data.SetItemText(nItem,2,temp);

If leak does not reduce. Remove the following loop:

while(!m_pRecordset->adoEOF)
{

If this still does not solve your problem, search for UMDH (http://support.microsoft.com/?kbid=268343).

www.fengyuan.com
PiggyXP 2004-04-29
  • 打赏
  • 举报
回复
对,楼主release掉试试看,应该没有什么问题了
hahu 2004-04-29
  • 打赏
  • 举报
回复
iverson7909 说的

m_pRecordset->Close();
m_pRecordset.Release();
m_pConnection->Close();
m_pConnection.Release();
m_pRecordset=NULL;
m_pConnection= NULL;

4,017

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧