Author Topic: Migrating Pointer to std::shared_ptr in sdk  (Read 2966 times)

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Migrating Pointer to std::shared_ptr in sdk
« on: May 12, 2022, 10:14:30 am »
Hi,
i would like to implement https://sourceforge.net/p/codeblocks/tickets/1255/ but i got a crash at closing the application, because of an invalid pointer.
The Problem is that a ProjectFile* is deleted but shared between different objects, and not cleared the right way. Even a comment in the code shows hints to the problem:
cbproject.cpp:1210
Code
// FIXME This call is rather strange. It advertises that it will close the files, but in fact it
// removes all files from the project, deallocates all ProjectFile instances and returns. This
// leaves the ProjectBuildTarget ProjectFile instances to be dangling. I don't undestand!!!
bool cbProject::CloseAllFiles(bool dontsave)
In addition to the ProjectBuildTarget  object, also the FileTreeData has a dangling ProjectFile*
This is probably a source of some crashes and bugs.

I think the "easiest" (but a LOT work) solution to fix this would be to use std::shared_ptr and std::weak_ptr.... But this would break a lot code....
One idea is to use internally shared pointer, and introduce some parallel api that returns the shared_ptr but let the old api alone...

Any ideas, suggestions?