Author Topic: Unexpected crash when removing file from the project  (Read 5024 times)

Offline comsytec

  • Multiple posting newcomer
  • *
  • Posts: 58
    • www.comsytec.com
Unexpected crash when removing file from the project
« on: May 15, 2014, 09:48:38 am »
Please review this (from trunk):

Code
bool cbProject::RemoveFile(ProjectFile* pf)
{
if (!pf)
return false;
m_ProjectFilesMap.erase(UnixFilename(pf->relativeFilename)); // remove from hashmap
Manager::Get()->GetEditorManager()->Close(pf->file.GetFullPath());
{
FilesList::iterator it = m_Files.find(pf);
if (it == m_Files.end())
Manager::Get()->GetLogManager()->DebugLog(_T("Can't locate node for ProjectFile* !"));
else
m_Files.erase(it);
if ( m_FileArray.GetCount() > 0 )
// m_FileArray.Remove(*it); // -------------------- wrong !! --------------------
                            // -------------------- it cannot be accessed -------
m_FileArray.Remove(pf);     // -------------------- correct !? -------------------
}

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Unexpected crash when removing file from the project
« Reply #1 on: May 15, 2014, 02:39:46 pm »
Yes.

For a moment you had me scared. That looked almost like the fix I did 3 days ago (this). Running cppcheck over our code brought that one up. Now your post made me wonder if I broke more than I fixed  ;D

This is the same issue, iterator being erased and thus invalidated, and subsequently dereferenced when removed from some list/array/map. Interesting that cppcheck didn't complain about this one...
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Unexpected crash when removing file from the project
« Reply #2 on: May 15, 2014, 02:59:15 pm »
I don't have time now to test, but this should be the proper implementation:

Code
	{
FilesList::iterator it = m_Files.find(pf);

if (it == m_Files.end())
{
Manager::Get()->GetLogManager()->DebugLog(_T("Can't locate node for ProjectFile* !"));
}
else
{
if (!m_FileArray.IsEmpty())
m_FileArray.Remove(*it);

m_Files.erase(it);
}
}

I am somewhat undecided about that checking for emptiness of the array. Actually it is kind of useless code since you won't ever remove a file from a project that isn't part of the project (at least, you shouldn't ever need to?). Unless someone writes a plugin that removes random filenames from a project or something the like, that should never happen.
On the other hand, wxArray does not silently ignore removing an element that is not in the array as you would expect, but triggers an assertion, so if that happens... bang, you're dead. That's somewhat uncool.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline comsytec

  • Multiple posting newcomer
  • *
  • Posts: 58
    • www.comsytec.com
Re: Unexpected crash when removing file from the project
« Reply #3 on: May 15, 2014, 06:54:23 pm »
If find() gives != end() then *it == pf should be true or I'm thinking bad. Of course inside else block but if already fixed then ok.
« Last Edit: May 15, 2014, 06:58:15 pm by comsytec »

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2775
Re: Unexpected crash when removing file from the project
« Reply #4 on: February 06, 2015, 11:49:37 pm »
I've test Thomas' code and solve a crash I've been having when an automated system deleted a file previously added to the project.

I'd like to commit it. Any objections?


Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Unexpected crash when removing file from the project
« Reply #5 on: February 07, 2015, 12:24:39 am »
Nope...
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Unexpected crash when removing file from the project
« Reply #6 on: February 07, 2015, 02:13:14 pm »
Crash fixes are always welcome, of course....
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ