Did anybody seen this issue? Crash on OSX with "BAD-ACCESS"

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Did anybody seen this issue? Crash on OSX with "BAD-ACCESS"

Post by ONEEYEMAN »

Hi, ALL,
I am trying to create an application for all 3 major platforms.
I usually perform my tests on Windows and Linux, and when everything works there go to OSX.

On OSX I have: OSX 10.8, latest Xcode for that version (I think it is version 4.x). I am using wxWidgets 3.1 all across platform. I am compiling it against Cocoa with C++11 (again - all across platforms).

So yesterday I tried my application on OSX and it crashed with the "BAD-ACCESS". (I'm in US and have US version of the OS).
In the application I have a dialog which have a wxTextCtrl. This text control have a size restriction - you can't enter more that 130 characters.

So, trying to reproduce the crash and debug it I started my application pull this dialog enter some text (all in English) "This is a test table" and hit "Apply". When looking at the debugger at the time of the crash I see that the variable contains "This is a test table <some garbage data>".

Now what I'm doing is I am converting this string (wxString from the call to wxTextCtrl::GetValue()) to std::wstring using wxString::ToStdWstring(). My guess is that problem is somewhere during the conversion, since as I said Windows and Linux works OK.

So now my questions are:
1. Does anybody works with OSX and come here to help? Most people here are using Windows and Linux only.
2. If you do - did you see something like this happenning? What would be the best course to debug this? Is there a unit test/sample which I can check to see whether it is wxWidgets or OSX or me?

Thank you in advance.
djmig
Earned a small fee
Earned a small fee
Posts: 23
Joined: Wed Feb 10, 2016 11:18 pm

Re: Did anybody seen this issue? Crash on OSX with "BAD-ACCESS"

Post by djmig »

Hi
I mostly develops for OSX. Used to develop on OSX 10.9 then 10.10, and now on 10.11 as Apple releases new OS versions. Been using wxWidgets 3.1 for a little more than a year. As you do, I compiled wxWidgets with Cocoa and C++11.

As for your problem, is it possible that you did not include --enable-unicode flag on configure?

Here is my test with wxTextCtrl:

Code: Select all

//convtest.h
#ifndef _TEXTCTRLAPP_H_
#define  _TEXTCTRLAPP_H_

#include <wx/wx.h>

const int ID_BUTTON = 10000;

class TextCtrlApp : public wxApp
{
public:
	bool OnInit();
};

class TextCtrlFrame : public wxFrame
{
public:
	TextCtrlFrame();
	~TextCtrlFrame();
	void OnButton1Click(wxCommandEvent &);
	void OnCloseWindow(wxCloseEvent &);
	void TextInfo(void);

protected:
	wxTextCtrl *tctrl;
	wxButton *button;
	std::wstring converted;
};

#endif		//  _TEXTCTRLAPP_H_

Code: Select all

//convtest.cpp
#include "convtest.h"

bool
TextCtrlApp::OnInit()
{
	TextCtrlFrame *frame = new TextCtrlFrame;
	frame->Show(true);

	return true;
}

TextCtrlFrame::TextCtrlFrame()
			: wxFrame(NULL, wxID_ANY, wxT("TextCtrl Sample"), wxDefaultPosition, wxSize(300,250))
{
	wxPanel *panel = new wxPanel(this, -1);
    tctrl = new wxTextCtrl(panel, wxID_ANY, wxT(""), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
    button = new wxButton(panel, ID_BUTTON, wxT("Apply"));

	wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
    vbox->Add(tctrl, 1, wxEXPAND|wxALL, 10);
    vbox->Add(button, 0, wxLEFT|wxRIGHT|wxBOTTOM|wxALIGN_RIGHT, 10);
    panel->SetSizer(vbox);
	this->SetMinSize(wxSize(300, 150));

	this->Bind(wxEVT_BUTTON, &TextCtrlFrame::OnButton1Click, this, ID_BUTTON);
	this->Bind(wxEVT_CLOSE_WINDOW, &TextCtrlFrame::OnCloseWindow, this, -1);
}

TextCtrlFrame::~TextCtrlFrame()
{
	
}

void
TextCtrlFrame::OnButton1Click(wxCommandEvent &event)
{
	wxString text = tctrl->GetValue();
	wxPrintf(text + "\n");
	converted = text.ToStdWstring();
	std::wcout << converted << std::endl << std::endl;
	TextInfo();
}

void
TextCtrlFrame::OnCloseWindow(wxCloseEvent &event)
{
	Destroy();
}

void
TextCtrlFrame::TextInfo()
{
	std::cout << "sizeof(wchar_t)     : " << sizeof(wchar_t) << std::endl;
	std::wcout << L"converted           : " << converted << std::endl;
	std::cout << "sizeof(converted)   : " << sizeof(converted) << std::endl;
	std::cout << "converted(bytes)    :" ;
	
	int max = converted.length();
	for(int i = 0; i < max; i++) {
	   std::cout << " " << static_cast<unsigned int>(static_cast<unsigned short>(converted[i]));
	}
	
	std::cout << std::endl;
}

IMPLEMENT_APP( TextCtrlApp )
And the output:
Screen Shot 2017-07-27 at 10.06.10.png

Code: Select all

$ ./convtest 
This is a test table
This is a test table

sizeof(wchar_t)     : 4
converted           : This is a test table
sizeof(converted)   : 24
converted(bytes)    : 84 104 105 115 32 105 115 32 97 32 116 101 115 116 32 116 97 98 108 101
Try it out and see what you get
briceandre
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 672
Joined: Tue Aug 31, 2010 6:22 am
Location: Belgium

Re: Did anybody seen this issue? Crash on OSX with "BAD-ACCESS"

Post by briceandre »

Dear Oneeyman,

Unfortunately, having a program that runs under Windows and Linux does not mean that you do not have a bug in it.

I am currently doing something similar to you (I am porting a new module of an application developed under Windows on Mac OSX) and I have myself several platform related issues.

From the very few information you provided, the most probable cause is a memory corruption. And yes, it is very possible of having bugs in your application that have no impact under Windows, and generate memory corruptions on Mac (or vice-versa). The last one I personally encountered (and that I remember...) was an application that was manipulating a wxTimer from a secondary thread : under Windows, no problem, under Mac, memory corruption...

Without any more information, it will be hard to help you.. A few guesses:
  • Do you develop a multi-thread application ?
  • Do you use additional third party libraries ?
  • Do you perform string manipulation outside wxString class ? Do you perform any other memory management operations (new/delete, malloc/free, memory copy in buffers, etc.)
If your code is not too big, maybe can you post it ?

Regards,
Brice
Post Reply