Page MenuHomePhabricator

Ensure correct pattern of mitk::WeakPointer::Lock()
Closed, ResolvedPublic

Description

There are several places in the code that have the following pattern:

if(!weakPointer.IsExpired())
{
  smartPointer = weakPointer.Lock();
 //do something with the smartPointer
}

This is not safe. As you have no guarantee that the weakPointer is valid in the next line. If you plan to use the weakPointer and want to be sure that you can use it, you have to do the following:

smartPointer = weakPointer.Lock();
if(smartPointer.IsNotNull())
{
  //do your stuff
}

In addition we should extend the documentation of mitk::WeakPointer::IsExpired() to stress the correct behavior. You should IsExpired() if you realy just want to know if the pointer is gone or not.

Revisions and Commits

rMITK MITK
Restricted Differential Revision
Restricted Differential Revision
Restricted Differential Revision

Related Objects

Event Timeline

floca triaged this task as High priority.Feb 15 2020, 9:51 AM
floca created this task.
kislinsk added a project: Auto-closed.
kislinsk added a subscriber: kislinsk.

Hi there! ๐Ÿ™‚

This task was auto-closed according to our Task Lifecycle Management.
Please follow this link for more information and don't forget that you are encouraged to reasonable re-open tasks to revive them. ๐Ÿš‘

Best wishes,
The MITK devs

floca edited projects, added Next Milestone; removed Auto-closed.
kislinsk added a revision: Restricted Differential Revision.May 3 2022, 5:55 PM

Deleted branch from rMITK MITK: bugfix/T27136-FixWeakPointerUsage.