[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Need help with singleton!

Diego Bernardes

3/19/2008 3:22:00 AM

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! :)
--
Posted via http://www.ruby-....

5 Answers

mt

3/19/2008 6:19:00 AM

0

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.

Diego Bernardes

3/19/2008 12:07:00 PM

0

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

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
--
Posted via http://www.ruby-....

mt

3/19/2008 1:24:00 PM

0

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.

On Wed, Mar 19, 2008 at 8:06 PM, Diego Bernardes
<di3go.bernardes@gmail.com> wrote:
> 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 :)
>
> 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
> --
> Posted via http://www.ruby-....
>
>

Diego Bernardes

3/19/2008 2:22:00 PM

0

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 :)
--
Posted via http://www.ruby-....

Thufir Hawat

3/28/2008 6:40:00 AM

0

On Wed, 19 Mar 2008 12:22:26 +0900, Diego Bernardes 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