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. Clearing a QSharedPointer pointed by QSharedPointer and QWeakPointer.
QtWS25 Last Chance

Clearing a QSharedPointer pointed by QSharedPointer and QWeakPointer.

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 218 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.
  • prinzkmP Offline
    prinzkmP Offline
    prinzkm
    wrote on last edited by
    #1

    In the following code,

    int *pI = new int(9);//memory allocated
    QSharedPointer<int> pI1(pI);//shared pointer pointing to pI
    QWeakPointer<int> pI2 = pI1;//weak pointer pointing to pI
    pI1.clear();
    
    qDebug()<<*pI; //prints 9 - no segmentation fault
    
    qDebug()<<pI2.isNull(); // evaluates to true
    qDebug()<<pI1.isNull(); //evaluates to true
    

    why I am able to dereference *pI.Won't shared pointer delete it from heap after pI1.clear()?

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

      It may be deleted, but the data is still there. You're accessing "dead" memory.

      (Z(:^

      1 Reply Last reply
      4

      • Login

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