Access violation at 0xfeeefeee
Hello,
I am having trouble with an access violation writing location 0xfeeefeee when I close my program.
I realize that I am having a common problem from my searches on Google, but I couldn't find anything that quite applies to my specific issue.
I am running this code in a separate thread from the main program thread, called by AfxBeginThread().
Code:
UINT CreateFilters(LPVOID v_Info) {
CreateFiltersInfo* createInfo=(CreateFiltersInfo*)v_Info;
HWND statusBar=createInfo->statusBar;
float width=createInfo->width;
int* percentDone=createInfo->percentDone;
int number_of_filters=MAX_BPM-MIN_BPM+1;
BPMFilters=new BPMFilter[number_of_filters];
for (int i=0; i<number_of_filters; ++i) {
if (!BPMFilters[i].set(i+MIN_BPM, width, 5*sampleRate)) {
return 0;
}
*(percentDone)=i;
}
return 1;
}
BPMFilter::set() reads as follows.
Code:
int BPMFilter::set(int sb, float sw, unsigned int sl) {
BPM=sb;
w=sw*sampleRate;
length=sl;
float B=TWOPI*(float)BPM/BASE_BPM_SAMPLES; // 2PI/B = Period
float temp=cos(B*w/2.f);
float A=temp/(1.f-temp); // Amplitude
float C=A++; // Vertical shift. (Note: C is set to A's original value, THEN A is incremented by 1)
if (a) delete [] a;
a=new float[length];
for (unsigned int i=0; i<length; i++) {
//these a[i]=abs(A*cos(B*(float)i));
//lines a[i]=(a[i]-C<0.f)?0.f:a[i]-C;
}
return 1;
}
The error occurs at the lines marked with comments. My issue is that even though my destructor deletes a[] and sets it to NULL and I tried a NULL check, the array seems to be set to 0xfeeefeee by HeapFree() (maybe?) when the program closes, causing the program to crash.
So, I am wondering what the best solution would be. Doing an IsBadReadPtr() check? Closing the thread from within itself when the program closes? Any help would be appreciated, thank you in advance.
Re: Access violation at 0xfeeefeee
Since you are sharing memory between threads, you'll need to synchronize access to it using a critical section or a mutex.
Re: Access violation at 0xfeeefeee
Any hints as to how I would use this to signal the thread to stop when the program closes?
Re: Access violation at 0xfeeefeee
it is being deleted somewhere and NOT set to NULL.
(or being deleted by one thread, and then accessed by another before its value is set to NULL)
Re: Access violation at 0xfeeefeee
Quote:
Originally Posted by
Amleto
it is being deleted somewhere and NOT set to NULL.
(or being deleted by one thread, and then accessed by another before its value is set to NULL)
I went through and made sure that every time I delete it, it is set to NULL (or a valid value if it's about to be used, like in set()), so the second case must be what is going on. Unfortunately, I have little idea how to fix it.
Re: Access violation at 0xfeeefeee
If your problem happens every time then it is unlikely that is a threading issue.
To fix this you could use a mutex/critical section in every function that accesses 'a'. That way no thread can be reading a whilst another is writing it.
Re: Access violation at 0xfeeefeee
Thank you very much, I successfully implemented CCriticalSection into my code.
Re: Access violation at 0xfeeefeee
Quote:
Originally Posted by
lawl_rock
Thank you very much, I successfully implemented CCriticalSection into my code.
Code:
if (a) delete [] a;
Not necessary to test for NULL. It is perfectly legal to delete a NULL pointer.
Regards,
Paul McKenzie