A question about GC

  • Thread starter Sam Sungshik Kong
  • Start date
S

Sam Sungshik Kong

Hello!

While I was making my own utility functions, I met an interesting situation.
I want to know how to solve this kind of problem.

I want to use Excel from ruby using OLE.
To make steps simple, I created functions to open excel and close it.

get_excel function has no problem.
I call it like
xl = get_excel

When I'm done with it, I want to close it and unload it from memory.
The right procedure is

xl.quit
xl = nil
GC.start

I want to make a function for the 3 steps.

def quit_excel(xl)
xl.quit
xl = nil
GC.start
end

When I call it, I do

quit_excel(xl) #don't work as expected

As you know, the argument is call-by-value and even if I set nil to xl, the
outer reference is still referencing Excel.
So GC won't collect it.

How can I solve this problem?

Thanks in advance.

Sam
 
E

Eric Hodel

--CQDko/0aYvuiEzgn
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
Hello!
=20
While I was making my own utility functions, I met an interesting situati= on.
I want to know how to solve this kind of problem.
=20
I want to use Excel from ruby using OLE.
To make steps simple, I created functions to open excel and close it.
=20
get_excel function has no problem.
I call it like
xl =3D get_excel
=20
When I'm done with it, I want to close it and unload it from memory.
The right procedure is
=20
xl.quit
xl =3D nil
GC.start
=20
I want to make a function for the 3 steps.
=20
def quit_excel(xl)
xl.quit
xl =3D nil
GC.start
end
=20
When I call it, I do
=20
quit_excel(xl) #don't work as expected
=20
As you know, the argument is call-by-value and even if I set nil to xl, t= he
outer reference is still referencing Excel.
So GC won't collect it.
=20
How can I solve this problem?

def with_excel
xl =3D get_excel
yield xl
ensure
xl.quit
xl =3D nil
GC.start
end

=2E..

with_excel do |xl|
... # your code
end

--=20
Eric Hodel - (e-mail address removed) - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04


--CQDko/0aYvuiEzgn
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (FreeBSD)

iD8DBQFAv2X4MypVHHlsnwQRAsStAKDgAxeTon25T8M6NjaahDPznBmWGQCfa72z
8ShPx8ur+KAdjNPpkzjmD7Q=
=gAHo
-----END PGP SIGNATURE-----

--CQDko/0aYvuiEzgn--
 
R

Robert Klemme

Sam Sungshik Kong said:
Hello!

While I was making my own utility functions, I met an interesting situation.
I want to know how to solve this kind of problem.

I want to use Excel from ruby using OLE.
To make steps simple, I created functions to open excel and close it.

get_excel function has no problem.
I call it like
xl = get_excel

When I'm done with it, I want to close it and unload it from memory.
The right procedure is

xl.quit
xl = nil
GC.start

I want to make a function for the 3 steps.

def quit_excel(xl)
xl.quit
xl = nil
GC.start
end

When I call it, I do

quit_excel(xl) #don't work as expected

As you know, the argument is call-by-value and even if I set nil to xl, the
outer reference is still referencing Excel.
So GC won't collect it.

How can I solve this problem?

Typically you create a class for the data that you manage and by
encapsulating this you can solve the problem:

class Excel
def self.use(*same_args_as_initialize)
ex = self.new(*same_args_as_initialize)

begin
yield ex
ensure
ex.close
end
end

def initialize(*args)
@handle = whatever_you_need
end

def close
if @handle
@handle.cleanup
@handle = nil
end
end

def do_something
ensure_open
@handle.do_something
end

def ensure_open
raise "Not open" unless @handle
end
end

This will still keep the instance of class Excel alive but with no
additional resources attached and in an invalid state (i.e. unusable). A
single instance like this does no harm to memory consumption.
Additionally you can do

Excel.open do |ex|
ex.do_something
end

and cleanup will be done automatically.

Kind regards

robert
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top