A
A
AlxMrz2018-07-01 09:33:52
C++ / C#
AlxMrz, 2018-07-01 09:33:52

How to fix the Segmentation fault; core dumped when dealing with C++ pointers?

There are 2 classes:
1) UI

//UI.h
#ifndef UI_H
#define UI_H
#include <map>
#include <string>
#include <FL/Fl_Button.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Output.H>
#include <FL/Fl_Widget.H>
using namespace std;

class App;
class Calculator;

class UI {
public:
    Fl_Window *flWindow;
    map< string, Fl_Box* > flBox;
    map< string, Fl_Button*> flButtons;

    Fl_Output *output;
    App *app;
    Calculator *calc;
    
    UI(App* app);
    void startWindow();
    void endWindow();
    void createUI();

    static void resetOutputCb(Fl_Widget *w, void *data);

    void changeOutputValue();
    void prepareOutput(string& insertedValue, bool isNewAction);
};
#endif /* UI_H */

2) Calculator:
//Calculator.h
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include <string>
#include <FL/Fl_Widget.H>
using namespace std;

class UI;

class Calculator {
public:
    Calculator(UI *ui);
    UI *ui;
    
    string leftOperand;
    string action;
    string rightOperand;
    
    static void clickButtonCb(Fl_Widget *w, void *data);
    bool isNewAction(string action);
private:
    void makeCalc(bool isNewValue);
    double plus(double x, double y);
};

#endif /* CALCULATOR_H */

The UI class initializes the Calculator class and passes it a pointer to itself:
UI::UI(App *app) {
    this->app = app;
    this->calc = new Calculator(this);
}

The Calculator class, in turn, writes a pointer to the UI in one of its properties:
Calculator::Calculator(UI *ui) :
leftOperand("0"),
rightOperand(""),
action("") {
    this->ui = ui;
}
}

However, now when trying to call a UI class method from the Calculator:
void UI::prepareOutput(string& insertedValue, bool isNewAction) {
    if (calc->action != "" && !isNewAction) { // странно, что при calc->action != "" ошибки нет
        if (calc->rightOperand == "0") {
            calc->rightOperand = "";
        }
        calc->rightOperand += insertedValue; // НО ВОТ ТУТ Возникает ошибка при работе с calc->rightOperand
    } else if (!isNewAction) {
        if (calc->leftOperand == "0") {
            calc->leftOperand = "";
        }
        calc->leftOperand += insertedValue;
    }
    if (isNewAction && calc->rightOperand == "" && calc->action != "" && insertedValue != "=") {
        calc->action = insertedValue;
    }
}

an error occurs: Segmentation fault; core-dumped;
The same error occurs if you do something like this in the Calculator class:
string str = this->ui->calc->leftOperand; // Segmentation fault; core dumped

When debugging, the variables show "strange" values ​​for me, because simple lines are expected:
5b3874af7b5f8755241208.png
Why does this error occur and how to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
1
15432, 2018-07-01
@15432

I will assume that you have something like
UI myUi = UI();
as a result, it turns out that a temporary object is created, pushes itself into the calculator, then this object is copied into the final variable (element by element, since there is no copy constructor)
... and the temporary object is deleted, while the pointer to the temporary object remains in the calculator, and not to real. That's why there are different pointers in this and calc->ui . Then there is a call to the already freed memory, and the familiar SegFault (it only occurs by writing, it is allowed to read the freed memory)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question