wxClientDC instantiation gives a segmentation fault

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
In need of some credit
In need of some credit
Posts: 1
Joined: Mon Apr 08, 2013 7:07 pm

wxClientDC instantiation gives a segmentation fault

Post by vanillalife » Wed Apr 10, 2013 12:30 am

I couldn't find another topic similar to this one. I'm trying to draw a binary tree on to my program based on its corresponding array. All of my code is working up to the point where I try to call the constructor for the wxClientDC that I'm going to draw on. Here's the relevant code. All the necessary headers were included in the file, so I didn't include them here. If you want to see something else just let me know. As you can see the drawing method isn't entirely complete yet.

Code: Select all

void _43ProjectFrame::OnRgrButtonClick(wxCommandEvent& event)
    int n = 0;
    const long ASK_ID = wxNewId();
    AskSize* ask = new AskSize(this,ASK_ID);
    n = ask->getN();

    if (n == 0) return;
    currentObject = new Catalan(n);

void _43ProjectFrame::outputBinaryTree(){
    wxClientDC* io_area = new wxClientDC(Panel1);
    int* width;
    int* height;

    int n = currentObject->getN();
    int index = 0;
    if (width == NULL || height == NULL) return;
    int widthInc[n];
    int widthStart[n];
    int heightInc = *height/(n+1);
    int heightStart = heightInc/2;

    for (int i = 0; i < n; i++){
        widthInc[i] = *width/((int)pow(2,i));
        widthStart[i] = widthInc[i]/2;
    wxPoint root(widthStart[0],heightStart);
    int leftIndex = 2*index+1;
    int rightIndex = 2*index+1;

Site Admin
Site Admin
Posts: 3989
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

Re: wxClientDC instantiation gives a segmentation fault

Post by DavidHart » Wed Apr 10, 2013 6:32 am


What is the value of Panel1 when you examine it in your debugger at that point. Or (which probably amounts to the same thing) when you ask your debugger for a backtrace when it tells you of the segfault?

Most likely it's NULL, or some random value. The 2 commonest reasons for this are:
  • 1) 'Shadowing'. You declare wxPanel* Panel1; in the class definition; then in the constructor you do wxPanel* Panel1 = new wxPanel(...); so instantiating a local pointer with the same name. The class member remains invalid.
    2) Using 'Connect()' badly. If you arrive in an event-handler, in your case _43ProjectFrame::OnRgrButtonClick, with an invalid 'this', it's often because you used wxEvtHandler::Connect to catch the event, and didn't set the correct 5th parameter, eventSink.
Those are just a couple of possibilities/probabilities; assuming a debug build, your debugger will know more.



Part Of The Furniture
Part Of The Furniture
Posts: 2473
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxClientDC instantiation gives a segmentation fault

Post by PB » Wed Apr 10, 2013 7:42 am

I just skimmed the code, so I may be missing something, but I also don't see you initializing local variables width and height in _43ProjectFrame::outputBinaryTree(), so they point to random values (or maybe are set to NULL/arbitrary value in the debug build, depending on you compiler) prior to calling Panel1->GetSize(width,height).

You probably meant something like

Code: Select all

int width, height;

panel1->GetSize(&width, &height);
I also don't understand your comparing returned values of width and height with NULL and returning if either is true... There seems to be a memory leak too - wxClientDC instance is not deleted, I would create it on stack instead of heap. No idea if any of the above is crash related though.

Post Reply