Error #1023: Stack overflow occurred - actionscript-3

I've read a lot of posts about this error. Yet it does not solve my problem. In my case, there are loop working through two funcions like this:
function getData() {
//doing some stuff here
call_socket("data", callback); //When response from socked server has been received, call callback function
}
function callback(data:Object) {
if(!data) { getData(); } //if data is null, continue loop
else { //stop the loop }
}
I got an error on both of these functions:
Error #1023: Stack overflow occurred.
I understand that this error tells me that I'm calling the functions repeatedly, but in my case, that is what I got to do there.
Is there any way to solve this?

The problem is in the logic of your code. Just think about situation when your call_socket does not return data for some time. This will result in numerous circles of callback and getData. Depending on your app you can achieve desired result in 2 ways:
You should use some Event to notify other parts of your app that the data has arrived (instead of callback)
You can poll the server at defined time spans. You may use Timer for this.

Related

Create delay in ActionScript 3 function

I have a function in Adobe Flex 4 (ActionScript 3) that accepts an object and returns an ArrayCollection...
If a certain global variable is set to true, I want the function to delay itself for 3 seconds before running. Otherwise I want the function to run as normal.
The problem is, if I use a Timer, that timer calls a separate function, and that function cannot return anything to my calling function, nor can the function it calls accept any parameters, so it's not like I can call my own function recursively after the TimerComplete event fires... And a recursive call wouldn't work anyway, because it would return the ArrayCollection to the timer-result function, not to the original calling function...
I need a delay within the function, not a delay that causes me to go outside that function. But I cannot figure out how to do it.
Something like this is what I need to do:
private function createArrayCollection(myObject:Object):ArrayCollection {
var myArrayCollection:ArrayCollection = new ArrayCollection();
if (globalWaitBoolean) {
//delay here for 3 seconds, somehow
}
//Here I do the stuff that uses the info in myObject to figure out what to
//put into the ArrayCollection I want to return
return (myArrayCollection);
}
So... Any ideas on how to accomplish this without calling an external Timer function that cannot return an object back to my original function?
Thanks,
The way you want it you will have your whole application to lag for 3 seconds, unresponsive to any user input and external events. But it is possible, sure:
import flash.utils.getTimer;
private function createArrayCollection(myObject:Object):ArrayCollection
{
var myArrayCollection:ArrayCollection = new ArrayCollection;
if (globalWaitBoolean)
{
var waitUntil:int = getTimer() + 3000;
// Method getTimer() returns time in ms passed since app start.
// So you just have to wait until it is greater than appointed time.
while (getTimer() < waitUntil)
{
// Do nothing.
}
}
return (myArrayCollection);
}
Still, if you want to do it in a correct way of doing it:
import flash.utils.setTimeout;
private function callerMethod():void
{
// Blah blah blah.
// ...
// Finally.
createArrayCollection(sourceData, asyncResult);
}
private function createArrayCollection(myObject:Object, handler:Function):void
{
var result:ArrayCollection = new ArrayCollection;
if (globalWaitBoolean) setTimeout(handler, 3000, result);
else handler(result);
}
private function asyncResult(source:ArrayCollection):void
{
// The rest of your processing code.
}
Normal (synchronous) code flow would not return until the value is prepared, so should you desire to actually wait for 3 seconds while not allowing your app to do anything, use getTimer() approach from #Organis's answer. If you'll go for an asynchronus result, you'll need to face and overcome some more problems.
First, when do you expect your returned ArrayCollection to actually arrive. Speaking of code design, asynchronous code requires a whole lot of assumptions, thread safety etc etc, and even while AS3/Flash does not have true multithreading unless you count Workers, the code flow with events is not as obvious. So, whoever called your createArrayCollection() MUST NOT expect value returned from it right away. So, speaking about your direct question, NO, you can't avoid timers of some sort if you desire a responsive application. But you can use them with an approach that would involve an indirectly returned result.
Second, whether there might be concurring requests for more array collections from objects if your app would require these - you have to prepare for any kind of interference that might be caused by this. Say your function is triggered by a button click - what if that button would get clicked more than once in 3 seconds?
Third, actual route to processing code is not direct with asynchronous return. You need either a callback, an event handler (which is essentially a semi-native callback), a code that periodically checks for value presence (enter frame handler, etc) or a similar trick to gather the value that's returned asynchronously, and then transfer it to any relevant code that would process it further. Therefore, you would need to design an interface capable of receiving complex data (source object forward, array collection backward) and then carefully test it against all the possible cases and flaws.
An example of implementing all that is very long, I'll try to outline it somehow. Ler's assume you have a sort of "server" class that accepts requests for data and processes it synchronously (no wait) or asynchronously (wait). It accepts a source object of type "T" and provides a newly created object of type ArrayCollection, supplied as a parameter to whatever callback function sent to it. Also it accepts a delay (a simple way to show sync/async return would be a boolean, but why not getting an int?) as a parameter, and guarantees (to the extent of event model limitations) that after this delay the callback will be called ASAP. The architecture will then look like this:
class Processor {
Dictionary requests; // here all the requests that are delayed will be stored
public function dpr(source:T,callback:Function,delay:int=0):void{...}
// creates requests and stores them
private function syncProcess(source:T):ArrayCollection {...}
// whatever routine you want to get variably delayed
private function processTimeout(e:Event=null):void {...}
// processes events from "setTimeout()" and calls callbacks
}
Note that asynchronous approach forced to create three more entities than a synchronous one. First is the request holding structure (the dictionary here), second is timeout event handler, third is whatever callback you'll desire to get called when the data is ready. The code flow would go like this:
Synchronous call would result in the callback directly called from within the class: request->processTimeout->syncProcess()->callback. Asynchronous call will have the callback called from within Timer::timerComplete event handler via setTimeout called within request, with data that originally came from request stored in requests.
You could use an embedded/inline function:
private function createArrayCollection(myObject:Object):ArrayCollection {
var myArrayCollection:ArrayCollection = new ArrayCollection();
if (globalWaitBoolean) {
var milliseconds:int = 3000;
//delay here for 3 seconds
setTimeout(function()
{
//Here I do the stuff that uses the info in myObject to figure out what to
//put into the ArrayCollection I want to return
return (myArrayCollection);
},
milliseconds);
}
else
{
//Here I do the stuff that uses the info in myObject to figure out what to
//put into the ArrayCollection I want to return
return (myArrayCollection);
}
}
The inner function will have access to all local vars of the outer function.

Parsley Command Decoupled Result Handlers and Observers

I posted a question last night that after reading back sounded awful, so I deleted it and have come back to try again, this time properly.
I have a Flex Mobile App, that uses Parsley, everything works as expected but I am trying to do use a decoupled result handler in my controller, but it is not firing when I expect it to, so would like a pointer as to why.
The command look like this:
public function execute():void
{
var asyncToken:AsyncToken = Db.Instance.ViewChildren(mainModel.loggedInUser.userId);
asyncToken.addResponder(new Responder(result, error));
}
public function result(result:ResultEvent):void
{
callback(result.result);
}
public function error(event:FaultEvent):void
{
callback(event.fault);
}
Which works as expected, the command is executed and the result handler handles the result, the problem comes when I try to put a message handler in the controller for the view.
[CommandResult]
public function handleResult(result:AsyncToken):void
{
trace("result in the controller");
}
[CommandError]
public function handleError(fault:AsyncToken):void
{
trace('error: ' + fault.fault.faultDetail);
}
Neither of these listeners fire when a result arrives, so I did the obvious thing and changed the code to:
[CommandResult]
public function handleResult():void
{
trace("result in the controller");
}
[CommandError]
public function handleError():void
{
trace('fault in controller);
}
Now it fires, but I have no data handle.
I did think of changing the commands execute method to
public function execute():AsyncToken
{
return Db.Instance.ViewChildren(mainModel.loggedInUser.userId);
}
as after all it does return an AsyncToken, but then the command doesn't fire at all (it is part of a 2 command sequence that is mapped to an event called ChildEvent, this is the second and last event in the chain.
So in summary, I want the above to work, but want to be able to manage the result in the decoupled result handler, but I can't work out how, the parsley manual is great for getting to this point (http://www.spicefactory.org/parsley/docs/3.0/manual/?page=commands&section=intro), but the finer details are a little sketchy.
Thanks
With a small tweak to the Controller code, we end up with this:
[CommandResult(type="view.user.UserEvent", selector="loadUser")]
public function handleResult(result:Object):void
{
trace("this is the command result");
}
OR
[CommandResult(selector="loadUser")]
public function handleResult(result:Object, trigger:UserEvent):void
{
trace("this is the command result");
}
Now this fires, I get an Object with my data in, resolved.
It would be useful to note that the manual for Parsley 3.0 misses out the section that explains how this actually works. I eventually found it in the Parsley 2.2 manual (the equivalent section in the 3.0 manual has been removed!) But if you ever need it http://www.spicefactory.org/parsley/docs/2.2/manual/messaging.php#command_methods
Thanks everyone!

AS3 Asynchronism problems

I had a strange bug in my program which I fortunately found quite quickly but am still puzzled why it was happening. Essentially it was to do with the order of commands in the source code and event listeners, here is the example:
function detectFaces(loader:ImageLoader)
{
var detector:FaceDetector=new FaceDetector();
detector.addEventListener(FaceDetectorEvent.FACE_CROPPED,facesDetected);
detector.loadFaceImageFromBitmap(loader.bitmap);
var something:Number = stage.width;
function facesDetected(e:FaceDetectorEvent):void{
trace(something);
}
}
Operation that raise the event here is not important, only thing to note about it would be it takes around 100ms. What I get as trace output is NaN and I don't know why that is since line declaring the variable something will definitely be called before callback of facesDetected and it is in scope of the handler function declared under it. This problem was easy to solve by just moving var something:Number = stage.width; before loadFaceImageFromBitmap(..) method, but I would really like to know why this is happening?
I am suspecting this is not due to order of execution but has something to do with passingByValue and passingByRefrence deferences but don't know how would these cause an error like this.
EDIT: Now I am even more puzzled... This code works in any order of declaration:
timers();
function timers()
{
var timerTest:Timer = new Timer(100,1);
timerTest.addEventListener(TimerEvent.TIMER,onTime);
//BEFORE DECLARATION
timerTest.start();
var something:Number = stage.width;
function onTime(e:Event)
{
trace("SOMETHING :"+something);
}
}
timers();
function timers()
{
var timerTest:Timer = new Timer(100,1);
timerTest.addEventListener(TimerEvent.TIMER,onTime);
var something:Number = stage.width;
//AFTER DECLARATION
timerTest.start();
function onTime(e:Event)
{
trace("SOMETHING :"+something);
}
}
With regard to your initial question, actionscript will complete the execution of a block of code before it continues to execute subsequent lines. If there was nothing asynchronous happening in your loadFaceImageFromBitmap method (ie, if your weren't using a Loader or some other object that had to wait for an event to fire) then, however long the code takes to execute, the FACE_CROPPED event will still fire before 'something' is set to a value.
As for the other problem, it looks to me like the answer is simply that you're using a TimerEvent - Actionscript will acknowledge that it shouldn't wait for the event to fire before continuing to execute code; It will, therefore, declare 'something' before the 100 miliseconds passes. So, in this case, because you're using an event, the code WILL continue 'reading' and executing the lines following the event listener.
The code of the function loadFaceImageFromBitmap run on a sync way. The FaceDetectorEvent.FACE_CROPPED event listener is invoked inside of that function, it is not a callback declared to run after some response is returned for ie(http request).
In the case of the Timer it works as expected, because event listener is not invoked right at the start moment, it waits for X time.

using AsyncToken and AsyncResponder

I wanna get two level results of an hierarchical structure. First results of AsyncToken is Ok,then I set a for loop and calling next level results by the same way in each loop step. Here is the problem, token2.addResponder(responder2); never return results on time,on each loop step. It sends me results after all for loop results. But I need in each step .
Can you please tell me about my wrong ?
public function getChildResultHandler(event:ResultEvent, token:Object=null ):void
{
myObject=event.result; //first asynToken result is Ok
var myArrayCol:ArrayCollection=new ArrayCollection();
myArrayCol=ArrayCollection(myObject);
var mlObject:MLObject=new MLObject();
var i:int;
for(i=0;i<myArrayCol.length;i++)
{
mlObject=myArrayCol[i];
if (mlObject.Type=="Bin")
{
token2=new AsyncToken(null);
token2=myService.GetChildObjects(sessionID,mlObject.ObjectID);
responder2=new AsyncResponder(getNextLevelChild,getChildFaultHandler);
token2.addResponder(responder2);
// I cant get results here
// this query goes to getNextLevelChild func after for loop results
}
}
}
Do I have to wait for both results, if yes, how can I wait the responder or token ?
Because, these are async calls. It's not possible to know order of returned results. May be first call will end first, may be second one.
So, we have to wait the finishing time of all async calls for correct results.
This tutorial might help because it solves a problem very similar to what you're experiencing. There are 3 remote procedure calls made passing in the letters ABC. The calls are returned in the order BAC. The tutorial demonstrates how to use ASyncToken and Responders to work out which result handler is firing for which call.

Adobe Cirrus Error on Direct Connect"Property startTransmit not found on flash.net.NetStream"

The error:
ReferenceError: Error #1069: Property startTransmit not found on flash.net.NetStream and there is no default value.
I've played around with cirrus plenty of times before and have yet to see this error before. But now I cant get it to go away.
My p2p Direct connect works great just fine. But every single time i see this error pop up. It throws an exception. I can't figure out where it's exactly happening.
Has anyone encountered this before? Any ideas where I should look?
Every client object needs to have the following functions defined.
client.stopTransmit=function($p1:*,$p2:*):void{
trace("stopTransmit called",$p1,$p2);
}
client.startTransmit=function():void{
trace("startTransmit called");
}
For example, set these in the onPeerConnect function:
sendStream.client = new Object();
sendStreamClient.onPeerConnect = function(subscriber:NetStream): Boolean{
var client:Object=new Object();
client.stopTransmit=function($p1:*,$p2:*):void{
trace("stopTransmit called",$p1,$p2);
}
client.startTransmit=function():void{
trace("startTransmit called");
}
subscriber.client=farStreamClient;
}
Additionally these need to be set on your sendStreamClient's client property:
sendStreamClient.client.stopTransmit=function($p1:*,$p2:*):void{
trace("stopTransmit called",$p1,$p2);
}
sendStreamClient.client.startTransmit=function():void{
trace("startTransmit called");
}
And they need to be set on your recieveStreamClient's client property.
On the server side script, you probably (or somebody else) have set up the application, so that it calls back a function -this time it is startTransmit-, and it isn't handled on the client side.
Remove the code from the server, or add a default value, or add a function to your code.
In my similar program, i had to add the function to my code (in my case it was not 'startTransmit') :
if ("NetConnection.Connect.Success" == e.info.code) {
netConnection.client=new Object();
netConnection.client.startTransmit=startTransmit; //no columns!
}
where startTransmit is
private function startTransmit():Boolean{
return true;
}
Are you sending h264 videos? I think it is to do with that...
If you add
public function startTransmit($p1:*,$p2:*):void{
}
public function stopTransmit():void{
}
where you have your media server connection it should work fine, at least it does for me :)
There is another netstream other than receiveStream and sendStream. You should set startTransmit and stopTransmit functions on the callerns netstream, something like this:
sendStreamClient.onPeerConnect = function(callerns:NetStream): Boolean{
var farStreamClient:Object=new Object();
farStreamClient.stopTransmit=function($p1:*,$p2:*):void{
trace("-------------farStream stopTransmit called!",$p1,$p2);
}
farStreamClient.startTransmit=function():void{
trace("-------------farStream startTransmit called!");
}
callerns.client=farStreamClient;
}
The problem is not on AMS or Red5 server. Even transmitting a video on P2P from an Android device triggers the same error.
The solution worked.
Actually the stopTransmit() sends a Boolean and an integer.
It would be amazing to know what they mean.
I have opened a bug on adobe bugbase in order to have it documented or removed.
Please vote:
https://bugbase.adobe.com/index.cfm?event=bug&id=3844856