Jump to content

Using a semaphore lock correctly


Omar Mussa

Recommended Posts

Yesterday I spent a few hours chasing down a memory leak in some code. It reminded me of a few things:

1) The value of reusable code (this VI should have been a reusable VI)

2) The value of unit testing (I wasn't unit testing)

3) The value of running long/overnight tests (until we ran the burn-in overnight test we didn't catch this problem).

Basically, I was using a semaphore as a lock to some hardware resources. I accidentally used the wrong check for 'Not A Refnum' which results in creating a new semaphore every time the code executes.

post-5746-1227290880.png?width=400

Here is the correct way that fixes the leak:

post-5746-1227290891.png?width=400

Hopefully this helps someone avoid a few hours of debugging a memory leak that shouldn't exist in the first place!

It would be nice if the 'Not a Refnum' primitive could do some kind of internal casting and checking so that both VIs would work -- but at least I won't be making this mistake again (and since I'm adding it to our company's reuse pool, hopefully nobody else will either!).

Link to comment

QUOTE (Omar Mussa @ Nov 21 2008, 10:09 AM)

It would be nice if the 'Not a Refnum' primitive could do some kind of internal casting and checking so that both VIs would work -- but at least I won't be making this mistake again (and since I'm adding it to our company's reuse pool, hopefully nobody else will either!).

Good catch! And, yes, it would be nice if that primitive could catch "not a semaphore refnum either".

Link to comment

Omar,

Thanks for sharing your story with us. This is certainly one of those "Why did LabVIEW let me make this mistake?" scenarios, and every LabVIEW user will eventually make this mistake a dozen times over the course of a long career.

It should also be noted that this affects any of the non-native by-reference "objects", such as: (classic) notifiers, semaphores, rendezvous, configuration files, (classic) queues, etc.

post-17-1227305815.png?width=400

Link to comment

QUOTE (Jim Kring @ Nov 21 2008, 02:19 PM)

It should also be noted that this affects any of the non-native by-reference "objects", such as: (classic) notifiers, semaphores, rendezvous, configuration files, (classic) queues, etc.

Hopefully, by now most people have moved away from the classic non-native Q's and notifiers, so they are less likely to be bitten by this. These VI's have been replaced by native function calls that don't exhibit this behaviour since atleast LV 7x?

It would be nice if NI cleaned up the remaining categories still affected namely: Semaphores, rendezvous and config files.

<nudge> <nudge> AQ are you listening? ;);)

Neville.

Link to comment

QUOTE (Neville D @ Nov 21 2008, 03:07 PM)

Hopefully, by now most people have moved away from the classic non-native Q's and notifiers, so they are less likely to be bitten by this. These VI's have been replaced by native function calls that don't exhibit this behaviour since atleast LV 7x?

It would be nice if NI cleaned up the remaining categories still affected namely: Semaphores, rendezvous and config files.

<nudge> <nudge> AQ are you listening? ;);)

Neville.

Or, how about native semaphores that can also store a single data element and that allow us to specify inheritance of the semaphore reference type ;)

Link to comment

QUOTE (Dean Mills @ Nov 21 2008, 04:32 PM)

In case no one has looked inside the semaphore VIs. The semaphore is just a wrapper for a queue. I haven't looked inside it for many years (LV6 likely) but the not a refnum got me thinking, what is it inside. The rendezvous VI's are also just a wrapper for a queue.

Same with the classic (compatibility) queue VIs.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.