Freeing up an object

  • Thread starter Thread starter Jack Addington
  • Start date Start date
J

Jack Addington

I have some 'service' classes that I instantiate when its service boolean is
enabled. When its disabled I want to destroy it. I'm not clear on if I
need to do this and if so how. Basically I have the following:

object ServiceXXX = null;

public void EnableServiceXXX(bool active)
{
if (active && ServiceXXX == null) ServiceXXX = new ServiceXXX(...);
else if (!active && ServiceXXX != null) ... ServiceXXX = null;
}

Is the last line the correct way to do it?
 
Hi,

Setting an referenced instance to null mark it to be reclaimed IF the same
instance is not referenced from another part of the application.

Please note that the memory is not reclaimed at once, but when the GC runs
the next time, hence it's not deterministic.

Cheers,
 
ok so that is an ok approach. When I call the object I always check to see
if the service is enabled / not null.

If I was to flip/flop several times turning the service off and on I would
just create more and more stuff the GC to reclaim whenever it runs?

thx

jack
 
if (active && ServiceXXX == null) ServiceXXX = new ServiceXXX(...);
else if (!active && ServiceXXX != null) ... ServiceXXX = null;

In my rather anal retentive way, that bothered me. It's pointless to
test if ServiceXXX is not null, as setting an object which is already null,
to null is harmless, and should be faster than doing the test. Further,
there's not really need to test if active is true, and then if it's false:

if (active)
{ // braces required so else is attached to the right if()
if (ServiceXXX == null)
ServiceXXX = new ServiceXXX(...);
}
else
ServiceXXX = null;


--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
If you only want only ever want one instance of the object at any one time,
it's worth implementing a singleton pattern here.
 
ok so that is an ok approach. When I call the object I always
check to see if the service is enabled / not null.

jack,

Here's a shorter version of your code:

object ServiceXXX = active ? new ServiceXXX(...) : null;

(I hope this was just some example code, and you're not really using
the ServiceXXX identifier for both the ServiceXXX type and the
ServiceXXX variable).
If I was to flip/flop several times turning the service off and
on I would just create more and more stuff the GC to reclaim
whenever it runs?

Yes. If you want to avoid this, consider redesigning ServiceXXX to
include a boolean "IsActive" property, so it can be directly turned
on and off, instead of recreating ServiceXXX every time to turn it
on.
 
You have a point :-). I changed my code.

thx

James Curran said:
if (active && ServiceXXX == null) ServiceXXX = new ServiceXXX(...);
else if (!active && ServiceXXX != null) ... ServiceXXX = null;

In my rather anal retentive way, that bothered me. It's pointless to
test if ServiceXXX is not null, as setting an object which is already
null,
to null is harmless, and should be faster than doing the test. Further,
there's not really need to test if active is true, and then if it's false:

if (active)
{ // braces required so else is attached to the right if()
if (ServiceXXX == null)
ServiceXXX = new ServiceXXX(...);
}
else
ServiceXXX = null;


--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com


Jack Addington said:
I have some 'service' classes that I instantiate when its service boolean is
enabled. When its disabled I want to destroy it. I'm not clear on if I
need to do this and if so how. Basically I have the following:

object ServiceXXX = null;

public void EnableServiceXXX(bool active)
{
if (active && ServiceXXX == null) ServiceXXX = new ServiceXXX(...);
else if (!active && ServiceXXX != null) ... ServiceXXX = null;
}

Is the last line the correct way to do it?
 
The object is a 'child service' for another object of which I will have
many.

thx
 
nice... thx

Chris R. Timmons said:
jack,

Here's a shorter version of your code:

object ServiceXXX = active ? new ServiceXXX(...) : null;

(I hope this was just some example code, and you're not really using
the ServiceXXX identifier for both the ServiceXXX type and the
ServiceXXX variable).


Yes. If you want to avoid this, consider redesigning ServiceXXX to
include a boolean "IsActive" property, so it can be directly turned
on and off, instead of recreating ServiceXXX every time to turn it
on.
 
Back
Top