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

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

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

Postby TestUzer » Wed Jan 09, 2019 8:14 am

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: 3019
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

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

Postby Patrick » Fri Jan 11, 2019 11:55 am

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: 5
Joined: Thu Jan 18, 2018 7:13 am
Contact:

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

Postby TestUzer » Sat Jan 12, 2019 4:13 am

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 109 times


Consider that the idle SBC2 with CPU monitor attached uses 11% CPU.

cpu_usage_idle.png
cpu_usage_idle.png (8 KiB) Viewed 109 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 109 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: 3019
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

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

Postby Patrick » Mon Jan 14, 2019 11:06 am

I'll try to reproduce the leak.

-Patrick

TestUzer
Phidgetsian
Posts: 5
Joined: Thu Jan 18, 2018 7:13 am
Contact:

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

Postby TestUzer » Wed Jan 16, 2019 12:11 am

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.


Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 1 guest