You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by Jason van Zyl <jv...@zenplex.com> on 2002/02/16 19:08:26 UTC

Re: Recursion -> Possible infinite loop

On Sat, 2002-02-16 at 14:05, Christian Asmussen wrote:
> I got across a problem, and would like to know if you guys have a formal
> way of dealing with it.  Supose we have the following recursive method
> 
> public int getDepth(){
> 	return getParent() == null ? 0 : getParent().getDepth() + 1;
> }
> 
> 
> If an interface disigner creates an object with "setParent(this)" then
> getDepth would cause an infinite loop.  Depending on the method, this
> could cause a whole server to go OOME.  Whats the best for to deal whit
> this?  I thought of three ways of avoiding it.  What's the best one?

If in this particular case you required an acyclic graph of objects then
you could either use something like a class invariant or an assertion to
make sure that the parameter to setParent(Object o) isn't itself.

-- 
jvz.

Jason van Zyl
jvanzyl@apache.org

http://tambora.zenplex.org


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Recursion -> Possible infinite loop

Posted by Daniel Rall <dl...@finemaltcoding.com>.
Christian Asmussen <kr...@kriconet.com.br> writes:

> On 16 Feb 2002, Jason van Zyl wrote:
>
>> On Sat, 2002-02-16 at 14:05, Christian Asmussen wrote:
>> > I got across a problem, and would like to know if you guys have a formal
>> > way of dealing with it.  Supose we have the following recursive method
>> > 
>> > public int getDepth(){
>> > 	return getParent() == null ? 0 : getParent().getDepth() + 1;
>> > }
>> > 
>> > 
>> > If an interface disigner creates an object with "setParent(this)" then
>> > getDepth would cause an infinite loop.  Depending on the method, this
>> > could cause a whole server to go OOME.  Whats the best for to deal whit
>> > this?  I thought of three ways of avoiding it.  What's the best one?
>> 
>> If in this particular case you required an acyclic graph of objects then
>> you could either use something like a class invariant or an assertion to
>> make sure that the parameter to setParent(Object o) isn't itself.
>
> But what if the guy goes getParent().setParent(this);  or more?  Ooops you
> sayd acyclic right ;-)  But I assume it is the developers task to avoid
> this right?

An IllegalArgumentException or subclass could be thrown.  Submit a
patch:

http://jakarta.apache.org/site/source.html


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Recursion -> Possible infinite loop

Posted by Christian Asmussen <kr...@kriconet.com.br>.
On 16 Feb 2002, Jason van Zyl wrote:

> On Sat, 2002-02-16 at 14:05, Christian Asmussen wrote:
> > I got across a problem, and would like to know if you guys have a formal
> > way of dealing with it.  Supose we have the following recursive method
> > 
> > public int getDepth(){
> > 	return getParent() == null ? 0 : getParent().getDepth() + 1;
> > }
> > 
> > 
> > If an interface disigner creates an object with "setParent(this)" then
> > getDepth would cause an infinite loop.  Depending on the method, this
> > could cause a whole server to go OOME.  Whats the best for to deal whit
> > this?  I thought of three ways of avoiding it.  What's the best one?
> 
> If in this particular case you required an acyclic graph of objects then
> you could either use something like a class invariant or an assertion to
> make sure that the parameter to setParent(Object o) isn't itself.

But what if the guy goes getParent().setParent(this);  or more?  Ooops you
sayd acyclic right ;-)  But I assume it is the developers task to avoid
this right?

 -- 
"If we did all the things we are capable of, 
we would literally astound ourselves"
 - Thomas Edison


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>