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. Program crashes after deleting the pointer
Forum Updated to NodeBB v4.3 + New Features

Program crashes after deleting the pointer

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 514 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.
  • K Offline
    K Offline
    Kira
    wrote on 9 Apr 2019, 06:11 last edited by
    #1
          QList<QPair<char,int>> inferenceResult;
          QPair<char,int> *tempInferenceResult;
          qDebug()<<"Inside connectivity function";
          inferenceResult = //Output from some function
        
        qDebug()<<"Inference result in connectivity"<<inferenceResult;
            if(i==0){
                tempInferenceResult = &inferenceResult[0];
                tempInferenceResult->second = 0;
    
                tempInferenceResult = &inferenceResult[2];
                tempInferenceResult->second = 0;
            }
            qDebug()<<"Temp Inference result"<<*tempInferenceResult;
        **delete tempInferenceResult;**
        ----------
    ------line  of codes------
    -----------------------
       
    

    In the above code i am trying to modify the value of list of Qpair using pointer of type QPair;
    My problem is that when i delete the pointer my program crashes;
    There are also other approaches like inferenceResult[0].second = 0 instead of using pointer but i wanted to know the exact problem with the approach and how to resolve.
    Thanks

    J 1 Reply Last reply 9 Apr 2019, 06:27
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 9 Apr 2019, 06:27 last edited by
      #2

      Hi
      You take a reference to the inline structure of the list.

      QList<QPair<char,int>> inferenceResult;

      Its not pointer to pair, but a pair. Directly.

      then
      tempInferenceResult = &inferenceResult[0];

      Points to the pair In the list. (inside list)

      delete tempInferenceResult; // try to delete something INSIDE the list which
      only list can do in a safe manner.

      1 Reply Last reply
      3
      • K Kira
        9 Apr 2019, 06:11
              QList<QPair<char,int>> inferenceResult;
              QPair<char,int> *tempInferenceResult;
              qDebug()<<"Inside connectivity function";
              inferenceResult = //Output from some function
            
            qDebug()<<"Inference result in connectivity"<<inferenceResult;
                if(i==0){
                    tempInferenceResult = &inferenceResult[0];
                    tempInferenceResult->second = 0;
        
                    tempInferenceResult = &inferenceResult[2];
                    tempInferenceResult->second = 0;
                }
                qDebug()<<"Temp Inference result"<<*tempInferenceResult;
            **delete tempInferenceResult;**
            ----------
        ------line  of codes------
        -----------------------
           
        

        In the above code i am trying to modify the value of list of Qpair using pointer of type QPair;
        My problem is that when i delete the pointer my program crashes;
        There are also other approaches like inferenceResult[0].second = 0 instead of using pointer but i wanted to know the exact problem with the approach and how to resolve.
        Thanks

        J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 9 Apr 2019, 06:27 last edited by
        #3

        @Kira
        well obviously, tempInferenceResult is not it's own object instance but it references part of the memory of one of your list items.

        By deleting it you invalidate part of the allocated memory of that list item.,


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        3
        • K Offline
          K Offline
          Kira
          wrote on 9 Apr 2019, 11:29 last edited by
          #4

          Thanks for the explanation

          1 Reply Last reply
          0

          1/4

          9 Apr 2019, 06:11

          • Login

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