Need help with singleton!

D

Diego Bernardes

Hi :)

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! :)
 
M

mutahhir hayat

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.
 
D

Diego Bernardes

mutahhir said:
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 :)

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 :)


Thanks mutahhir
 
M

mutahhir hayat

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. :)

Take care,
Mt.
 
D

Diego Bernardes

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 :)

Thanks for the help :)
 
T

thufir

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
 

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,276
Messages
2,571,384
Members
48,073
Latest member
ImogenePal

Latest Threads

Top