SBC2: Bug in Phidget22 C library (20190107) memory leak for VoltageRatioInput.

C, C++, and Visual C++
Post Reply
TestUzer
Phidgetsian
Posts: 9
Joined: Thu Jan 18, 2018 7:13 am
Contact:

SBC2: Bug in Phidget22 C library (20190107) memory leak for VoltageRatioInput.

Post by TestUzer »

Using Phidget22 version 2.1.9.20190107-1

When compiling on the SBC2 I'm seeing a memory leak when calling the Phidget_openWaitForAttachment with Phidget_close on a PhidgetVoltageRatioInput channel but not on DigitalInput channel.

Using cut down sample code (including the the common code from the samples)

The opening and closing of a VoltageRatioInput channel produces a memory leak for me.

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <phidget22.h>
#include "PhidgetHelperFunctions.h"

int main() {
	PhidgetVoltageRatioInputHandle ch = NULL;
	PhidgetReturnCode prc; 
 
	while(1)
	{
		prc = PhidgetVoltageRatioInput_create(&ch);
		CheckError(prc, "Creating Channel", (PhidgetHandle *)&ch);

		prc = Phidget_setDeviceSerialNumber((PhidgetHandle)ch, -1);
		CheckError(prc, "Setting DeviceSerialNumber", (PhidgetHandle *)&ch);

		prc = Phidget_setHubPort((PhidgetHandle)ch, -1);
		CheckError(prc, "Setting HubPort", (PhidgetHandle *)&ch);

		prc = Phidget_setIsHubPortDevice((PhidgetHandle)ch, 0);
		CheckError(prc, "Setting IsHubPortDevice", (PhidgetHandle *)&ch);
	
		prc = Phidget_setChannel((PhidgetHandle)ch, 0);
		CheckError(prc, "Setting Channel", (PhidgetHandle *)&ch);

		prc = Phidget_openWaitForAttachment((PhidgetHandle)ch, 5000);
		CheckOpenError(prc, (PhidgetHandle *)&ch);

		Sleep(100);

		prc = Phidget_close((PhidgetHandle)ch);
		CheckError(prc, "Closing Channel", (PhidgetHandle *)&ch);

		prc = PhidgetVoltageRatioInput_delete(&ch);
		CheckError(prc, "Deleting Channel", (PhidgetHandle *)&ch);

	}
	return 0;
}

Similar code for a DigitalInput channel does not produce a memory leak.

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <phidget22.h>
#include "PhidgetHelperFunctions.h"

int main() {
	PhidgetDigitalInputHandle ch = NULL;
	PhidgetReturnCode prc;

	while(1){
		prc = PhidgetDigitalInput_create(&ch);
		CheckError(prc, "Creating Channel", (PhidgetHandle *)&ch);

		prc = Phidget_setDeviceSerialNumber((PhidgetHandle)ch, -1);
		CheckError(prc, "Setting DeviceSerialNumber", (PhidgetHandle *)&ch);

	     prc = Phidget_setHubPort((PhidgetHandle)ch, -1);
		CheckError(prc, "Setting HubPort", (PhidgetHandle *)&ch);

		prc = Phidget_setIsHubPortDevice((PhidgetHandle)ch, 0);
		CheckError(prc, "Setting IsHubPortDevice", (PhidgetHandle *)&ch);
	
		prc = Phidget_setChannel((PhidgetHandle)ch, 2);
		CheckError(prc, "Setting Channel", (PhidgetHandle *)&ch);
      
		prc = Phidget_openWaitForAttachment((PhidgetHandle)ch, 5000);
		CheckOpenError(prc, (PhidgetHandle *)&ch);

		Sleep(100);

		prc = Phidget_close((PhidgetHandle)ch);
		CheckError(prc, "Closing Channel", (PhidgetHandle *)&ch);

		prc = PhidgetDigitalInput_delete(&ch);
		CheckError(prc, "Deleting Channel", (PhidgetHandle *)&ch);
	}

	return 0;

}
User avatar
Patrick
Lead Developer
Posts: 3403
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: SBC2: Bug in Phidget22 C library (20190107) memory leak for VoltageRatioInput.

Post by Patrick »

If you want to force the library to free up all it's memory before your program exits, call Phidget_finalize(0);

Let me know if you're still seeing any leaks in this case.

-Patrick
TestUzer
Phidgetsian
Posts: 9
Joined: Thu Jan 18, 2018 7:13 am
Contact:

Re: SBC2: Bug in Phidget22 C library (20190107) memory leak for VoltageRatioInput.

Post by TestUzer »

Thanks for looking at this, but Phidget_finalize(0); doesn't help in this situation.

Adding Phidget_finalize(0); to the end of the program won't change the memory leak happening within the loop.

Adding Phidget_finalize(0); to the loop throws a mutex exception.
lock 0x40302e24 failed with 22 (Success)
VoltageRatioInputExample: src/ext/mos/mos_lock-pthread.c:292: mos_mutex_lock: Assertion `res == 0' failed.
Aborted
Taking an approach of open on start and close on exit has the CPU throttling at about 30% and 40% with one open handle. That is before reading values or doing anything else.
cpu_usage_single_create.png
cpu_usage_single_create.png (10.31 KiB) Viewed 22169 times
Consider that the idle SBC2 with CPU monitor attached uses 11% CPU.
cpu_usage_idle.png
cpu_usage_idle.png (8 KiB) Viewed 22169 times
Using an open/close as needed approach is more efficient for the CPU. Adding a more typical 5 second Sleep() in the loop, CPU usage is significantly improved, but the pesky memory leak will eventually eat it up.
cpu_usage_create_per_loop.png
cpu_usage_create_per_loop.png (9.88 KiB) Viewed 22169 times
Can this bug be fixed to release the memory cleanly without a mutex exception or a memory leak? To access the VoltageRatioInput efficiently without crippling the CPU or creating a memory leak.
User avatar
Patrick
Lead Developer
Posts: 3403
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: SBC2: Bug in Phidget22 C library (20190107) memory leak for VoltageRatioInput.

Post by Patrick »

I'll try to reproduce the leak.

-Patrick
TestUzer
Phidgetsian
Posts: 9
Joined: Thu Jan 18, 2018 7:13 am
Contact:

Re: SBC2: Bug in Phidget22 C library (20190107) memory leak for VoltageRatioInput.

Post by TestUzer »

Below is the system status information, if that helps.
System information
Board Name PhidgetSBC2
Board Revision 200
Webif Version 2.1.10-1
Firmware Version PhidgetSBC2 - Version 1.1.0 - Built Thu Jan 15 11:37:24 MST 2015
Kernel Version Linux version 3.2.66 (patrick@debian) (gcc version 4.9.2 ( 4.9.2-10) ) #5 PREEMPT Tue Aug 4 12:04:27 MDT 2015
Distribution Debian GNU/Linux 7
Phidget Library Phidget22 - Version 1.0.0 - Built Jan 7 2019 14:16:44
Current Date/Time Wed Jan 16 16:09:00 AEST 2019

61% 254/419M of Storage used
32% of 59.3MiB when idle.
Just double checked software versions with "apt-get update" "apt-get upgrade"
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
TestUzer
Phidgetsian
Posts: 9
Joined: Thu Jan 18, 2018 7:13 am
Contact:

Re: SBC2: Bug in Phidget22 C library (20190107) memory leak for VoltageRatioInput.

Post by TestUzer »

Attached is test code to help.

Wasn't expecting this issue to be updated yet, but can confirm no change in 20190116-1 "Phidget22 - Version 1.0.0 - Built Jan 16 2019 16:48:45"
Attachments
Test.zip
(5.66 KiB) Downloaded 513 times
User avatar
Patrick
Lead Developer
Posts: 3403
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: SBC2: Bug in Phidget22 C library (20190107) memory leak for VoltageRatioInput.

Post by Patrick »

I see the leak, tracking it down now.

-Patrick
User avatar
Patrick
Lead Developer
Posts: 3403
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: SBC2: Bug in Phidget22 C library (20190107) memory leak for VoltageRatioInput.

Post by Patrick »

Bug is fixed for the next release (probably today or tomorrow)

-Patrick
TestUzer
Phidgetsian
Posts: 9
Joined: Thu Jan 18, 2018 7:13 am
Contact:

Re: SBC2: Bug in Phidget22 C library (20190107) memory leak for VoltageRatioInput.

Post by TestUzer »

Have done an update/upgrade with a restart. Library is reported as "Phidget22 - Version 1.0.0 - Built Jan 29 2019 11:16:26"

Recompiled and executed the test code from my previous message and had top watch the usage. I can see a leak (may not be the same one as before) eating memory. Starting out at 1540 and growing over time. The strange part is the increase is not linear? (+24 +16 +4 +4 +12) Not sure what that means.
mem1.png
mem1.png (4.94 KiB) Viewed 21871 times
mem2.png
mem2.png (4.78 KiB) Viewed 21871 times
mem3.png
mem3.png (4.88 KiB) Viewed 21871 times
mem4.png
mem4.png (5.02 KiB) Viewed 21871 times
mem5.png
mem5.png (4.85 KiB) Viewed 21871 times
mem6.png
mem6.png (4.63 KiB) Viewed 21871 times
After 10 CPU minutes it had RES=1640. There is a leak in there.
Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests