if (my_pointer==nullptr) causes segmentation fault
-
Hi
Basic question but drives me mad for the whole day.I have a class inicio_common
For this class I declared a pointer and a method in
inicio_common.h :
private: parameter *param=nullptr; public: QString parameter_read(QString param_name, bool force_read = false);
I define the function in
inicio_common.cppQString inicio_common::parameter_read(QString param_name, bool force_read) { qDebug()<<"param read enter"; qDebug()<<"param name="+param_name; if (this->param==nullptr) { qDebug()<<"param obj is NULL"; this->param = new parameter(receiver_app); } else { qDebug()<<"param obj ok"; } qDebug()<<"param obj checked"; QString val=this->param->read(param_name,force_read); qDebug()<<"param val="<<val; return (val); }
This function runs fine several times and suddenly on one iteration it throws a segmantation fault on
if (this->param==nullptr)
and never reaches anything after that.Any hints for a scenario ?
-
Hi
Basic question but drives me mad for the whole day.I have a class inicio_common
For this class I declared a pointer and a method in
inicio_common.h :
private: parameter *param=nullptr; public: QString parameter_read(QString param_name, bool force_read = false);
I define the function in
inicio_common.cppQString inicio_common::parameter_read(QString param_name, bool force_read) { qDebug()<<"param read enter"; qDebug()<<"param name="+param_name; if (this->param==nullptr) { qDebug()<<"param obj is NULL"; this->param = new parameter(receiver_app); } else { qDebug()<<"param obj ok"; } qDebug()<<"param obj checked"; QString val=this->param->read(param_name,force_read); qDebug()<<"param val="<<val; return (val); }
This function runs fine several times and suddenly on one iteration it throws a segmantation fault on
if (this->param==nullptr)
and never reaches anything after that.Any hints for a scenario ?
@Seb-Tur said in if (my_pointer==nullptr) causes segmentation fault:
Any hints for a scenario ?
Use a debugger, see what's going wrong. I would guess 'this' is either a nullptr or garbage.
-
with if(param==nullptr) I get the same segfault
I added this-> just in case after being clueless for hours
@Seb-Tur said in if (my_pointer==nullptr) causes segmentation fault:
with if(param==nullptr) I get the same segfault
Because it's exactly the same...
-
with if(param==nullptr) I get the same segfault
I added this-> just in case after being clueless for hours
-
This will happen when you call a method on a deleted object.
this
points to garbage and any access to its members results in seg fault. Make sure your object is still alive when you call this function. -
This will happen when you call a method on a deleted object.
this
points to garbage and any access to its members results in seg fault. Make sure your object is still alive when you call this function.I don't understand
I'm calling this check in a method belonging to inicio_common class and param also belongs there.
so when I create an objectinicio_common *common_object;
so then it gets its member from .h
common_object->param =nullptrso
method common_object->parameter_read()
reffers to common_object->paramParam is a sibling member , so how come it's parent "this" can be garbage? If this was garbage than its method read_paramater wouldnt't be called either?
I don't delete param in any method in inicio_common class. It is a private member. -
I don't understand
I'm calling this check in a method belonging to inicio_common class and param also belongs there.
so when I create an objectinicio_common *common_object;
so then it gets its member from .h
common_object->param =nullptrso
method common_object->parameter_read()
reffers to common_object->paramParam is a sibling member , so how come it's parent "this" can be garbage? If this was garbage than its method read_paramater wouldnt't be called either?
I don't delete param in any method in inicio_common class. It is a private member.@Seb-Tur said in if (my_pointer==nullptr) causes segmentation fault:
If this was garbage than its method read_paramater wouldnt't be called either?
Why not? Who would prevent it?
-
@Seb-Tur said in if (my_pointer==nullptr) causes segmentation fault:
inicio_common *common_object;
how did you create this one: inicio_common *common_object{}; ? is it nullptr?
-
declared in mainwindow.h as
inicio_common = *common_object =nullptrthan contructed at the begining of mainwindow.cpp
common_object = new inicio_common();
-
@Seb-Tur It's not the
param
member that is deleted. It's the instance of class inicio_common.One case where this would happen:
inicio_common *common_object; // common_object is an uninitialized variable common_object->parameter_read(...); // calling method with garbage "this" -> crash
another way the same happens:
inicio_common *common_object = new inicio_common(); // common_object is initialized, ok common_object->parameter_read(...); // works delete common_object; // common_object now points to garbage common_object->parameter_read(...); // calling method with garbage "this" -> crash
If this was garbage than its method read_paramater wouldnt't be called either?
There's nothing in C++ language preventing that. You can create pointers pointing to random memory and call methods on them. For example:
QString* foo = (QString*)12345; // because why not foo->clear(); // garbage "this" -> crash
-
Thanks all for the support, your hints directed me to the right path to find an error I had no idea was an error.
Here's the scenario :
I used common_object in mainwindow doing various activities and all was fine;
at some point I was passing the common_object pointer in a constructor to another object of action_class that needed some properties of inicio_common object.,
action_class.h contained
inicio_common *ref_common = nullptr;
but action class cunstructor was capturing the common_object
action_class::action_class(inicio_common *common) { ref_common = common; }
so in the mainwindow I had common_object used by mainwindow and action_class_object because I run:
action_class *action_class_object = new (common_object);
After I was done with the actions I deleted action_class_object with
delete(action_class_object);
and this seems to have deleted the common_object by deleting ref_common in cascade .... I guess
Does it make sense or is it just my wicked thinking?Anyway I still don't get why if(param==nullptr) would crash instead of returning true;
-
Thanks all for the support, your hints directed me to the right path to find an error I had no idea was an error.
Here's the scenario :
I used common_object in mainwindow doing various activities and all was fine;
at some point I was passing the common_object pointer in a constructor to another object of action_class that needed some properties of inicio_common object.,
action_class.h contained
inicio_common *ref_common = nullptr;
but action class cunstructor was capturing the common_object
action_class::action_class(inicio_common *common) { ref_common = common; }
so in the mainwindow I had common_object used by mainwindow and action_class_object because I run:
action_class *action_class_object = new (common_object);
After I was done with the actions I deleted action_class_object with
delete(action_class_object);
and this seems to have deleted the common_object by deleting ref_common in cascade .... I guess
Does it make sense or is it just my wicked thinking?Anyway I still don't get why if(param==nullptr) would crash instead of returning true;
@Seb-Tur said:
and this seems to have deleted the common_object by deleting ref_common in cascade .... I guess
delete something
does not delete "something". It deletes the thing that "something" points to. "something" points to garbage after that. If you copy a pointer you copy a pointer. You're not copying the object it points to, so when you delete the object via one pointer both the original and the copied pointer point to the same garbage memory.Anyway I still don't get why if(param==nullptr) would crash instead of returning true;
It's not the
if
that is crashing. It can't return anything because it's not even executed. It doesn't get to the comparison operator.this
points to garbage, sothis->param
is trying to read a value from garbage memory. Reading from garbage memory is access violation and terminates your app. -
@Seb-Tur said:
and this seems to have deleted the common_object by deleting ref_common in cascade .... I guess
delete something
does not delete "something". It deletes the thing that "something" points to. "something" points to garbage after that. If you copy a pointer you copy a pointer. You're not copying the object it points to, so when you delete the object via one pointer both the original and the copied pointer point to the same garbage memory.Anyway I still don't get why if(param==nullptr) would crash instead of returning true;
It's not the
if
that is crashing. It can't return anything because it's not even executed. It doesn't get to the comparison operator.this
points to garbage, sothis->param
is trying to read a value from garbage memory. Reading from garbage memory is access violation and terminates your app.@Chris-Kawa said in if (my_pointer==nullptr) causes segmentation fault:
this points to garbage, so this->param is trying to read a value from garbage memory. Reading from garbage memory is access violation and terminates your app.
Not that I said it in my first post... :)