Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to check pointer object isExist before?
Forum Updated to NodeBB v4.3 + New Features

How to check pointer object isExist before?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 5 Posters 2.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M MimCimm

    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..

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #2

    @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 to nullptr, and if you delete it reset it to nullptr so that you can test that in the future.

    M 1 Reply Last reply
    2
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #3

      A pointer is just an integer - if you make sure to set it to nullptr when it's not created, you can simply check with if:

      if (myClassObject) {
        delete myClassObject;
        myClassObject = nullptr;
      }
      

      Alternatively, you can use QPointer and it's isNull() method - if your MyClass is a subclass of QObject.

      (Z(:^

      M 1 Reply Last reply
      2
      • JonBJ JonB

        @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 to nullptr, and if you delete it reset it to nullptr so that you can test that in the future.

        M Offline
        M Offline
        MimCimm
        wrote on last edited by
        #4

        @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.

        sierdzioS JonBJ 2 Replies Last reply
        0
        • jeremy_kJ Offline
          jeremy_kJ Offline
          jeremy_k
          wrote on last edited by
          #5

          Deleting a null pointer is a safe operation. There's no need to check for it.

          Asking a question about code? http://eel.is/iso-c++/testcase/

          1 Reply Last reply
          2
          • M MimCimm

            @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.

            sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #6

            @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.

            (Z(:^

            M 1 Reply Last reply
            1
            • M MimCimm

              @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.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #7

              @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 to nullptr any time you delete it (unless you immediately set it to something else). I still don't understand what your issue is.

              1 Reply Last reply
              0
              • sierdzioS sierdzio

                A pointer is just an integer - if you make sure to set it to nullptr when it's not created, you can simply check with if:

                if (myClassObject) {
                  delete myClassObject;
                  myClassObject = nullptr;
                }
                

                Alternatively, you can use QPointer and it's isNull() method - if your MyClass is a subclass of QObject.

                M Offline
                M Offline
                MimCimm
                wrote on last edited by
                #8

                @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.

                sierdzioS 1 Reply Last reply
                0
                • B Offline
                  B Offline
                  Bonnie
                  wrote on last edited by
                  #9

                  ---x.h---
                  MyClass *myClassObject;

                  Is that a global pointer or a class member?
                  If it is a global pointer, it shouldn't be put in a header file...Otherwise you'll get problems when you include this header file in multiple other files...

                  1 Reply Last reply
                  0
                  • sierdzioS sierdzio

                    @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.

                    M Offline
                    M Offline
                    MimCimm
                    wrote on last edited by MimCimm
                    #10
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • M MimCimm

                      @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.

                      sierdzioS Offline
                      sierdzioS Offline
                      sierdzio
                      Moderators
                      wrote on last edited by
                      #11

                      @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;
                      

                      (Z(:^

                      1 Reply Last reply
                      1

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved