[Solved] [BUG] EventListener crash application

Hi team.
i currenly build my application from GitHub version [7442c0d7321cf379f303e942b65ad1bbe2916209]
and looks like there is a error for Android

I/DEBUG   ( 3561):     r0 00000000  r1 43f00000  r2 43900000  r3 0c000000
I/DEBUG   ( 3561):     r4 400ebbc8  r5 7a1723a8  r6 00000298  r7 7a6b1390
I/DEBUG   ( 3561):     r8 00000002  r9 7ad84e34  sl 7aea5da0  fp 77a0105c
I/DEBUG   ( 3561):     ip 00000000  sp 78e0f820  lr 7752820c  pc 772072e8  cpsr 80070030
I/DEBUG   ( 3561): 
I/DEBUG   ( 3561): backtrace:
I/DEBUG   ( 3561):     #00  pc 0042f2e8  /data/app-lib/net.orionlab.netshooter-1/libMyGame.so (HelloWorld::onMyTouchEnded(cocos2d::Touch*, cocos2d::Event*)+39)
I/DEBUG   ( 3561):     #01  pc 00750208  /data/app-lib/net.orionlab.netshooter-1/libMyGame.so (cocos2d::GLView::getVisibleSize() const+80)
I/DEBUG   ( 3561): 
I/DEBUG   ( 3561): stack:
I/DEBUG   ( 3561):          78e0f7e0  00037ea1  
I/DEBUG   ( 3561):          78e0f7e4  4030e8e7  /system/lib/libgui.so (android::Surface::queueBuffer(ANativeWindowBuffer*, int)+302)
I/DEBUG   ( 3561):          78e0f7e8  79746b68  
I/DEBUG   ( 3561):          78e0f7ec  7ad5e028  
I/DEBUG   ( 3561):          78e0f7f0  00001002  
I/DEBUG   ( 3561):          78e0f7f4  00000000  
I/DEBUG   ( 3561):          78e0f7f8  00000000  
I/DEBUG   ( 3561):          78e0f7fc  00000000  
I/DEBUG   ( 3561):          78e0f800  00000000  
I/DEBUG   ( 3561):          78e0f804  00000000  
I/DEBUG   ( 3561):          78e0f808  78e0f86c  [stack:3494]
I/DEBUG   ( 3561):          78e0f80c  7a1723a8  
I/DEBUG   ( 3561):          78e0f810  00000298  
I/DEBUG   ( 3561):          78e0f814  7755099c  /data/app-lib/net.orionlab.netshooter-1/libMyGame.so (cocos2d::Director::getVisibleSize() const+32)
I/DEBUG   ( 3561):          78e0f818  400ebbc8  
I/DEBUG   ( 3561):          78e0f81c  772072e7  /data/app-lib/net.orionlab.netshooter-1/libMyGame.so (HelloWorld::onMyTouchEnded(cocos2d::Touch*, cocos2d::Event*)+38)
I/DEBUG   ( 3561):     #00  78e0f820  00004024  
I/DEBUG   ( 3561):          78e0f824  79748ee0  
I/DEBUG   ( 3561):          78e0f828  00000000  
I/DEBUG   ( 3561):          78e0f82c  79b79008  
I/DEBUG   ( 3561):          78e0f830  00000000  
I/DEBUG   ( 3561):          78e0f834  75dda120  /system/lib/libMali.so
I/DEBUG   ( 3561):          78e0f838  00000007  
I/DEBUG   ( 3561):          78e0f83c  00000008  
I/DEBUG   ( 3561):          78e0f840  00000008  
I/DEBUG   ( 3561):          78e0f844  00000008  
I/DEBUG   ( 3561):          78e0f848  78e0f86c  [stack:3494]
I/DEBUG   ( 3561):          78e0f84c  a49f6ff2  
I/DEBUG   ( 3561):          78e0f850  0000000b  
I/DEBUG   ( 3561):          78e0f854  77558ddc  /data/app-lib/net.orionlab.netshooter-1/libMyGame.so (std::_Hashtable<std::string, std::pair<std::string const, cocos2d::EventDispatcher::EventListenerVector*>, std::allocator<std::pair<std::string const, cocos2d::EventDispatcher::EventListenerVector*> >, std::__detail::_Select1st, std::equal_to<std::string>, std::hash<std::string>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true> >::_M_find_before_node(unsigned int, std::string const&, unsigned int) const+84)
I/DEBUG   ( 3561):          78e0f858  88548344  
I/DEBUG   ( 3561):          78e0f85c  77a019b0  

Header

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

using namespace cocos2d;

class HelloWorld : public Layer {
public:
    static Scene* createScene();
    virtual bool init();
    CREATE_FUNC(HelloWorld);
    
    bool onMyTouchBegan(Touch *touch, Event *unused_event);
    void onMyTouchEnded(Touch *touch, Event *unused_event);
};

#endif

Implementation

#include "HelloWorldScene.h"

Scene* HelloWorld::createScene() {
    auto scene = Scene::create();
    auto layer = HelloWorld::create();
    scene->addChild(layer);
    return scene;
}

bool HelloWorld::init() {
    if (!Layer::init()) {
        return false;
    }
    
    auto listener = EventListenerTouchOneByOne::create();
    listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onMyTouchBegan, this);
    listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onMyTouchEnded, this);
    this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);

    return true;
}

bool HelloWorld::onMyTouchBegan(cocos2d::Touch *touch, cocos2d::Event *unused_event) {
    return true;
}

void HelloWorld::onMyTouchEnded(cocos2d::Touch *touch, cocos2d::Event *unused_event) {
    //this->removeChildByTag(100);

    Size winSize = Director::getInstance()->getVisibleSize(); // <<< CRASH HERE >>>

    std::string lblText = StringUtils::format("Touch(%f,%f) WinSize(%f,%f)",
                                              touch->getLocation().x, touch->getLocation().y,
                                              winSize.width, winSize.height);
    Label *lblPos = Label::createWithTTF(lblText, "fonts/arial.ttf", 35);
    lblPos->setPosition(Vec2(winSize.width/2, winSize.height/2));
    this->addChild(lblPos, 100, 100);
}

hmm looks like i am wrong with Topic header, it need to be rename from ‘Director getVisibleSize crash application’ to ‘EventListener crash application’

Hi
At first glance the code seems right.
If you change the line:

Size winSize = Director::getInstance()->getVisibleSize();

with:

Size winSize {1280, 720};

all works fine?

Hi, have you found a solution? I just ran into the same problem(nothing to do with visible zise, I’m doing something entirely different). Does your app crash in Visual Studio? Or only on android?

Also, I’ve tried running my code outside of the touch listener and it works, so it’s a handler problem.
And I’ve also tried overriding layer’s onTouchBegan and onTouchEnded like this:

virtual void onEnter() override;
virtual void onExit() override;

Same result, the same happens in onTouchBegan.

@drakon99
no it does not help, the same error happens related to EventListener

@Boby
no i don’t find solution, it looks like some error deep inside at cocos2dx engine.
i write my code at Xcode then compile and run project for Android, i don’t try to run it with iOS.

I’ve managed to solve my problem, well kind of. What I was doing in the listener was that I was calling a method on a singleton that was calling another method on another singleton. Both existed and were initialized, so I don’t know what the problem was. I fixed it by calling the second singleton directly. I don’t know why this works and the previous method doesn’t. Calling on both singletons, as I did at first works elsewhere in code, but not on a touch event.

I’ve also tried your code and it broke like you said, but I didn’t have the font. After changing your code with a BMFont that I definitely knew that existed, your error also disappeared.

You should try your code in Xcode targeting MacOS, it’s faster debugging and you can see more easily if you’re missing something, like a font file. Also, your code will probably work when targeting MacOS, mine worked every time when targeting Win32.

@Boby
yes you are right, i will try to run it as Mac application, i will back later today.

@Boby
looks like i am asshole :smile:
inspected my code carefully with Xcode and debugger and found that i try to use not initialized variable at onTouchEnded, so my problem is solved.

Glad you solved it, anyway, it’s strange that some of these obvious errors break or not your app depending on the platform.