CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2010
    Posts
    27

    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.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.

  3. #3
    Join Date
    Dec 2010
    Posts
    27

    Re: Access violation at 0xfeeefeee

    Any hints as to how I would use this to signal the thread to stop when the program closes?

  4. #4
    Join Date
    Apr 2008
    Posts
    725

    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)
    Last edited by Amleto; February 28th, 2011 at 01:44 PM.

  5. #5
    Join Date
    Dec 2010
    Posts
    27

    Re: Access violation at 0xfeeefeee

    Quote Originally Posted by Amleto View Post
    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.

  6. #6
    Join Date
    Apr 2008
    Posts
    725

    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.
    Last edited by Amleto; February 28th, 2011 at 02:15 PM.

  7. #7
    Join Date
    Dec 2010
    Posts
    27

    Re: Access violation at 0xfeeefeee

    Thank you very much, I successfully implemented CCriticalSection into my code.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Access violation at 0xfeeefeee

    Quote Originally Posted by lawl_rock View Post
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured