Bugfix: KTorrent crashes on exit due to access to invalid memory block

Review Request #130036 - Created March 19, 2017 and updated

Information
Alexander Trufanov
ktorrent
master
Reviewers
ktorrent

I've noticed KTorrent crashing every time it close. I rarely close such apps but during development it happens often. And I realized that my previously submitted review cause debugger to bark on invalid memory access when KTorrent exits at particular line.

It happenes when I call item->tc->getTrackersList(); in ViewModel::data(). And it looks like this is because tc points to object that was already deleted in Core::onExit(). I've rolled back my changes and found out that other code in data() seems not to worry debugger and looks like that's because it acess only simple object's properties like getDisplayName() that mey be inlined... don't know. Still original app is crashing, but somewhere in delegate classes.

I've dubugged this and think the problem is following:
ViewModel's items contain pointers TorrentInterface* which were coped from QueueManager's pointers list.
When I close the app I'm invoking Core::onExit() which calls QueueManager::clear() and there all TorrentInterface* are deleted but their memory is not nulled. And no one informs ViewModel that its items now contain invalid pointers. And in case your ViewModel lives some time longer than core you're accessing dead objects throw these links.

In my case I have > 200 torrents and it takes 30 sec for KTorrent to save all chunks and stats at exit. All that time GUI is on screen. Probably this allows me to face with this problem in 100% cases.

It's not easy to communicate destruction of torrent pointers to data model without making serious changes to architecture. So I've decided to do the same trick core do - to subscribe QCoreApplication::aboutToQuit() signal and set up an exiting flag to do nothing on exit. And now everything works like a charm with my code (highlights on review) and with original code.


  

Issues

  • 0
  • 3
  • 0
  • 3
Description From Last Updated
Anthony Fieroni
Alexander Trufanov
Review request changed
Loading...