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 Offline
    M Offline
    MimCimm
    wrote on 31 Dec 2020, 07:55 last edited by
    #1

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

    J 1 Reply Last reply 31 Dec 2020, 08:13
    0
    • M MimCimm
      31 Dec 2020, 07:55

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

      J Offline
      J Offline
      JonB
      wrote on 31 Dec 2020, 08:13 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 31 Dec 2020, 08:19
      2
      • S Offline
        S Offline
        sierdzio
        Moderators
        wrote on 31 Dec 2020, 08:15 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 31 Dec 2020, 08:30
        2
        • J JonB
          31 Dec 2020, 08:13

          @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 31 Dec 2020, 08:19 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.

          S J 2 Replies Last reply 31 Dec 2020, 08:20
          0
          • J Offline
            J Offline
            jeremy_k
            wrote on 31 Dec 2020, 08:20 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
              31 Dec 2020, 08:19

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

              S Offline
              S Offline
              sierdzio
              Moderators
              wrote on 31 Dec 2020, 08:20 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 31 Dec 2020, 08:47
              1
              • M MimCimm
                31 Dec 2020, 08:19

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

                J Offline
                J Offline
                JonB
                wrote on 31 Dec 2020, 08:22 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
                • S sierdzio
                  31 Dec 2020, 08:15

                  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 31 Dec 2020, 08:30 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.

                  S 1 Reply Last reply 31 Dec 2020, 08:51
                  0
                  • B Offline
                    B Offline
                    Bonnie
                    wrote on 31 Dec 2020, 08:30 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
                    • S sierdzio
                      31 Dec 2020, 08:20

                      @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 31 Dec 2020, 08:47 last edited by MimCimm
                      #10
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • M MimCimm
                        31 Dec 2020, 08:30

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

                        S Offline
                        S Offline
                        sierdzio
                        Moderators
                        wrote on 31 Dec 2020, 08:51 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

                        1/11

                        31 Dec 2020, 07:55

                        • Login

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