471,605 Members | 1,675 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,605 software developers and data experts.

Advice on best way to handle object disposal...

I have three classes. One (Class ObjectC) and two other classes (Class1 and
Class2) that will hold instances of the ObjectC class. I would like to
transfer an instance of OjectC from Class1 into Class2, while making sure
that no resource leak occurs. Is the following the best way to hanlde this?
Is there a more efficient way?

public class ObjectC
{
public ObjectC()
{
// Initialize some data.
}
public void Dispose()
{
// Destroy initialized data.
}
}
public class Class1 : System.Collections.CollectionBase
{
public Class1()
{
}
public void CreatObject()
{
ObjectC oc = new ObjectC();
List.Add(oc);
}
public void TransferObject(Class2 c2)
{
if(List.Count>0)
{
c2.AddObject(List[0]);
((ObjectC)List[0]).Dispose();
List.RemoveAt(0);
}
}
}
public class Class2 : System.Collections.CollectionBase
{
public Class2()
{
}
public void AddObject(Object o)
{
List.Add(o);
}
}

At execution time I would then:

Class1 theC1 = new Class1();
Class2 theC2 = new Class2();

// Create 100 ObjectC objects.
for(int i=0;i<100;i++)
{
theC1.CreateObject();
}
I then would transfer the newly instantiated ObjectC objects into theC2 from
theC1:

// Transfer 100 ObjectC objects from theC1 to theC2.
for(int i=0;i<100;i++)
{
theC1.TransferObject(theC2);
}

Any ideas or comments welcomed.
Jan 10 '06 #1
2 1583
If you implement the IDisposable interface correctly, you shouldn't have to
worry. The destructor method can dispose the object when it is garbage
collected (if it is overlooked). See:

http://msdn.microsoft.com/library/de...classtopic.asp

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

"Robert Vasquez" <Ro***********@discussions.microsoft.com> wrote in message
news:B5**********************************@microsof t.com...
I have three classes. One (Class ObjectC) and two other classes (Class1
and
Class2) that will hold instances of the ObjectC class. I would like to
transfer an instance of OjectC from Class1 into Class2, while making sure
that no resource leak occurs. Is the following the best way to hanlde
this?
Is there a more efficient way?

public class ObjectC
{
public ObjectC()
{
// Initialize some data.
}
public void Dispose()
{
// Destroy initialized data.
}
}
public class Class1 : System.Collections.CollectionBase
{
public Class1()
{
}
public void CreatObject()
{
ObjectC oc = new ObjectC();
List.Add(oc);
}
public void TransferObject(Class2 c2)
{
if(List.Count>0)
{
c2.AddObject(List[0]);
((ObjectC)List[0]).Dispose();
List.RemoveAt(0);
}
}
}
public class Class2 : System.Collections.CollectionBase
{
public Class2()
{
}
public void AddObject(Object o)
{
List.Add(o);
}
}

At execution time I would then:

Class1 theC1 = new Class1();
Class2 theC2 = new Class2();

// Create 100 ObjectC objects.
for(int i=0;i<100;i++)
{
theC1.CreateObject();
}
I then would transfer the newly instantiated ObjectC objects into theC2
from
theC1:

// Transfer 100 ObjectC objects from theC1 to theC2.
for(int i=0;i<100;i++)
{
theC1.TransferObject(theC2);
}

Any ideas or comments welcomed.

Jan 10 '06 #2
Robert Vasquez <Ro***********@discussions.microsoft.com> wrote:
I have three classes. One (Class ObjectC) and two other classes (Class1 and
Class2) that will hold instances of the ObjectC class. I would like to
transfer an instance of OjectC from Class1 into Class2, while making sure
that no resource leak occurs. Is the following the best way to hanlde this?
Is there a more efficient way?


Just to check - do you actually have any non-memory resources? Are you
sure you really need to do *anything*?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 10 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by J.Bijleveld | last post: by
14 posts views Thread by Howard | last post: by
4 posts views Thread by Olaf Baeyens | last post: by
6 posts views Thread by V. Jenks | last post: by
5 posts views Thread by wapsiii | last post: by
16 posts views Thread by anonymous.user0 | last post: by
2 posts views Thread by Gary Howlett | last post: by
8 posts views Thread by george.leithead | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.