When should you run CPhidget_delete?

C, C++, and Visual C++
gusdsnail
Phidgetsian
Posts: 11
Joined: Thu Aug 01, 2013 6:29 am
Contact:

When should you run CPhidget_delete?

Postby gusdsnail » Sat Nov 04, 2017 8:49 pm

Hi,

While porting over to windows from mac, my app was crashing. I isolated the problem to `CPhidget_delete`. I was running the `CPhidget_delete` as clean up on sensor detach event. This was not an issue on mac.

This begs the question. When should you run `CPhdiget_delete`? Only on exit?

Cheers,

Ed

User avatar
mparadis
Site Admin
Posts: 608
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: When should you run CPhidget_delete?

Postby mparadis » Mon Nov 06, 2017 11:28 am

The "delete" function frees up the memory space that was used up by creating the Phidget object. This is essential for long-running programs that create a lot of Phidget handles, since the computer would eventually run out of memory if unused objects weren't deleted. As for why it's crashing, it's possible there's a weird interaction from having it in the detach handler. Are you using Phidget21 or Phidget22?

gusdsnail
Phidgetsian
Posts: 11
Joined: Thu Aug 01, 2013 6:29 am
Contact:

Re: When should you run CPhidget_delete?

Postby gusdsnail » Tue Nov 07, 2017 7:38 pm

I'm running Phidget21. I'm not ready yet to upgrade to Phidget22.

Yes I understand the memory issue. I'm trying to make sure there aren't any memory issues. I suppose I'm curious to know what the proper lifecycle is.

On windows it seems executing CPhidget_delete in the detach event is a problem. When is it safe to execute CPhidget_delete?

Thanks for your help

User avatar
mparadis
Site Admin
Posts: 608
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: When should you run CPhidget_delete?

Postby mparadis » Wed Nov 08, 2017 3:46 pm

It should be safe to run CPhidget_delete any time after closing. The problem here is that the object probably is still in use during the detach handler.

If your program is expecting to connect to a lot of different Phidgets, you can try using the Phidget Manager to attach and detach the Phidgets. You should be able to safely call delete in the Manager detach event. If you're only ever expecting to be connected to one Phidget of that type at any given time, then you can safely reuse the handle since you don't have to worry about them piling up over time.

The other option is to upgrade to Phidget22 which handles this issue.


Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 1 guest