COCOS2DX 2.x version in Android under Cclabelttf a bug

Source: Internet
Author: User

Cocos2dx in Android is using paint to generate images and then in the Cclabelttf display, its specific code is in the Java class Cocos2dxbitmap, after the completion of the production will call a JNI function to pass the results to the CPP layer, The CPP layer relies on a static variable to exchange data with the Java layer, as follows

       BITMAPDC &DC = SHAREDBITMAPDC ();        Cc_break_if (Dc.getbitmapfromjava (Ptext, nwidth, nheight, Ealignmask, Pfontname, nSize));        Assign the Dc.m_pdata to M_pdata in order to save time        m_pdata = dc.m_pdata;        CC_BREAK_IF (! m_pdata);

Here is a problem, SHAREDBITMAPDC is always shared, its dc.m_pdata is always saved is the last data, if some reason the Java layer is called to fail, then the CPP layer continues to get the last data, However, this data is owned by the CPP layer and the double free causes the program to crash. We encountered this situation in practice, when the stack of crashes told us that there was a problem in the Ccimage destructor.

Ccimage::~ccimage () {    cc_safe_delete_array (m_pdata); #if (cc_target_platform = = CC_PLATFORM_WINRT) | | (Cc_target_platform = = CC_PLATFORM_WP8)    Cc_safe_delete (m_ft); #endif}

But simply look at the Ccimage code is not a problem, and if there is a problem, it is a big trouble. After the final check or confirm that the CPP section may hide the bug, so the above Ccimage modified as follows

        BITMAPDC &DC = SHAREDBITMAPDC ();        Cc_break_if (Dc.getbitmapfromjava (Ptext, nwidth, nheight, Ealignmask, Pfontname, nSize));        Assign the Dc.m_pdata to M_pdata in order to save time        m_pdata = dc.m_pdata;        Dc.m_pdata = NULL;        CC_BREAK_IF (! m_pdata);

In this way, the CPP layer will no longer appear double free, and then test, this time found that Java layer thrown exception, the previous exception because the program exit is not printed at all. Go back to the Java layer and find out the specific reason is

After the setcontentsize, the Java layer according to contentsize judgment, the font is not put in, so it thinks that the height of the bitmap created is 0, and bitmap is not allowed to create a height of 0, so the exception. My solution is to let it show up in a row at least, so that we can see the display and know where the problem is, not just crash.

From this issue once again, fail early, fail loud programming habits, otherwise a simple problem becomes a complex problem.

COCOS2DX 2.x version in Android under Cclabelttf a bug

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.