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. Model crashed after static_cast
QtWS25 Last Chance

Model crashed after static_cast

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 2.2k Views
  • 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.
  • Ivan NetskinI Offline
    Ivan NetskinI Offline
    Ivan Netskin
    wrote on last edited by Ivan Netskin
    #1

    Hello, let me represent the problem:

    1. I'm populating my model with Item object
    class Item{
    ..........
    void installData(Data* ptr);
    Data *data();
    ..........
    }
    

    Now suppose this:
    1)AdditionalData class is inherited from Data
    2)Now in the installData function i've put the initialized AdditionalData pointer;

    And now getting the problem:

    data(const QModelIndex& index, int role)const{
    Item* item=reinterpret_cast<Item*>(index.internalPointer())
    Data* itemData=item.data();
    AdditionalData* addtionalData=static_cast<AdditionalData*>(itemData);
    //IF I CALL ANY METHOD FROM addtionalData THE PROGRAM WILL CRASH otherwise everything is fine
    }
    

    Any suggestions? Thanks in advance;

    jsulmJ 1 Reply Last reply
    0
    • Ivan NetskinI Ivan Netskin

      Hello, let me represent the problem:

      1. I'm populating my model with Item object
      class Item{
      ..........
      void installData(Data* ptr);
      Data *data();
      ..........
      }
      

      Now suppose this:
      1)AdditionalData class is inherited from Data
      2)Now in the installData function i've put the initialized AdditionalData pointer;

      And now getting the problem:

      data(const QModelIndex& index, int role)const{
      Item* item=reinterpret_cast<Item*>(index.internalPointer())
      Data* itemData=item.data();
      AdditionalData* addtionalData=static_cast<AdditionalData*>(itemData);
      //IF I CALL ANY METHOD FROM addtionalData THE PROGRAM WILL CRASH otherwise everything is fine
      }
      

      Any suggestions? Thanks in advance;

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Ivan-Netskin Well, you should ALWAYS check the pointer after casting. And it should be reinterpret_cast, not static_cast.
      If itemData is Data and not AdditionalData the cast will fail and you will get a null pointer.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      Ivan NetskinI kshegunovK 2 Replies Last reply
      1
      • jsulmJ jsulm

        @Ivan-Netskin Well, you should ALWAYS check the pointer after casting. And it should be reinterpret_cast, not static_cast.
        If itemData is Data and not AdditionalData the cast will fail and you will get a null pointer.

        Ivan NetskinI Offline
        Ivan NetskinI Offline
        Ivan Netskin
        wrote on last edited by Ivan Netskin
        #3

        @jsulm
        yes, i know, i should. I just wrote it for simplicity. In my code i have just 1 item with initialized AdditionalData so it's for sure Item contains pointer to the AdditionalData object

        jsulmJ 1 Reply Last reply
        0
        • Ivan NetskinI Ivan Netskin

          @jsulm
          yes, i know, i should. I just wrote it for simplicity. In my code i have just 1 item with initialized AdditionalData so it's for sure Item contains pointer to the AdditionalData object

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Ivan-Netskin Why do you use static_cast?
          Did you check whether addtionalData is nullptr?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • Ivan NetskinI Offline
            Ivan NetskinI Offline
            Ivan Netskin
            wrote on last edited by
            #5

            yes i did and it is not, i can get some additional info from the pointer and after that the program will crash. Like i said, if u don't touch the pointer(not calling any of it's member functions) the program will not crash.

            FlotisableF 1 Reply Last reply
            0
            • Ivan NetskinI Ivan Netskin

              yes i did and it is not, i can get some additional info from the pointer and after that the program will crash. Like i said, if u don't touch the pointer(not calling any of it's member functions) the program will not crash.

              FlotisableF Offline
              FlotisableF Offline
              Flotisable
              wrote on last edited by
              #6

              @Ivan-Netskin
              I think you should check if additionalData is realy a AdditionalData*

              Ivan NetskinI 1 Reply Last reply
              2
              • FlotisableF Flotisable

                @Ivan-Netskin
                I think you should check if additionalData is realy a AdditionalData*

                Ivan NetskinI Offline
                Ivan NetskinI Offline
                Ivan Netskin
                wrote on last edited by
                #7

                Okay, the code was correct!
                the line caused the crash :

                qDebug()<<model->data(model->index(1,0),Qt::DisplayRole);
                

                because there is only 1 item like i said. Thanks guys anyway!

                1 Reply Last reply
                0
                • jsulmJ jsulm

                  @Ivan-Netskin Well, you should ALWAYS check the pointer after casting. And it should be reinterpret_cast, not static_cast.
                  If itemData is Data and not AdditionalData the cast will fail and you will get a null pointer.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by kshegunov
                  #8

                  @jsulm said in Model crashed after static_cast:

                  And it should be reinterpret_cast, not static_cast.

                  I think you made a typo here, it must be dynamic_cast.

                  @Ivan-Netskin said in Model crashed after static_cast:

                  Okay, the code was correct!

                  With those reinterpret_casts (here it's necessary, I know, but at least check for NULL) and static_casts you're on a slippery slope ... neither is safe casting, so you may at some point in the future spend some hours debugging them.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  2

                  • Login

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