DispatchEvent results in Runtime Error

If dispatchEvent is called from a listener it results with a Runtime Error.

Here is a sample code:

local circle = display.newCircle( 100, 100, 20 )  
  
local function myEventListener()  
 print("my event occured")  
end  
  
local function myTimerHandler()  
 circle:dispatchEvent( { name="myEvent" } )  
end  
  
circle:addEventListener ( "myEvent", myEventListener )  
  
timer.performWithDelay ( 1000, myTimerHandler, 1)  

Output:
[bash]
my event occured
Runtime error
?:0: attempt to index a nil value
stack traceback:
[C]: ?
?: in function ‘?’
?: in function <?:215>
[/bash] [import]uid: 46529 topic_id: 14512 reply_id: 314512[/import]

[code]
local circle = display.newCircle( 100, 100, 20 )
local function myEventListener(event)
print(“my event occured”)
return;
end

circle.myEvent = myEventListener;
circle:addEventListener ( “myEvent”, circle )

local function myTimerHandler(event)
circle:dispatchEvent( {name = “myEvent”,target=circle})
end

timer.performWithDelay ( 1000, myTimerHandler,1)

[/code] [import]uid: 24 topic_id: 14512 reply_id: 53787[/import]

so is this a workaround or expected behavior? [import]uid: 46529 topic_id: 14512 reply_id: 53796[/import]

d) none of the above
e) all of the above
f) whack carlos for being a smart ass

  • the way should be

c.
[import]uid: 24 topic_id: 14512 reply_id: 53797[/import]

well i don’t get it. do you know why it does result with a runtime error if a function listener used?

If called within normal flow dispatchEvent seems to work even with a function listener, but if I call it from timer listener or transition onComplete, it gives the error. your solution is ok, somewhere in the forum I think I saw it before. but i still need to know if it is accepted as a bug to be fixed, or not. [import]uid: 46529 topic_id: 14512 reply_id: 53800[/import]

during this weekend’s code fest, i ran into this error many times. yes, i’m using timers/callbacks/event dispatch, etc. i’m pretty forgiving when it comes to bugs in apps, however it was disconcerting to find that this error could popup in seemingly totally unrelated areas.

for example, i added a line in a different method (outside of the callback, etc) which changed isVisible on a display group and the error would occur. remove the line and it would go away.

i think this is a pretty serious issue since 1. the traceback is worthless 2, randomness of the bug and 3. it causes a serious error to happen in the application.

i’d be happy to send code samples if you need them (and when it occurs again). i’ve managed to make them go away without needing to apply the fix, but unfortunately don’t know what i did exactly.

cheers,
dmc

ps, using Mac build 591 [import]uid: 74908 topic_id: 14512 reply_id: 56420[/import]

This is not a bug.

There are 2 ways to listen for a custom event: TABLE (OBJECT) listeners and FUNCTION listeners, and I think you are confusing/mixing the syntax. Very easy mistake to make.

My suggestion is to learn one syntax well, then learn the second one. And I would start with TABLE (OBJECT) syntax, because it will help you avoid potential confusion about variable scope issues that could occur with the function syntax.

[lua]-- THIS EXAMPLE USES TABLE (OBJECT) SYNTAX

– Using table/object syntax, the listener is your circle object (renamed
–to help illustrate)
local myListenerObj = display.newCircle( 100, 100, 20 )

– You defined a function to be called. But it was a local function.
– Instead, you need to make this function belong to the listener obj as
– a method of that object. Originally, you called this “myEventListener”
– which can add to your confusion. Instead, we will now call this
– “myHandlerFunc” and attach it as a method of myListenerObj

– WAS: local function myEventListener()
function myListenerObj:myHandlerFunc()
print(“my event occured”)
end

– Now tell our circle to listen for events called “myHandlerFunc”
myListenerObj:addEventListener(“myHandlerFunc”, myListenerObj)

– Now we wait 1 second and trigger the dispatch event method
timer.performWithDelay ( 1000, function()
myListenerObj:dispatchEvent( {name=“myHandlerFunc”} )
end, 1)[/lua]

[import]uid: 10818 topic_id: 14512 reply_id: 56435[/import]

@duneunit

how could you call it as “not a bug”. it is obviously a bug because it gives a runtime error if you dispatch a function listener within a callback. In normal program flow you can use function listeners but from callbacks it gives runtime error. Carlos offered a workaround by using table listeners but i really want to be able to use function listeners as well. [import]uid: 46529 topic_id: 14512 reply_id: 56437[/import]

@culutas

I should have said “I THINK this is not a bug.” :wink:

I took a quick look and thought it was just a confusion between the two possible methods. I know when I first messed with Corona it kept throwing me off, so I thought that’s what it was. I have been pushing against a deadline so I want to play with this more and understand what is going on cuz, well, I’m a curious kind of guy.

If using the function syntax, when I look at it, I think it is a scope thing. But I will look again when I have more time. [import]uid: 10818 topic_id: 14512 reply_id: 56502[/import]

I get this bug constantly - I cant belive its not appearing for more people - to dispatch an event in a timer handler is such a common act… really frustrating. [import]uid: 81475 topic_id: 14512 reply_id: 65784[/import]