Reputation: 745
I have a class called "StatusInformation", where I want to set the variable _status to true/false, but I am getting only "segmentation fault". I think, that the _status doesn't exist, becouse I call it from the other class. Anybody know how to prevent this error?
StatusInformation.cpp
void StatusInformation::SetClientConnectStatus(bool status)
{
_status = status;
}
StatusInformation.h
class StatusInformation
{
private:
bool _status = false;
public:
void SetClientConnectStatus(bool status);
};
CallerClass.cpp
_statusInformation = new StatusInformation();
_statusInformation->SetClientConnectStatus(true);
CallerClass.h
StatusInformation *_statusInformation;
Upvotes: 0
Views: 52
Reputation: 394
EDIT : That link to your code is just giving me code that is missing ArduinoProtocol.
For me, this compiles with 2 warnings about non static data-member initializers, and then runs fine. I really do not believe the segmentation fault can come from this code. The only thing that could cause that would be if
_statusInformation = new StatusInformation();
Failed and returned 0, making your pointer a NULL pointer. Causing:
_statusInformation->SetClientConnectStatus(true);
To be equivalent to :
NULL->SetClientConnectStatus(true);
But that could only happen if you chose to use a no-throw new. Which you much specify. So realistically, the only thing in that code that could make you segmentation fault, cannot happen. Worst case an std::bad_alloc will be thrown.
Upvotes: 1