RoadRoadRuner 2018年01月20日
C++ dll 内存泄露问题
各位大侠:
现今遇到一个问题,我在线程中动态调用了DLL,发现系统的句柄数不断升高,最后确定为其中一个方法,这个方法的目的是连接一个USB设备,只要一运行该方法,就会导致系统句柄数升高,最后死锁。我认为应该内存泄露问题,但苦于找不到泄漏的地方,现把代码贴出来,求助!
// 1 返回成功
// 0 无法创建设备句柄
//
int UsbConnect(int m_PID,int m_VID)
{

//获得所有的HIDS
HidD_GetHidGuid(&HidGuid);

//获得指向的句柄
hDevInfo=SetupDiGetClassDevs(&HidGuid,NULL,NULL,DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);

//获得明细数据大小
devInfoData.cbSize = sizeof(devInfoData);

//开始查找设备
bool LastDevice = FALSE; //是否最后一个
int MemberIndex = 0; //成员序号
bool MyDeviceDetected; //是否找到

LastDevice = FALSE;
MemberIndex = 0;
detailData = NULL;
DeviceHandle=NULL;
LONG Result;

do
{
//枚举每个设备
Result=SetupDiEnumDeviceInterfaces(hDevInfo,0,&HidGuid,MemberIndex,&devInfoData);

if(Result !=0)
{
//每个设备的明细数据
Result = SetupDiGetDeviceInterfaceDetail(hDevInfo,&devInfoData,NULL,0,&Length,NULL);

//获得明细数据大小

detailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(Length);

//设置数据大小

detailData -> cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);

//再次调用,获得实际明细数据

Result = SetupDiGetDeviceInterfaceDetail(hDevInfo,&devInfoData,detailData,Length,&Required,NULL);

//获得具体对象句柄
DeviceHandle=CreateFile(detailData->DevicePath,0,FILE_SHARE_READ|FILE_SHARE_WRITE,(LPSECURITY_ATTRIBUTES)NULL,
OPEN_EXISTING,0,NULL);

if(DeviceHandle != INVALID_HANDLE_VALUE)
{
//获得参数
Attributes.Size = sizeof(Attributes);
Result = HidD_GetAttributes(DeviceHandle,&Attributes);

MyDeviceDetected = FALSE;

if (Attributes.VendorID == m_VID)
{
if (Attributes.ProductID == m_PID)
{
MyDeviceDetected = TRUE;

//设定写句柄
WriteHandle=CreateFile(detailData->DevicePath,GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,
(LPSECURITY_ATTRIBUTES)NULL,OPEN_EXISTING,0, NULL);

//设定读句柄
ReadHandle=CreateFile(detailData->DevicePath,GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
(LPSECURITY_ATTRIBUTES)NULL, OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);

//获得读写容量
PHIDP_PREPARSED_DATA PreparsedData;
HidD_GetPreparsedData(DeviceHandle,&PreparsedData);
HidP_GetCaps (PreparsedData,&Capabilities);
HidD_FreePreparsedData(PreparsedData);

//事件处理
EventInit();

break; //跳出循环
} //PID
else
{
//PID不匹配
CloseHandle(DeviceHandle);
//CloseHandle(WriteHandle);
// CloseHandle(ReadHandle);
}
} //VID
else
{
//CloseHandle(hDevInfo);
CloseHandle(DeviceHandle);
//CloseHandle(WriteHandle);
//CloseHandle(ReadHandle);
}
}
else
{
//无法创建
//CloseHandle(hDevInfo);
CloseHandle(DeviceHandle);
//CloseHandle(WriteHandle);
//CloseHandle(ReadHandle);
}
}
else
{
// CloseHandle(hDevInfo);
//CloseHandle(DeviceHandle);
LastDevice=true;
}

//CloseHandle(DeviceHandle);
//CloseHandle(WriteHandle);
//CloseHandle(ReadHandle);

MemberIndex += 1;

free(detailData);
detailData = NULL;
DeviceHandle=NULL;

}while((LastDevice == false) && (MyDeviceDetected == false));


if(MyDeviceDetected == true)
return 1; //成功
else
return 0;
}
...全文
406 点赞 收藏 4
写回复
4 条回复

还没有回复,快来抢沙发~

发动态
发帖子
进程/线程/DLL
创建于2007-09-28

6375

社区成员

4.9w+

社区内容

VC/MFC 进程/线程/DLL
社区公告
暂无公告