Welcome Guest,Register Now
Log In

ANT Forum

Welcome guest, please Login or Register

   

bug in dll (heap corruption, only 3 lines of code)

Rank

Total Posts: 16

Joined 2012-11-28

PM

I'm trying to test the dll from DEMO_DLL and always get a heap corruption error when running in debug mode on visual studio 2010 in windows 7, 64 bit. The test program is only 3 lines of code. The error message only appears after main() exits.

ANT_Load();
ANT_Init(0, 57600);
ANT_Close();


The Error message is

HEAP[DEMO_DLL.exe]: HEAP: Free Heap block 103510 modified at 103770 after it was freed
Windows has triggered a breakpoint in DEMO_DLL.exe.

This may be due to a corruption of the heap, which indicates a bug in DEMO_DLL.exe or any of the DLLs it has loaded.


Does anyone else see this or know why it is happening?

Bob
     
Avatar
RankRankRankRank

Total Posts: 662

Joined 2012-10-09

PM

Thanks for reporting this issue, we'll look into it.      
Rank

Total Posts: 16

Joined 2012-11-28

PM

I should add that I'm not sure this error occurs on every system. A colleague running the same program hasn't seen the error.

If it might matter, we're talking to Garmin Ant sticks. Mine is connected through a 4 port USB2 hub. His is not.

     
Avatar
RankRankRankRank

Total Posts: 662

Joined 2012-10-09

PM

What type of USB stick are you using? (e.g. USB1/USB2)      
Rank

Total Posts: 16

Joined 2012-11-28

PM

It's a 'Garmin Ant Stick' plugged into a USB 2.0 port.

I just read this info on the back of it:
USB1-Wireless USB Stick 011-01872-00

Looking in Device Manager, under libusb-win32 devices I see that it says "ANT USB Stick 2"

One of the dll calls returns this info:

Product Description: ANT USBStick2
Serial String: 097

I tried the test program without the external Belkin hub and I get the same heap corruption error.

     
Rank

Total Posts: 16

Joined 2012-11-28

PM


I think that I've narrowed the heap corruption problem somewhat. It seems to be related to

void USBDeviceHandleLibusb::PClose(BOOL bReset_)

in ...\ANT_Windows_Library_Package_v3.0\ANT_LIB\software\USB\device_handles\usb_device_handle_libusb.cpp


If this function is not called, I don't get the corruption (although I get a memory leak).

     
Rank

Total Posts: 16

Joined 2012-11-28

PM

More info.

If I comment out this line in ...\ANT_Windows_Library_Package_v3.0\ANT_LIB\software\USB\device_handles\usb_device_handle_libusb.cpp the heap corruption doesn't happen. This is around line 552 in the file.

iRet = clLibusbLibrary.FreeAsync(&asyncContextObject;);

It's something to do with stopping the USB receiver thread, I think.


     
Avatar
RankRankRankRank

Total Posts: 662

Joined 2012-10-09

PM

Thanks for the additional information.
What is the return value of ANT_Init when you run your program?      
Rank

Total Posts: 16

Joined 2012-11-28

PM

ANT_Init returns 1 (TRUE)

ANT_Close returns 0x01f00040 (shouldn't that just be a BOOL?)

I also added a ANT_UnLoad() after ANT_Close(), but it makes no difference about the heap corruption.

     
Rank

Total Posts: 16

Joined 2012-11-28

PM

I got back to this problem. I realize that ANT_Close() is a void function.

I found that ANT_UnLoad() causes the heap corruption problem. There isn't much in this function. The culprit is the call to FreeLibrary (hANTdll);

ANT_Load();
ANT_Init(0, 57600);
ANT_Close();
ANT_UnLoad(); // <<<<<<<< heap corruption error message

I do check the return values of ANT_Load() and ANT_Init() and they both return TRUE.

Any ideas how to find the problem?

     
Rank

Total Posts: 16

Joined 2012-11-28

PM


More hints. I found that if I put a delay of 2500 ms in this function:

void USBDeviceHandleLibusb::ReceiveThread() {
......
iRet = clLibusbLibrary.FreeAsync(&asyncContextObject;);
Sleep(2500); // <<<<<<<<<<<<<<<<<<<<< this delay
.......
}

then the heap corruption goes away. Or at least it didn't happen in 10 test runs. It did happen with a delay of 2250 ms. It happens every time with no delay or a short delay like 1000 ms. Then, the longer the delay the less often it happens.

I'm running a fast I7 computer.




     
Avatar
RankRankRankRank

Total Posts: 662

Joined 2012-10-09

PM

We have not been able to reproduce the issue you are seeing by calling the sequence of functions above. Generally, a heap corruption indicates trying to access memory that has already been freed, and the point at which the corruption appears may not necessarily be where the problem is, which makes these issues very tricky to debug.

Could you post the code you are using that exhibits the problem to see if we can reproduce it on our end?      
Rank

Total Posts: 16

Joined 2012-11-28

PM

I did post the code. It's very short:

int main() {

try {
ANT_Load();
ANT_Init(0, 57600);
ANT_Close();
ANT_UnLoad();
}
catch (const char *str) {
....
}
catch (int &i) {
....
}
catch (...) {
....
}

return 0;
}

That's about it. Sometimes I get the memory corruption on calling ANT_UnLoad(), and sometimes after main() returns to the OS. I do check the return values and assert those. Also this code is in a try/catch block. Visual Studio 2010 and Windows 7 and a Sony Vaio with 8 gigs of RAM.

This program is pretty much the DEMO_DLL project that I downloaded, just stripped down to the above lines of code, and some debugging stuff in the dll to try to locate the heap corruption problem.

Please let me know if there is anything else I can do to help find the problem.


     
Avatar
RankRankRankRank

Total Posts: 662

Joined 2012-10-09

PM

Please post the entire project that you are using. We would like to try to use the exact same code you are using, as we have been unable to reproduce this with the DEMO_DLL.      
Avatar
RankRankRankRank

Total Posts: 662

Joined 2012-10-09

PM

We have been so far unsuccessful reproducing this issue, even when using the same project. Given the location of the magic delay, I am wondering if you are running into particular error conditions when attempting to exit the receive thread. Do you have debug logging enabled when building the DLL? (#define DEBUG_FILE). It would look like it is, since you are building the Debug configuration. The ao_debug_ao_libusb_receive.txt file may provide more insight into this.      
Rank

Total Posts: 16

Joined 2012-11-28

PM

The program only runs these lines of code.

ANT_Load();
ANT_Init(0, 57600);
ANT_Close();
ANT_UnLoad();

I can't get the ao_debug_ao_libusb_receive.txt to be created. DEBUG_FILE is defined in the preprocessor settings for ant_unmanaged_wrapper, ant_lib, and ant_dll. A file Device0.txt is created, however. How do you force creation of the other debug file?

I tried the DEMO_LIB project and it has the same heap corruption problem. I also tried the original unmodified DEMO_DLL with the original Visual Studio version. Same problem.

In the DEMO_LIB version, the error always seems to occur on the call to FreeLibrary(hLibHandle) in the file dsi_libusb_library.cpp.

Could the problem be due to my USB driver / hardware?

thanks