PDA

View Full Version : a destroyed view is not really destroyed !


Benibur
02-25-2007, 10:49 AM
I need to test if the instance of an object exists or not.
Since I didn't know how to realize this test, i made different experiments, and i descovered that a destroyed view is not really destroyed ! ! !
=> after a myView.destroy() the view deseapear of the screen but its attributes are still available ! !

Demonstration :

<canvas debug="true">


<simplelayout axis="y"/>

<button text="create link" onclick="creation()">
<method name="creation">
this.parent.link = new LzView(canvas,{x:10,y:40,width:50,height:50,bgcolo r:'black'});
Debug.write("after creation link =", parent.link, parent.link.x);
</method>
</button>

<button text="destruction of link" onclick="destruction()">
<method name="destruction">
parent.link.destroy();
Debug.write("after destruction link =", parent.link, parent.link.x);
</method>
</button>

<button text="check link existence" onclick="destruire()">
<method name="destruire">
Debug.write("after destruction link =", parent.link, parent.link.x);
</method>
</button>

</canvas>


=> how do you test the existence of an instance of an object ?? do you test : "myView == null" doesn't works...

=> and more important : how do you destroy an object ? the memory could have pb if objects are not destroyed...

thanks,
ben

senshi
02-25-2007, 12:01 PM
You should give your view a name, instead of making manually a reference to your new view in parent.link.
Because by giving a name, this reference will be made automatically for you and in destroy(), this reference will also be deleted automatically.


<method name="creation">
new LzView(canvas,{name:"link",x:10,y:40,width:50,height:50,bgcolor:'black'});
Debug.write("after creation link =", parent.link, parent.link.x);
</method>


PS:
for deleting an attribute, you can use the keyword "delete".
for testing whether a view is deleted or not, you can use the private property "__LZdeleted".

trucker_
02-25-2007, 02:00 PM
senshi you are a very wise man
:cool:

Benibur
02-26-2007, 01:23 PM
yess ! !
it works !
thank's a lot !

but... there are 3 "but".

1/ i don't see the difference between :

canvas.link = new LzView(canvas,{x:10,y:40,width:50,height:50,bgcolo r:'black'});

and

new LzView(canvas,{name:"link",x:10,y:40,width:50,height:50,bgcolor:'black'});

In both cases "canvas.link" should adress the same object, no ?
But you are right : there is a diference since your way works...
I can imagine some cases where this solution could be heavy : imagine you just have a reference to a view that you want to destroy, then you would have to write :

myRefView.parent[myRefView.name].destroy();

I am wrong ?


2/ delete ? __LZdeleted ?
I tried to use these, without success. What does mean "deleting an attribute" ?
Do you have an exemple ?

3/ existence test
I need to test the existence of an object. I found this solution :

if( myVar == undefined ) {...}

Is it the best / only solution ?
The debugger sends warnings (logic since myVar is not pointing to an object...) : is there a solution to avoid warnings ?

many thanks,

Ben

tpatput
02-27-2007, 04:36 AM
2/ delete ? __LZdeleted ?
I tried to use these, without success. What does mean "deleting an attribute" ?
Do you have an exemple ?

I was curious about that myself...eventually found an attribute delete in a post:

<canvas>
...
<attribute name="attr1" type="text" value="text-attribute"/>
...
delete canvas.attr1;



I need to test the existence of an object. I found this solution :
Code:

if( myVar == undefined ) {...}

Is it the best / only solution ?

the typeof operation should work and won't throw a warning:
if (typeof(canvas.attr1) == 'undefined') Debug.write('attr1 is deleted');

hope that helps
Tom

note: testing with the original code, typeof(view) did not throw a warning, but typeof(view.x) does throw a warning...I'm not sure if there's a way, or need, to extend typeof to the children of deleted objects...if a parent is deleted probably shouldn't need to test it's children

senshi
02-27-2007, 08:01 AM
1/ i don't see the difference between :

canvas.link = new LzView(canvas,{x:10,y:40,width:50,height:50,bgcolo r:'black'});

and

new LzView(canvas,{name:"link",x:10,y:40,width:50,height:50,bgcolor:'black'});

In both cases "canvas.link" should adress the same object, no ?
But you are right : there is a diference since your way works...


Yes, "canvas.link" does point to the same object, but let's discuss it a bit more:
You've got a new unnamed view and you create a reference to this new view (through "canvas.link = new LzView(..)").
You, as the developer, know that there is a reference on canvas to this new view, but the new view doesn't know anything about this reference. So if you destroy the view, this view won't delete this reference, because it's just not aware of this reference.
But if you give your new view a name, you'll get two advantages: a reference will be made automatically and your new view will look up in its destroy()-Method, whether its parent has got a named-reference to this view or not, and it will delete this reference if it is present.

Here is the sourcecode for deleting this reference (directly from the LFC):

//remove name
if ( this.name != null ){
if ( this.parent[ this.name ] == this ){
delete this.parent[ this.name ];
}
if ( this.immediateparent[ this.name ] == this ){
delete this.immediateparent[ this.name ];
}
if ( this.parent == _root.canvas && _root[ this.name ] == this ){
delete _root[ this.name ];
}
}



I can imagine some cases where this solution could be heavy : imagine you just have a reference to a view that you want to destroy, then you would have to write :

myRefView.parent[myRefView.name].destroy();

I am wrong ?


myRefView.destroy() will perform exactly the same! (see also the code at the end)


2/ delete ? __LZdeleted ?
I tried to use these, without success. What does mean "deleting an attribute" ?
Do you have an exemple ?


See the extended version of your original code at the end.


3/ existence test
I need to test the existence of an object. I found this solution :

if( myVar == undefined ) {...}



As already said, use typeof(..)

if( typeof( canvas.myRef ) == "undefined" ){
//"canvas.myRef" does not exist
} else {
//"canvas.myRef" exists
}



Code:
(you should ignore the "pragmas", it is just a lazy way to avoid the warnings, you should not use this in real applications!)

<canvas debug="true">

<simplelayout axis="y"/>

<button text="create link" onclick="creation()">
<method name="creation">
var myview = new LzView(canvas,{name:"link",x:10,y:40,width:50,height:50,bgcolor:'black'});
canvas.myRef = myview;//set up a manual reference for your new view

Debug.write("after creation link =", canvas.link, canvas.link.x);
Debug.write("after creation link(myRef) =", canvas.myRef, canvas.myRef.x);
</method>
</button>

<button text="destruction of link" onclick="destruction()">
<method name="destruction">
#pragma 'warnUndefinedReferences=false'

//both versions are totally equal!
//canvas.link.destroy();
canvas.myRef.destroy();

Debug.write("after destruction link =", canvas.link, canvas.link.x);
Debug.write("canvas.myRef.__LZdeleted:", canvas.myRef.__LZdeleted );
Debug.write("after destruction link(myRef) =", canvas.myRef, canvas.myRef.x);
</method>
</button>

<button text="check link existence" onclick="destruire()">
<method name="destruire">
#pragma 'warnUndefinedReferences=false'

Debug.write("after destruction link =", canvas.link, canvas.link.x);
Debug.write("canvas.myRef.__LZdeleted:", canvas.myRef.__LZdeleted );
Debug.write("after destruction link(myRef) =", canvas.myRef, canvas.myRef.x);
</method>
</button>

<button text="delete myRef" onclick="destruire()">
<method name="destruire">
#pragma 'warnUndefinedReferences=false'

delete canvas.myRef;

Debug.write("after destruction link =", canvas.link, canvas.link.x);
Debug.write("canvas.myRef.__LZdeleted:", canvas.myRef.__LZdeleted );
Debug.write("after destruction link(myRef) =", canvas.myRef, canvas.myRef.x);
</method>
</button>

</canvas>

Benibur
02-27-2007, 01:56 PM
clap clap clap !
clear & precise : i have understand everything !
the difference between

canvas.link = new LzView...
&
new LzView(canvas,{name:'link'...

is particularly interesting !
the use of delete can be very usefull !

thank you

Ben