Need help with singleton!

Hi :slight_smile:

Yea, again a question to help me building the game XD
I have a class called Mob, when i instance a mob from the main i search
the name of mob in a list, if the name is there i create it and i call a
singleton class with all the images of the mob, so if i create the same
kind of mob again he will create it and use the images from the
singleton, to save memory.
Well here comes the problem, if all mobs die and i go town and i never
see this mob again, the singleton gonna still in the memory? 1 mob isnt
trouble, just 2mb, but if i see 200 kinds of mob its alot of memory and
i need free this, i tried to set the singleton nil but when i create the
same kind of mob again gives me a error.

So have any way to create the mobs and when all the mobs die, delete the
singleton too? And ofcourse, the same mob can be instanciable after if
the player see it.

And how i can destroy objetos is just = nil it?

Thanks guys! :slight_smile:

Hi,

If you’re using the Singleton Module from the standard library, i
think it must have a flag or something that automatically gets marked
when it first creates the instance object. By setting that instance to
nil, you allow the garbage collector to clean it, however, the flag
being a class member, exists in memory still and remembers that it has
already created the instance. So, allows you to access it, which in
effect passes you a nil, hence the error.

Solution… hmm… probably you’d like to use a self-brewed singleton
class. Or by adding a resource-cleanup method, that releases all your
images, that can be called by the last mob’s destructor. I’d go with
the second, so that your singleton class becomes lean, yet still
exists, when you don’t need it.

Mt.

mutahhir hayat wrote:

Hi,

If you’re using the Singleton Module from the standard library, i
think it must have a flag or something that automatically gets marked
when it first creates the instance object. By setting that instance to
nil, you allow the garbage collector to clean it, however, the flag
being a class member, exists in memory still and remembers that it has
already created the instance. So, allows you to access it, which in
effect passes you a nil, hence the error.

Solution… hmm… probably you’d like to use a self-brewed singleton
class. Or by adding a resource-cleanup method, that releases all your
images, that can be called by the last mob’s destructor. I’d go with
the second, so that your singleton class becomes lean, yet still
exists, when you don’t need it.

Mt.

Thanks :slight_smile:

i did this

private_class_method :new
attr_accessor :animacao, :animacao_delay, :deslocamento_x,
:deslocamento_y

def Info.create(*args, &block)
@me = new(*args, &block) if ! @me

if @descarregar == true
  @me.initialize(*args, &block)
else
  return @me
end

end
def initialize()
@descarregar = false
end
def destruir
everything = nil
@descarregar = true
end

its working :slight_smile:

Thanks mutahhir

I forgot that, but well, it was working XD i was using puts to see the
object id and it was working
I changed now and still working :slight_smile:

Thanks for the help :slight_smile:

Np, my pleasure.

Something else. I’ve just scanned your code, maybe i’m wrong, but if
@me is your singleton instance, shouldn’t it be a class variable? like
maybe @@me?

Since its working, i’m most probably wrong. :slight_smile:

Take care,
Mt.

On Wed, Mar 19, 2008 at 8:06 PM, Diego B.

On Wed, 19 Mar 2008 12:22:26 +0900, Diego B. wrote:

Yea, again a question to help me building the game XD I have a class
called Mob, when i instance a mob from the main i search the name of mob
in a list, if the name is there i create it and i call a singleton class
with all the images of the mob, so if i create the same kind of mob
again he will create it and use the images from the singleton, to save
memory.

my understanding is that it’s good practice to write good code, use
patterns, standard idioms and so forth, and then, if there’s a
performance problem, start figuring out where the problem is.

I think you may have the horse before the cart in that sense.

Not that singleton isn’t a great thing, it is!

-Thufir

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs