Critical error when calling _("str").mb_str(wxConvUTF8)

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
User avatar
Randalph
In need of some credit
In need of some credit
Posts: 8
Joined: Thu Jun 28, 2018 12:34 am

Critical error when calling _("str").mb_str(wxConvUTF8)

Post by Randalph »

I'm using the wxMSW-3.1.3_vc14x_x64_Dev.7z binaries. The program I'm working on is being debugged under Visual Studio Code. When I use the following line, everything works fine:

Code: Select all

    
    char* psz = (const_cast<char*>((const char*) _("foo")));
However, if I instead use the line:

Code: Select all

 
     char* psz = (const_cast<char*>((const char*) _("foo").mb_str(wxConvUTF8)));
I get a "Critical error detected c0000374" and the debugger stops in the following function (in buffer.h) where it attempts to delete m_data.

Code: Select all

 
    void DecRef()
    {
        if ( m_data == GetNullData() ) // exception, not ref-counted
            return;
        if ( --m_data->m_ref == 0 )
            delete m_data;
        m_data = GetNullData();
    }
Unfortunately, I wasn't using this prior to 3.1.3, so I don't know if this has never been a valid way to convert to a UTF8 string (though wxWidgets docs say it should work), or if it's a bug perhaps new to 3.1.3.

My question is, if this is expected behavior, then what is the correct way to convert a wxGetTranslation() string into a UTF8 string?
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Critical error when calling _("str").mb_str(wxConvUTF8)

Post by doublemax »

Use the source, Luke!
User avatar
Randalph
In need of some credit
In need of some credit
Posts: 8
Joined: Thu Jun 28, 2018 12:34 am

Re: Critical error when calling _("str").mb_str(wxConvUTF8)

Post by Randalph »

The examples you pointed to me (which is where I derived the original example from) also generate a critical error. The following is an exact copy of one of the examples which generates the same critical error:

Code: Select all

    wxString mystring(wxT("HelloWorld"));
    char cstring[1024];
    // assuming you want UTF-8, change the wxConv* parameter as needed
    strncpy(cstring, (const char*)mystring.mb_str(wxConvUTF8), 1023);
Just to be clear, the critical error occurs before the call to mb_str returns -- I never get a return pointer of any type.

I did some further testing with one of the other examples, and it appears that it is not specific to wxConvUTF8, but rather the call to mb_str(). The following will also crash in the call to mb_str():

Code: Select all

    wxString str(wxT("HelloWorld"));
    char* pszTmp = (const_cast<char*>((const char*)str.mb_str()));
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Critical error when calling _("str").mb_str(wxConvUTF8)

Post by doublemax »

I've never used the prebuilt binaries. Can you please try to build wxWidgets from the sources yourself? With the provided VS solution files that is a trivial task.

And just for the record:

Code: Select all

char* pszTmp = (const_cast<char*>((const char*)str.mb_str()));
You should not use a pointer returned from wxString::mb_str (and similar methods) like this, as it points to a temporary object which can be already destroyed at that time.
Use the source, Luke!
Post Reply