Strange bug - I cannot find the cause


#1

I’m programming a little BPM Detector which scans files. So the user can select some files via a FileChooser and then a new Thread is created that scans the files.
The problem arises when this Thread gets destroyed (for instance when the user presses Cancel).

Here’s the code (only the important bits):

[code]
// ***** BPM DETECTION THREAD *****

class BPMDetectionTask:public Thread
{
private:
OwnedArray files;
public:
BPMDetectionTask(const OwnedArray &files_) : Thread(T(“Thread”))
{

	for (int i=0; i<files_.size(); i++)
	{
		const File *f=files_[i];
		files.add(new File(*f));
	}	
}

...

}

//***** GUI THREAD *****

private:
BPMDetectionTask *task;

public:

void doSomething()
{
FileChooser *myChooser = new (…)
const OwnedArray &files=myChooser->getResults();
task=new BPMDetectionTask(files);
task->startThread();
}

void cancelTask()
{
//WHEN CANCEL BUTTON IS PRESSED:
if (task!=0)
{
task->stopThread(-1);
deleteAndZero(task);
}
}[/code]

And here’s what happens when the scan Thread gets deleted:

First-chance exception at 0x004d215e in start.exe: 0xC0000005: Access violation writing location 0xfeeefeee.

Call Stack:

start.exe!juce::atomicDecrementAndReturn() + 0xe
start.exe!juce::String::~String() + 0x14
start.exe!juce::File::~File() Line 85 + 0x2b
start.exe!juce::File::`scalar deleting destructor’() + 0x2b
start.exe!juce::OwnedArrayjuce::File,juce::DummyCriticalSection::clear(const bool deleteObjects=true) Line 99 + 0x45
start.exe!juce::OwnedArrayjuce::File,juce::DummyCriticalSection::~OwnedArrayjuce::File,juce::DummyCriticalSection() Line 88
start.exe!BPMDetectionTask::~BPMDetectionTask() + 0x4a

Disassembly:

?atomicDecrementAndReturn@juce@@YIHAAH@Z:
004D2150 sub esp,8
004D2153 mov dword ptr [esp],ecx
004D2156 mov ecx,dword ptr [esp]
004D2159 mov eax,0FFFFFFFFh
004D215E lock xadd dword ptr [ecx],eax <------------------------
004D2162 dec eax
004D2163 mov dword ptr [esp+4],eax
004D2167 mov eax,dword ptr [esp+4]
004D216B add esp,8

Comments:

I thought this might have something to do with no CriticalSection being passed to some OwnedArray, but there’s no concurrent use of the OwnedArray… I tried to add the CriticalSection, but it doesn’t change anything.

I also applied the String::empty reference count bug patch to my String::~String() . Doesn’t help.


#2

Cant see anything obvious, but just looks like a straightforward dangling pointer. If it was me, I’d put a breakpoint in the destructor to see if you can catch it deleting it twice, and also to see what state the object is in at that point, in case the object’s memory has been corrupted by something else.