Git lib build wxArrayString Segmentation fault

Do you have a question about makefiles, a compiler or IDE you are using and need to know how to set it up for wxWidgets or why it doesn't compile but other IDE's do ? Post your questions here.
Post Reply
Joel
In need of some credit
In need of some credit
Posts: 3
Joined: Wed Apr 25, 2018 5:41 am

Git lib build wxArrayString Segmentation fault

Post by Joel »

//should be a 3.1 git ticket, but the CAPTCHA/trac site is broken. :shock:

//Build issues on ubuntu prior to cross porting

Code: Select all

cmake -DCMAKE_C_FLAGS="-fPIC" \
		-DCMAKE_CXX_FLAGS="-fPIC" \
		-DCMAKE_BUILD_TYPE=RELEASE \
		-DCMAKE_EXPORT_COMPILE_COMMANDS="1" \
		-DwxBUILD_PRECOMP="0" \
		-DwxUSE_EXPAT="OFF" \		//On = does not build on MacOS or Linux
		-DwxUSE_REGEX="builtin" \
		-DwxUSE_ZLIB="builtin" \
		-DwxUSE_LIBJPEG="builtin" \
		-DwxUSE_LIBPNG="builtin" \
		-DwxUSE_LIBTIFF="OFF" \		//On = does not build on MacOS or Linux after last weeks pull
		-DwxUSE_LIBLZMA="OFF" \		//On =does not build on MacOS
		-DwxBUILD_SAMPLES="SOME" \
		-DwxBUILD_SHARED="0" \		//for the sake of porting, but will revert to native package .so for legacy testing 
		.
//Example from unit test: issues with wxArrayString seems to seg fault under large file list adds
//if I missed something obvious, than I would welcome any feedback. :)

Code: Select all

...
	strFilePaths = new wxArrayString();	//copy to public var in class 
	strFilePaths->Alloc(50);
	strFilePaths->Add(wxT("empty!"));
	
	 wxString list_image_choices[] = {
		_("drop files here..."),
	};
	image_scan = new wxListBox(feel_my_pane, wxID_ANY, wxDefaultPosition, wxDefaultSize, 1, list_image_choices, wxLB_ALWAYS_SB|wxLB_SINGLE|wxLB_SORT);
    
	//in wxFrame
	this->DragAcceptFiles(true);
	this->SetDropTarget(new DnDFile(image_scan, strFilePaths));	//may or may not bind the frame under GTK2.0
...

	
class DnDFile : public wxFileDropTarget
{
public:
    DnDFile(wxListBox *pOwner = NULL, wxArrayString *strOwner = NULL) {
	    m_pOwner = pOwner;  *strOwner;
	    };

    virtual bool OnDropFiles(wxCoord x, wxCoord y,
                             const wxArrayString& filenames) wxOVERRIDE;

private:
    wxListBox *m_pOwner;
    wxArrayString *str_pOwner;
};



bool DnDFile::OnDropFiles(wxCoord, wxCoord, const wxArrayString& filenames)
{
std::cout << "\n DnDFile::OnDropFiles \n" << std::endl;
    wxArrayString filenameTotal(filenames);
    wxFileName parseFileName;
	
        static wxSortedArrayString fileExtFilter;
        if ( fileExtFilter.empty() )
        {
            fileExtFilter.push_back(wxS("jpeg"));	//supported media files
            fileExtFilter.push_back(wxS("jpg"));
            fileExtFilter.push_back(wxS("png"));
            fileExtFilter.push_back(wxS("gif"));
            fileExtFilter.push_back(wxS("svg"));  
        }

	
    if((m_pOwner != NULL) && (str_pOwner != NULL) )
    {
	if((str_pOwner->GetCount() <= 0) || (str_pOwner->GetCount() > m_pOwner->GetCount() ) )	//empty defunt list?
	{
		m_pOwner->Clear();
		str_pOwner->Empty();
		//str_pOwner->Clear();
	}
	    
        for ( size_t n = 0; n < filenameTotal.GetCount(); n++ )
	{
		parseFileName.Assign(filenameTotal[n]);
		if( ( fileExtFilter.Index(parseFileName.GetExt().MakeLower()) != wxNOT_FOUND ) && //check file type ext
			(wxFileExists(filenameTotal[n])) &&
				( str_pOwner->Index(filenameTotal[n], true, false) == wxNOT_FOUND ) 	//remove dupes, bug: wxArrayString->Index needs all params to work
			)
		{
			
			std::cout << "\n ========== Add; " << filenameTotal[n] << std::endl;
			//m_pOwner->Insert((filenameTotal[n]), m_pOwner->GetCount()); // bug: wxArrayString* ptr Segmentation fault
			//str_pOwner->Add((filenameTotal[n])); // bug: wxListBox* ptr Segmentation fault
			m_pOwner->Append(parseFileName.GetName().MakeLower() + "." + parseFileName.GetExt().MakeLower());	//sometimes seg falts on repeated adds
			str_pOwner->Insert((filenameTotal[n]), str_pOwner->GetCount());
		}else{
			//recurse into sub-dirs?
			if (wxDirExists(filenameTotal[n]))
			{
				wxDir dir(filenameTotal[n]);
				if(dir.IsOpened())
				{
				dir.GetAllFiles(filenameTotal[n], &filenameTotal,wxALL_FILES_PATTERN, wxDIR_FILES|wxDIR_DIRS);
				}
			}
		}
	}

	std::cout << "\n Probed filenameTotal.GetCount(); \n" << filenameTotal.GetCount() << std::endl;
	std::cout << "\n ADDED str_pOwner->GetCount \n" << str_pOwner->GetCount() << std::endl;
	
	for ( size_t n = 0; n <  str_pOwner->GetCount(); n++ )
	{
		std::cout << str_pOwner->Item(n) << std::endl;
	}
    }
std::cout << "\n ==================== " << std::endl;
    return true;
}
Last edited by catalin on Thu Apr 26, 2018 7:48 am, edited 1 time in total.
Reason: code tags
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: Git lib build wxArrayString Segmentation fault

Post by PB »

Can you provide actual short self-contained compilable example (using forum code gats) and point out the line where it crashes? This will make easier to determine whether the issue is actually in wxWidgets or your (invalid index / pointer...) code.

You could also try to run your code with the debug version of the library which may be able to provide more information about the issue via assert messages and such.

You should also provide exact version of wxWidgets (IIRC there were some wxVector/wxArrayString changes recently) and relevant wxWidgets build info e.g. values of wxUSE_STL, wxUSE_STD_CONTAINERS_COMPATIBLY, and wxUSE_STD_CONTAINERS.

BTW, wxTrac seems to be working just fine for me atm.

Edit
This line looks very wrong to me but I may be wrong, your code is pretty much unreadable as you did not use the code tags

Code: Select all

dir.GetAllFiles(filenameTotal[n], &filenameTotal,wxALL_FILES_PATTERN, wxDIR_FILES|wxDIR_DIRS);
I believe that modifying the array in the loop where you are iterating it is generally not a good idea...
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Git lib build wxArrayString Segmentation fault

Post by ONEEYEMAN »

Hi,
One more thing - I presume that long CMake line in the beginning of the post is how you compiled wxWidgets?
Until the official announcement that CMake is fully supported, I'd stay away from it and use the official build approach, i.e. ..configure --enable-debug <some_other_options> && make. This way it will be immediately visible what options did you use.

Now in your own software you can use CMake to build it.
Also, just as a side not - you can try to modify one of the samples/unit tests provided with the library in order to reproduce the issue and just post a patch to the official code. This will very easy to review to anybody who will try to help you. And don't forget to post it inside the "code" tags.

Thank you.
Joel
In need of some credit
In need of some credit
Posts: 3
Joined: Wed Apr 25, 2018 5:41 am

Re: Git lib build wxArrayString Segmentation fault

Post by Joel »

Thanks for the feedback, but most of the code is from the repo samples and wxGlade.

I needed to use cmake, as there are about 5 other libs I needed to auto-link into the project on macOS/deb/mingw.
Thanks for the tip about the alpha status, as I did end up having to pass in a few tags to get it to statically link. Eventually I had to embed some parser calls inside the build script to mop up platform specific tags ( no joy finding out mac grep isn't gnu grep in xcode... :( ),

It ended up being two issues:

Code: Select all

 m_pOwner = pOwner;  *strOwner;
not sure why it wasn't a syntax error, but should have been:

Code: Select all

 m_pOwner = pOwner;  str_pOwner=strOwner;
Also this oddity kept me looking in the wrong spot:

Code: Select all

	wxString image_choices[] = {""};	//empty {} braces would seg fault only in xcode9.3/clang
    	list_box = new wxListBox(... //when used here
Now I need to learn the secret knock or something to get registered on the trac site.
The admin must think I'm hallucinating a broken reCAPTCHA js lib is preventing new registrations. :D

Thanks again guys, and I will try to get up to speed on how things are done here.
J
Attachments
nope.png
nope.png (33.41 KiB) Viewed 1142 times
Post Reply