473,320 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

What is the best way to remove all references of an object?


I have a large pool of business objects all referencing one another in
various ways.
In the client application I want to do something like

employee.Delete();

Behind the scenes, I want to remove all references to the object
allowing it to be garbage collected. I then want to remove the physical
representation of the object from persistance. Unless I have missed a
scenario, the references to the object can be deleted in two ways

MyParentObject.EmployeeProp = null; or
MyEmployeeList.Remove(employee);

In order to do this, I have to maintain some kind of internal lists of
these relationships.

My questions are --

1) Have I missed any other way that an object can be referenced?
2) Isn't there an easier way? Doesn't the GC already have this list
somewhere?

Robert Zurer
ro****@zurer.com
Nov 15 '05 #1
5 9644
Robert Zurer <ro****@zurer.com> wrote:
I have a large pool of business objects all referencing one another in
various ways.
In the client application I want to do something like

employee.Delete();

Behind the scenes, I want to remove all references to the object
allowing it to be garbage collected. I then want to remove the physical
representation of the object from persistance. Unless I have missed a
scenario, the references to the object can be deleted in two ways

MyParentObject.EmployeeProp = null; or
MyEmployeeList.Remove(employee);

In order to do this, I have to maintain some kind of internal lists of
these relationships.

My questions are --

1) Have I missed any other way that an object can be referenced?
Well, the second case is really just the same as the first - it's just
that the Remove method will do appropriate things to the list object so
that it does the first thing internally.
2) Isn't there an easier way? Doesn't the GC already have this list
somewhere?


No, not necessarily. The GC doesn't have to know *all* the references
at any time, it only needs to be able to spot at the end of a sweep if
there are *no* live references left. As soon as it's seen that there's
*one* live reference, it doesn't need to look at any others.

This is basically a nasty situation to be in - one thing you *could* do
instead would be to have a flag within the object which said it was
basically no longer useful, and make sure that everything you do with
the object checks that flag before going any further.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
In article <MP************************@msnews.microsoft.com >,
sk***@pobox.com says...

This is basically a nasty situation to be in - one thing you *could* do
instead would be to have a flag within the object which said it was
basically no longer useful, and make sure that everything you do with
the object checks that flag before going any further.


Thanks for your input.

The solution I am planning to implement, (which I was trying to avoid)
is this. Any architectual or performance-related comments or suggestions
would be appreciated.

Assuming the following to be true:

1 - All business object have a globally unique object id (OID)
2 - All business object descend from ContextBoundObject
3 - All business objects are decorated with a ContextAttribute which
will allow interception of all calls to Set or Add methods.

When a call is intercepted, an entry is made in a globally accessable
Singleton structure which will associate the OID and PropertyName of the
caller with the OID of the object being set.

Robert Zurer
ro****@zurer.com
Nov 15 '05 #3
"Robert Zurer" <ro****@zurer.com> wrote in message news:MP***********************@news.microsoft.com. ..

Assuming the following to be true:

1 - All business object have a globally unique object id (OID)
2 - All business object descend from ContextBoundObject
3 - All business objects are decorated with a ContextAttribute which
will allow interception of all calls to Set or Add methods.

When a call is intercepted, an entry is made in a globally accessable
Singleton structure which will associate the OID and PropertyName of the
caller with the OID of the object being set.


I think that the problem you are trying to solve is too general.
And even if you do find all references to an employee object,
what will you do if particular reference *is* accessed after the
employee has been removed?

It looks like objects that use employees must be ready to cope
with the fact that by the time they access the employee, it
can be long removed.

Off the top of my head, I can suggest two solutions.
They can be used separately, or together.

1. Use weak references. Each employer should have one and
only one object that it reports to ("the manager"). Only this object
should have a real reference to an employee. Manager can
remove employee, if needed. All other users should
receive weak references to the employee (see WeakReference
class) and be ready that this reference can yield null at any time.

One problem with weak references is that the object does not die
immediately after all real references are gone, so weak reference can
retrieve an employee after it has been logically removed.
Thus, you probably will want to use a Removed flag in your employee
object and an intelligent weak reference class that works along these lines:

class EmployeeWeakReference : WeakReference
{
public overrides object Target
{
get
{
object Obj = Base.Target;
if (Obj == null) return null;
Employee E = (Employee)Obj;
if (E.Removed)
return null;
return E;
}
...
}
}

2. If user objects need to be notified about employee removal,
have an OnRemove event in your employee class. Fire this
event when employee is to be removed.

Don't forget about thread safety.

If your program is multithreaded, remember that an employee can be removed
while some other thread is working with it. Either you need to serialize all
access to employees, or work out some protection mehanism that will
prevent this from happening. Note, that this problem does not go away
even if you use some sofisticated reference-tracking machinery to
find out your references.

Ivan
Nov 15 '05 #4

Thankyou for your very excellent reply.
It looks like objects that use employees must be ready to cope
with the fact that by the time they access the employee, it
can be long removed.
Yes, if there is eber a possibility of a reference being null, I always
use conditional logic in the client code.
1. Use weak references
I have not looked into WeakReferences but I will.
Thus, you probably will want to use a Removed flag in your employee


I didn't mention it, but my main aim is to have my business objects
totally agnostic of anything apart from business logic, i.e. plumbing of
the sort that is the subject of this thread. That's why I'm moving
towards using ContextBoundObject and an aspect-oriented approach.

By maintaining a "ReferenceManager" to maintain these lists, concerns
like notifying interested objects that a reference has changed or been
deleted could be handled centrally rather than in the business object
code.

Another point I didn't mention is that these objects exist in a shared
pool, accessed by all clients. Thread-safety is a big concern. Actions
like writing to an object, saving it to persistence or deleting all
references to it would have to be synchronized whereas read operations
may not have to be.

This threading business is very new to me so I would appreciate it if
any gotchas with this architecture could be brought to light.

Thanks again.
Robert Zurer
ro****@zurer.com
Nov 15 '05 #5
"Robert Zurer" <ro****@zurer.com> wrote in message news:MP************************@news.microsoft.com ...
Thus, you probably will want to use a Removed flag in your employee
I didn't mention it, but my main aim is to have my business objects
totally agnostic of anything apart from business logic, i.e. plumbing of
the sort that is the subject of this thread.


This is exactly the source of your trouble.

Deciding what to do when in the middle of your operation you discover
that employee in question has been deleted **is** the business logic.

As long as employees can be deleted, this problem will not go away.
No amount of miraculuous behind-the-scenes plumbing can help it.

If you realize that, your life will be easier :-)
Thread-safety is a big concern. Actions
like writing to an object, saving it to persistence or deleting all
references to it would have to be synchronized whereas read operations
may not have to be.


Take a look at System.Threading.ReaderWriterLock.

Ivan
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
14
by: bo | last post by:
And why and where one should use one vs. the other? Verbally, it seems like semantics to me--but obviously there is some actual difference that makes references different and or preferable over...
4
by: None | last post by:
Hi, I have declared array as Int ids = new int; In ArrayList we can remove specified index using RemoveAt(5) method. My question is how can we do this one with int array (single dimensional...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
3
by: John Nagle | last post by:
The Python "reference manual" says, for "del", "Rather that spelling it out in full details, here are some hints." That's not too helpful. In particular, when "del" is applied to a class object,...
6
by: Jack | last post by:
I have a set of functions to wrap a library. For example, mylib_init() mylib_func() mylib_exit() or handle = mylib_init() mylib_func(handle)
5
by: Smokey Grindel | last post by:
Anyone able to figure whats wrong here? It is saying it cant convert from itself to itself? I am confused on this error that the designer is throwing... One or more errors encountered while...
25
by: lovecreatesbea... | last post by:
Suppose I have the following three classes, GrandBase <-- Base <-- Child <-- GrandChild The following cast expression holds true only if pBase points object of type of ``Child'' or...
66
by: John | last post by:
Hi What are the advantages actually achieved of managed code? I am not talking of theory but in reality. Thanks Regards
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.