How to check pointer object isExist before?
-
Hello Everyone!
Are there any way to control pointer object created before.
Specifically:---x.h---
MyClass *myClassObject;
---x.cpp---
void methodX(){ //Here is line that I want to check that myClasssObject is created or not? delete myClasssObject; myClasssObject = new MyClassObject(); ... }
Because I trigger methodX() with QTimer periyodically so that I need o know is it created before if created then I will detele old one.
I should do do that otherwise every trigger increase thread count in OS.Any Help ,I will be gratified..
-
Hello Everyone!
Are there any way to control pointer object created before.
Specifically:---x.h---
MyClass *myClassObject;
---x.cpp---
void methodX(){ //Here is line that I want to check that myClasssObject is created or not? delete myClasssObject; myClasssObject = new MyClassObject(); ... }
Because I trigger methodX() with QTimer periyodically so that I need o know is it created before if created then I will detele old one.
I should do do that otherwise every trigger increase thread count in OS.Any Help ,I will be gratified..
@MimCimm
I don't fully understand. You cannot check that a pointer is valid/points to something valid safely in C++. You can, however, initialize it yourself. So initialize it tonullptr
, and if youdelete
it reset it tonullptr
so that you can test that in the future. -
A pointer is just an integer - if you make sure to set it to
nullptr
when it's not created, you can simply check withif
:if (myClassObject) { delete myClassObject; myClassObject = nullptr; }
Alternatively, you can use QPointer and it's
isNull()
method - if your MyClass is a subclass of QObject. -
@MimCimm
I don't fully understand. You cannot check that a pointer is valid/points to something valid safely in C++. You can, however, initialize it yourself. So initialize it tonullptr
, and if youdelete
it reset it tonullptr
so that you can test that in the future. -
@JonB If I initialize with nullptr than I am losing old created object before delete it. But old thread still countinue to work when I check OS from linux terminal.
@MimCimm said in How to check pointer object isExist before?:
@JonB If I initialize with nullptr than I am losing old created object before delete it. But old thread still countinue to work when I check OS from linux terminal.
You definitely need to initialize with
nullptr
- in constructor or (IMO better) in the header:MyClass *myClassObject = nullptr;
Otherwise it is very, very easy to crash your app.
-
@JonB If I initialize with nullptr than I am losing old created object before delete it. But old thread still countinue to work when I check OS from linux terminal.
@MimCimm said in How to check pointer object isExist before?:
If I initialize with nullptr than I am losing old created object before delete it.
That's not "initializing", that's just "overwriting", of course I was not suggesting that. Just make sure it's
nullptr
before you start, and reset it tonullptr
any time youdelete
it (unless you immediately set it to something else). I still don't understand what your issue is. -
A pointer is just an integer - if you make sure to set it to
nullptr
when it's not created, you can simply check withif
:if (myClassObject) { delete myClassObject; myClassObject = nullptr; }
Alternatively, you can use QPointer and it's
isNull()
method - if your MyClass is a subclass of QObject.@sierdzio I tried with QPointer but also QPointer stops running if I try to set myObject into QPoiter object before new ;
QPointer<MyClassObject>qPointObject(myClassObject); //it throws error in this line qDebug()<< qPointObject.isNull(); myClasssObject = new MyClassObject();
I know It throws because I am trying to set before using new (that means create) but That is exactly thing ,I want to check.
If I control after new line I am losing the old thread. -
@MimCimm said in How to check pointer object isExist before?:
@JonB If I initialize with nullptr than I am losing old created object before delete it. But old thread still countinue to work when I check OS from linux terminal.
You definitely need to initialize with
nullptr
- in constructor or (IMO better) in the header:MyClass *myClassObject = nullptr;
Otherwise it is very, very easy to crash your app.
-
@sierdzio I tried with QPointer but also QPointer stops running if I try to set myObject into QPoiter object before new ;
QPointer<MyClassObject>qPointObject(myClassObject); //it throws error in this line qDebug()<< qPointObject.isNull(); myClasssObject = new MyClassObject();
I know It throws because I am trying to set before using new (that means create) but That is exactly thing ,I want to check.
If I control after new line I am losing the old thread.@MimCimm said in How to check pointer object isExist before?:
@sierdzio I tried with QPointer but also QPointer stops running if I try to set myObject into QPoiter object before new ;
QPointer<MyClassObject>qPointObject(myClassObject); //it throws error in this line qDebug()<< qPointObject.isNull(); myClasssObject = new MyClassObject();
I know It throws because I am trying to set before using new (that means create) but That is exactly thing ,I want to check.
If I control after new line I am losing the old thread.I meant to use it in your header:
QPointer<MyClass> myClassObject;