Skip to content

Fix double-free bug when viewing certain timeline pages

Joshua Goins requested to merge (removed):fix-double-free into master

So I hit this bug when submitting !76 (merged) but I realized it was also replicated on master. For some reason, when viewing certain timeline pages, Tokodon will crash on exit. Running it through a debugger reveals that Posts are being delete'd twice.

I dug into why this could be happening, and I believe I understand what was going on here:

  1. AccountManager::~AccountManager is called, and starts deleting all of it's accounts one by one.
  2. Each account calls Account::~Account on destruction, clearing it's identity cache (not sure if that is related, but worth mentioning). Qt also starts deleting all of it's children objects (the Posts)
  3. At some point, something else attempts to delete those same posts.
  4. The double-free is encountered and Tokodon goes out with a bang!

However when attempting to figure out the root cause of the bug, I ended up fixing it by making it clearer who the owner of a Post was. Originally, each Post was owned by the Account, which I think is the crux of the issue. To be honest, I'm not sure if it makes much sense for them to be tied to Accounts at all, since models handle most of their function.

Now the QObject that owns the Post can be explicit (and separate from the account), and is set to what I believe to be the "sensible" option but I'm not familiar with Tokodon's codebase yet. For example, TimelineModel owns all of it's Posts, ThreadModel owns theirs, and so on. For some reason, AbstractAccount is making it's own posts but I'm not sure for what purpose, it owns it's Posts right now as well. Feel free to tear it apart! :-)

Merge request reports