Best way to free a device

D

Detlef Reichl

Hi,

what is the best way to release an unix device that can't handle
multiple opens?

I've created a video device class in C, where the device gets opened
with an instanciation like:

Wc::Cam.new '/dev/video0'

In this class i also added a destructor that closes the device. But it
is called to late. From

http://www.rubygarden.org/ruby?ResourceAcquisitionIsInitialization

i've seen, that i cant aspect a direct destruction of the object if it's
not more in use.

Is there a more elegant way to free the resources automatically, as to
create a special free or close method for my object?

Cheers
detlef
 
H

Hugh Sasse

Hi,

what is the best way to release an unix device that can't handle
multiple opens? [...]
http://www.rubygarden.org/ruby?ResourceAcquisitionIsInitialization

i've seen, that i cant aspect a direct destruction of the object if it's
not more in use.

Make it a Singleton?

http://www.ruby-doc.org/stdlib/libdoc/singleton/rdoc/
Is there a more elegant way to free the resources automatically, as to
create a special free or close method for my object?

Cheers
detlef
Hugh
 
E

Edward Faulkner

--TB36FDmn/VVEgNH/
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Is there a more elegant way to free the resources automatically, as to
create a special free or close method for my object?

Yes. Write an open method that takes a block:

def open(device_name)
dev = do_the_opening(device_name)
yield dev
do_the_closing(dev)
end

And use it like this:

MyDevice.open("/dev/foo") do |dev|
dev.write('bar')
puts dev.read
end

regards,
Ed

--TB36FDmn/VVEgNH/
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDZ6u+nhUz11p9MSARAvdVAJ9RAgrOtpKsw5hAORCA+yrpbOExYwCbB+9h
RoiKLY7lrH4ob6SX0YqNux8=
=l0wk
-----END PGP SIGNATURE-----

--TB36FDmn/VVEgNH/--
 
A

Ara.T.Howard

Hi,

what is the best way to release an unix device that can't handle
multiple opens?

I've created a video device class in C, where the device gets opened
with an instanciation like:

Wc::Cam.new '/dev/video0'

In this class i also added a destructor that closes the device. But it
is called to late. From

http://www.rubygarden.org/ruby?ResourceAcquisitionIsInitialization

i've seen, that i cant aspect a direct destruction of the object if it's
not more in use.

Is there a more elegant way to free the resources automatically, as to
create a special free or close method for my object?

make 'Wc::Cam::new' return a singleton object and allow new to take a block
that cleans up.

module Wc
class Cam
INSTANCES = {}
def new path
cam =
if INSTANCES.has_key? path
return INSTANCES[path]
else
super
end
if block_given?
begin
yield cam
ensure
cam.close
end
else
cam
end
end
end
end

that way you can write

Wc::Cam::new('/dev/video0') do |cam|

end

and ensure the cam is closed. when blocks dont' suffice you can write

a = Wc::Cam::new '/dev/video0'
b = Wc::Cam::new '/dev/video0'

and both a and b will be the same object.

regards.

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| anything that contradicts experience and logic should be abandoned.
| -- h.h. the 14th dalai lama
===============================================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,979
Members
47,579
Latest member
CharaS3188

Latest Threads

Top