odd bug or newbie mistake

Calwen's Avatar

Calwen

26 Nov, 2010 07:29 PM

Hello all,

New to the the forum and just started on Robotlegs.

I was experimenting a little bit with Flex and bumped into an odd bug. Basically I have a view component (see code below)

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="342" height="232">
<mx:Script>

<![CDATA[
    import events.myFormEvent;

    private function doSomething(e:MouseEvent):void{        
     dispatchEvent(new myFormEvent(myFormEvent.MESSAGE_SENT,false,false,"test"));
    }       
]]>

</mx:Script>

<mx:Panel width="315" height="200" layout="absolute" verticalCenter="0" horizontalCenter="0">
    <mx:Form x="10" y="10" width="275" height="110">
        <mx:FormItem label="Firstname">
            <mx:TextInput id="firstname"/>
        </mx:FormItem>
        <mx:FormItem label="Lastname">
            <mx:TextInput id="lastname"/>
        </mx:FormItem>
        <mx:FormItem label="Job">
            <mx:TextInput id="job"/>
        </mx:FormItem>
    </mx:Form>
    <mx:Button x="115" y="128" label="Button" click="doSomething(event)"/>
</mx:Panel>

</mx:Canvas>


As you can see I am just dispatching an event in the event flow. Now I have a a mediator mapped to this view, see code below:


package controller {

import events.myFormEvent;  
import org.robotlegs.mvcs.Mediator; 
import view.*;

public class myFormMediator extends Mediator {



    [Inject]
    public var myView:myForm;

    override public function onRegister():void {

        eventMap.mapListener(myView, myFormEvent.MESSAGE_SENT, messageReceived);            
    }

    private function messageReceived(e:myFormEvent):void{       
        trace(e.message);
    }
}

}


The good news is that it works...the bad news is that the listener in the mediator will only be called 5 times !!!! So either the event is only dispatched 5 times from the component or either the mediator stop listening to the event after 5 times, I am a bit confused.

Calwen

  1. 1 Posted by Stray on 26 Nov, 2010 08:12 PM

    Stray's Avatar

    Hi Calwen - this sounds to me like a garbage collection issue.

    Any chance that could be it?

    You need to hang on to your context (which in turn hangs on to the mediatorMap).

    ie

    private var myContext:Context;

    // then when you create it (or whatever you're doing here in terms of params)

    myContext = new Context(this);

    If it's not that, come back and we'll chase it up more. It's definitely not something generally reported!

    Thanks,

    Stray

  2. 2 Posted by Calwen on 26 Nov, 2010 08:18 PM

    Calwen's Avatar

    Thanks Stray,

    I have simplied the the component and droppped the source at http://www.flashvalley.com/files/rs/srcview/

    you can play with it at http://www.flashvalley.com/files/rs/ and you will notice that after a couple of click on the "show date" button (sometimes just 1) the data is not updated.

    Calwen

  3. 3 Posted by Calwen on 26 Nov, 2010 08:22 PM

    Calwen's Avatar

    Stray,

    It was a garbage collection issue :) I found the culprit in:

    private function init(val:DisplayObjectContainer):void {

          // forgot the private accessor below :)
                    var myContext:myTestContext;            
            myContext = new myTestContext(val);    }
    

    ]]>

  4. 4 Posted by Calwen on 26 Nov, 2010 08:23 PM

    Calwen's Avatar

    Glad it was such a stupid mistake because I am really starting to like Robotlegs.

  5. 5 Posted by Stray on 26 Nov, 2010 08:23 PM

    Stray's Avatar

    Like I said, you need to hold your context in a member - not just a temporary variable.

    So, where you have:

    var myContext:myTestContext;
            myContext = new myTestContext(val);

    the myContext variable will be available for garbage collection as soon as this function has run.

    You need to be assigning that context to a permanent property.

    I don't use MXML but assuming it's the same as in pure as3

    private var myContext:myTestContext
    private function init(val:DisplayObjectContainer):void {
                myContext = new myTestContext(val); }

    should do it...

    Fingers crossed that it's that simple - if not, shout back.

    Stray

  6. 6 Posted by Stray on 26 Nov, 2010 08:24 PM

    Stray's Avatar

    Excellent! Don't worry - this happens to people quite a lot!

  7. Stray closed this discussion on 10 Feb, 2011 04:52 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac