Hi group, I develop dll that access usb hid device. But every time I
look for usb device it allocates more memory.

The Problem: code works, it finds device I want. But every time I use
it, it allocs more and more memory. I donīt know how to release. The
function: SetupDiDestroyDeviceInfoList(hDevInfo); instead of release
memory it allocs more.

The code I'm using is this:


HidD_GetHidGuid(&HidGuid);

hDevInfo=SetupDiGetClassDevs(&HidGuid, NULL, NULL,
DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);

devInfoData.cbSize = sizeof(devInfoData);
MemberIndex = 0;
LastDevice = FALSE;
result = USB_OK;

do
{
MyDeviceDetected = FALSE;
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,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
(LPSECURITY_ATTRIBUTES)NULL,
OPEN_EXISTING,
0,
NULL);
Attributes.Size = sizeof(Attributes);
Result = HidD_GetAttributes(DeviceHandle,&Attributes);
MyDeviceDetected = FALSE;
if (Attributes.VendorID == VendorID)
{
if (Attributes.ProductID == ProductID)
{
result = USB_OK;

MyDeviceDetected = TRUE;
HidD_GetPreparsedData(DeviceHandle,&PreparsedData);

HidP_GetCaps(PreparsedData,&Capabilities);

HidD_FreePreparsedData(PreparsedData);


ReadHandle=CreateFile(detailData->DevicePath,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
(LPSECURITY_ATTRIBUTES)NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
if (hEventObject2 == 0)
{
hEventObject2 = CreateEvent(NULL,TRUE,TRUE,"");
HIDOverlapped.hEvent = hEventObject2;
HIDOverlapped.Offset = 0;
HIDOverlapped.OffsetHigh = 0;
}
} //if (Attributes.ProductID == ProductID)
else
CloseHandle(DeviceHandle);
} //if (Attributes.VendorID == VendorID)
else
CloseHandle(DeviceHandle);
//Free the memory used by the detailData structure (no longer
needed).
free(detailData);
} //if (Result != 0)
else
LastDevice=TRUE;
MemberIndex = MemberIndex + 1;
} //do
while ((LastDevice == FALSE) && (MyDeviceDetected == FALSE));

//Free the memory reserved for hDevInfo by SetupDiClassDevs.
SetupDiDestroyDeviceInfoList(hDevInfo);