我发现内存泄漏通过同步使用libUSB的USB中断传输读取数据。我的简单用户程序本身没有使用任何动态内存分配。内部libusb过度使用动态内存分配。通信流程按预期工作。使用libusb_interrupt_transfer后是否有一个特殊的函数来释放任何内部动态内存?有没有人知道是什么原因造成运行期间内存不断增加?
我的协议实现了双向握手。因此,简单的数据交换导致OUT(请求),IN(Ack / Nack),IN(响应)和OUT(Ack / Nack)传输。报告大小为32字节,outEndpointAddr为1,inEndpointAddr为129,以下是相关代码片段。
int main (void)
{
uint32_t devFound = 0;
uint32_t devErrors = 0;
...
int libUsbErr = 0;
if(!findSensor(&devFound, &devErrors, &libUsbErr, foundCB))
printf("finding sensor failed %d\n", libUsbErr);
if(!openSensor(mySensor, &libUsbErr))
printf("open sensor failed %d\n", libUsbErr);
int i = 0;
while(1)
{
printf("[%06d] Int Temp %f C\n",i++, readIntTemper());
Delay(0.5);
}
closeSensor(&mySensor, NULL);
closeSensorContext();
return 0;
}
float readIntTemper()
{
static uint8_t tmp[32];
static uint8_t response[32];
...//Prepare request frame
int libUsbErr = 0;
if(!HID_Write(mySensor, tmp, &written, 4000, &libUsbErr))
{
printf("write request failed %d\n", libUsbErr);
return 0;
}
//Read Ack / Nack
if(!HID_Read(mySensor, tmp, &read, 4000, &libUsbErr))
{
printf("Read ACK NACK failed %d\n", libUsbErr);
return 0;
}
...//Test if Ack / Nack
if(!HID_Read(mySensor, response, &read, 4000, &libUsbErr))
{
printf("Read response failed %d\n", libUsbErr);
return 0;
}
... //Prepare ACK
if(!HID_Write(mySensor, tmp, &written, 4000, &libUsbErr))
{
printf("Ack response failed %d\n", libUsbErr);
return 0;
}
...
float* temper = (float*)&response[8];
return *temper;
}
bool HID_Write(const Sensor* sens, uint8_t* repBuf, int* transferred, uint32_t timeout, int* libUsbErr)
{
if(sens == NULL || repBuf == NULL || transferred == NULL)
return returnlibUSBErr(libUsbErr, -1008); ///TODO nice error codes;
if(!sens->claimed)
return returnlibUSBErr(libUsbErr, -1012); ///TODO nice error codes;
int r = libusb_interrupt_transfer(sens->devHandle, sens->outEndpointAddr,
repBuf, sens->outRepSize, transferred, timeout);
if (r < 0)
return returnlibUSBErr(libUsbErr, r);
return returnlibUSBErr(libUsbErr, LIB_USB_OK);
}
bool HID_Read(const Sensor* sens, uint8_t* repBuf, int* read, uint32_t timeout, int* libUsbErr)
{
if(sens == NULL || read == NULL)
return returnlibUSBErr(libUsbErr, -1008); ///TODO nice error codes;
if(!sens->claimed)
return returnlibUSBErr(libUsbErr, -1012); ///TODO nice error codes;
int r = libusb_interrupt_transfer(sens->devHandle, sens->inEndpointAddr, repBuf,sens->inRepSize, read, timeout);
if (r < 0)
return returnlibUSBErr(libUsbErr, r);
return returnlibUSBErr(libUsbErr, LIB_USB_OK);
}
修改
如果按照此指令监视内存使用情况:
为了找到泄漏,我使用了如下所述的UMDH Windows工具:
问题是我必须使用CVI NI compilter来构建我的应用程序。我无法从这个编译器中获取符号表。所以我的堆转储diff只显示地址。
// Each log entry has the following syntax:
//
// + BYTES_DELTA (NEW_BYTES - OLD_BYTES) NEW_COUNT allocs BackTrace TRACEID
// + COUNT_DELTA (NEW_COUNT - OLD_COUNT) BackTrace TRACEID allocations
// ... stack trace ...
//
// where:
//
// BYTES_DELTA - increase in bytes between before and after log
// NEW_BYTES - bytes in after log
// OLD_BYTES - bytes in before log
// COUNT_DELTA - increase in allocations between before and after log
// NEW_COUNT - number of allocations in after log
// OLD_COUNT - number of allocations in before log
// TRACEID - decimal index of the stack trace in the trace database
// (can be used to search for allocation instances in the original
// UMDH logs).
//
+ 80000 ( 80000 - 0) 1 allocs BackTrace4920B3C
+ 1 ( 1 - 0) BackTrace4920B3C allocations
ntdll!RtlAllocateHeap+274
cvirte!LoadExternalModule+291EC
cvirte!CVIDynamicMemoryInfo+12B6
cvirte!CVIDynamicMemoryInfo+1528
cvirte!CVIDynamicMemoryInfo+1AF9
cvirte!mblen+84D
cvirte!_CVI_Resource_Acquire+116
cvirte!malloc+68
libUSB_HID!???+0 : 41DCE8
libUSB_HID!???+0 : 4E95C7
libUSB_HID!???+0 : 4C13BE
libUSB_HID!???+0 : 4BA09D
libUSB_HID!???+0 : 4C7ABA
libUSB_HID!???+0 : 4F92F0
libUSB_HID!???+0 : 4FB3BD
libUSB_HID!???+0 : 4FC50E
libUSB_HID!???+0 : 415C31
libUSB_HID!???+0 : 408847
libUSB_HID!???+0 : 402967
libUSB_HID!???+0 : 41B51E
libUSB_HID!???+0 : 41A021
kernel32!BaseThreadInitThunk+E
ntdll!__RtlUserThreadStart+70
我还使用我自己的实现跟踪每个内存请求,替换了libUSB中的所有free,alloc,calloc和realloc cmd。此跟踪未显示任何内存泄漏。分配的字节数在运行时期间保持不变,如预期的那样。无论如何,UMDH工具显示堆分配差异。所以我完全没有想法接下来要测试什么。
答案 0 :(得分:1)
我的简单用户程序没有使用任何动态内存分配。
不幸的是,那些libusb_xxx_transfer
函数在内部使用malloc()
。但是也可以在退出调用者之前执行相应的free()
。
该内存通常不会返回到操作系统,但保留在应用程序中以用于下一次malloc()
调用。因此,您将在任务管理器中看到一些内存使用。
这就是为什么你需要更好的工具来检测实际的内存泄漏,比如valgrind。
答案 1 :(得分:0)
很抱歉,我把我的程序移植到minGW gcc,而且每个方面都按预期工作。我认为我为CVI编译器移植libusb并不完全正确。现在我使用标准的DLL,内存泄漏消失了。