From: transfire@... Date: 2006-07-11T01:21:47+09:00 Subject: Re: instance_exec Okay, I think I've improved it some more: require 'thread' class Proc MethodMutexes = Hash.new do |hash, key| hash[key] = Mutex.new end # Creates a local method based on a Proc. def to_method(name=nil, recv=nil) unless name n = 0 n += 1 while recv.respond_to?(name = "__to_method_#{n}") end recv ||= eval("self", self) klass = recv.class MethodMutexes[klass => name].synchronize do begin klass.send(:define_method, name, &self) return recv.method(name) ensure klass.send(:remove_method, name) unless name end end end end > This can create a lot of different symbols, leading to large memory leaks. > > > recv ||= eval("self", self) > > klass = recv.class > > MethodMutexes[klass => name].synchronize do > ============================ > > This makes the memleak even heavier, going from potentially ~70 bytes to > nearly 300 bytes per call... I don't understand this though. isn't the memory freed when done? Or is some symbol getting created in the process, or ? > > begin > > klass.send(:define_method, name, &self) > ========== > > This will fail if the class was frozen. Ah, I see if the _klass_ is frozen. But that will fail regardless won't it? You can;t even include a module (ef InstanceExecHelper) in a frozen class. Right? Thanks, T.