Personal tools

Warning after deleting

From OpenLaszlo

Why do I get a warning after deleting something?

When you delete an object, if there are still references to it, you will often get warnings if a method is called on the deleted object. The solution is to remove all references at the time of the delete.

This is a common error when deleting something controlled by the selectionmanager.

The object that you deleted was still referenced by the selection manager. If you delete it, then the selection manager will still try to unselect it, and you get a warning because the object does not exist.

When deleting, unselect it first, by calling clearSelection() on the selectionmanager.

Example

The following example lets someone select an image. The current selection is managed by a selectionmanager. At the running time, after I delete a selected image, if I click on any other remaining image, I am getting these error warnings in debugger:

  example.lzx:52: reference to undefined property 'crb'
  example.lzx:52: undefined object does not have a property 'setVisible'
  example.lzx:53: reference to undefined property 'crb2'
  example.lzx:53: undefined object does not have a property 'setVisible'

Here's the code:

   <canvas debug="true">
       <dataset name="clipdataset" src="clipartdb.xml" autorequest="true" type="http"/>

       <class name="clipartimage">
           <method name="applyData" args="d" >
               if (d == null) { return; }
               setSource( d );
           </method>
       </class>
       <class name="top" extends="state" >
           <attribute name="xroffset" value="$once{this.getMouse('x')}" />
           <attribute name="initx" value="$once{this.x}" />
           <attribute name="initwidth" value="$once{this.width}" />
           <attribute name="width"
                  value="${this.initwidth+this.initx
                       -this.immediateParent.getMouse('x')+xroffset}" />
           <attribute name="x"
                  value="${this.immediateParent.getMouse('x')
                       -this.xroffset}" />
   
           <attribute name="yroffset" value="$once{this.getMouse('y')}" />
           <attribute name="inity" value="$once{this.y}" />
           <attribute name="initheight" value="$once{this.height}" />
           <attribute name="height"
                  value="${this.initheight+this.inity
                       -this.immediateParent.getMouse('y')+yroffset}" />
           <attribute name="y"
                  value="${this.immediateParent.getMouse('y')
                       -this.yroffset}" />
   
         </class>
   
       <class name="clipartView">
           <selectionmanager name="selector" toggle="false"/>
             <clipItem id="cpitem"  x="${Math.round( Math.random() * 200 )}" y="${Math.round( Math.random() * 200 )}" 
                 datapath="clipdataset:/Clip/ClipArtImg"  width="90" height="90" clickable="true" 
                 onclick="this.parent.selector.select(this);"
                 onmousedown="draggable.apply();" onmouseup="draggable.remove();" >
               <resizestate name="rs"/>
                   <top name="trrs"/>
                <dragstate name="draggable"/>
                   <method name="setSelected" args="amselected">
                   if ( amselected )
                   {
                       this.crb.setVisible(true);
                       this.crb2.setVisible(true);
                       this.bringToFront();
   
                   }
                   else
                   {
                       this.crb.setVisible(false);
                       this.crb2.setVisible(false);
                               }
                    </method>
   
               <view name="crb" align="right" valign="bottom" visible="false"
                         width="5" height="5" bgcolor="red"
                         onmousedown="parent.rs.apply();" 
                         onmouseup="parent.rs.remove()" />
   
               <view name="crb2" align="left" valign="top" visible="false"
                     width="5" height="5" bgcolor="red"
                     onmousedown="parent.trrs.apply();" 
                    onmouseup="parent.trrs.remove()" />
               </clipItem>
               <method event="onkeydown" reference="LzKeys" args="k" >
                       if (k==46) {
                       var selectedNode = this.selector.selected[0];
                       selectedNode.deleteNode();//.deleteNode();
                       }
               </method>
   
           </class>
   
       <class name="clipItem" >
           <clipartimage  stretches="both" width="${parent.width}" height="${parent.height}" datapath="ClipArt/text()"  />
       </class>
       <clipartView name="clipartview"  width="200"  height="200" />
       <button  x ="10" y="200">Delete
           <method event="onclick">
               var selectedNode = canvas.clipartview.selector.selected[0];
               selectedNode.deleteNode();
           </method>
        </button>
   </canvas>

The whole example (contributed by thriputapriya) with the images, can be found here: http://www.laszlosystems.com/developers/community/forums/attachment.php?s=b190e3017a31dce97eedee01337dfac0&postid=6688