2015-05-18 467 views
-3

我一直在調用free()函數時出現堆損壞錯誤。該項目在VC++ 2010年的工作,整個建設過程中工作正常,但在運行時出現錯誤:(CircularQueue是我的項目的名稱)免費堆損壞()

ERRORS:

Windows has triggered a breakpoint in CircularQueue.exe.

This may be due to a corruption of the heap, which indicates a bug in CircularQueue.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while CircularQueue.exe has focus.

The output window may have more diagnostic information.

#include "stdafx.h" 
#include<stdio.h> 
#include <string.h> 
#include <Windows.h> 


#include "CircularQ.h" 

#define max 4 
//char circQ[10][3145728]; 

Image_details_t circQ[max],*ptr[max]; 
Image_details_t *temp; 
LONG q[10],front=0,rear=-1; 
#if 1 
void main() 
{ 

    int ch; 
    //void insert();  
    insert("h",1,1,1,1); 
    insert("h",1,1,1,1); 
    insert("h",1,1,1,1); 
    delet(); 
    delet(); 
    delet(); 
    delet(); 
    while(1); 
} 

#endif 

void insert(char *img,int channel,int imgWidth,int imgHeight,int imgLen) 
{ 
    //int x; 
    //char x[20]; 
    int l = 0; 

    if((front==0&&rear==max-1)||(front>0&&rear==front-1)) 
     printf("Queue is overflow\n"); 
    else 
    {  
     l = strlen(img); 
     //scanf("%d",&x); 
     if(rear==max-1&&front>0) 
     { 
      printf("hello i m here"); 
      InterlockedCompareExchange(&rear,0,rear); 
      circQ[rear].img = (char *) malloc(1); 
      strcpy(circQ[rear].img,img); 
      circQ[rear].channel = channel; 
      circQ[rear].imgWidth = imgWidth; 
      circQ[rear].imgHeight = imgHeight; 
      circQ[rear].imgLen = imgLen;    

      //q[rear]=x; 
     } 
     else 
     { 
      if((front==0&&rear==-1)||(rear!=front-1)) 
      { 
       InterlockedExchangeAdd(&rear,1); 
       circQ[rear].img = (char *)malloc(l); 
       strcpy(circQ[rear].img,img); 
       circQ[rear].channel = channel; 
       circQ[rear].imgWidth = imgWidth; 
       circQ[rear].imgHeight = imgHeight; 
       circQ[rear].imgLen = imgLen; 
       //q[rear]=x; 
      } 
     } 
    } 
} 
void delet() 
{ 
    char a[20]; 
    // char *temp; 

    if((front==0)&&(rear==-1)) 
    { 
     printf("Queue is underflow\n"); 
     return; 
     //exit(0); 
    } 
    if(front==rear) 
    { 
     //a=q[front]; 
     strcpy(a,circQ[front].img); 
     //temp = circQ[front]; 
     //free(temp); 
     //free(circQ[rear].img); 
     InterlockedCompareExchange(&rear,-1,rear); 
     InterlockedCompareExchange(&front,0,front); 
    } 
    else 
     if(front==max-1) 
     { 
      //a=q[front]; 
      strcpy(a,circQ[front].img); 
      //free(circQ[rear].img); 
      //temp = circQ[front]; 
      //free(temp); 
      InterlockedCompareExchange(&front,0,front); 
     } 
     else 
      { 

       strcpy(a,circQ[front].img); 
       //free(circQ[rear].img); 
       temp = &circQ[front]; 
       free(temp);    // in this part problem is occurring 
       InterlockedExchangeAdd(&front,1); 
       //a=q[front]; 
      } 
     printf("Deleted element is:%s\n",a); 
     free(&circQ[front]); 


} 

頭文件:

#include <stdio.h> 
#include <malloc.h> 
#include <stdint.h>  

typedef struct Image_details 
{  
    char *img;  
    int channel;  
    int imgWidth;  
    int imgHeight;  
    int imgLen; 
}Image_details_t;  

void insert(char *img,int channel,int imgWidth,int imgHeight,int imgLen); 
void delet(); 

回答

3
  1. 你釋放一個非堆變量,你不應該刪除此

    free(&circQ[front]); 
    
  2. 你爲img成員分配空間,只是一個字節,一個空字符串需要終止'\0'一個字節,那麼你做的strcpy()這是指字符串,即非nul字節,隨後的序列由nul,字節。

    也許你的意思是

    memcpy(circQ[rear].img, img, 1); 
    

    這也是一樣

    circQ[rear].img[0] = img[0]; 
    
  3. 您應該檢查的malloc()返回值。

  4. 如果您需要使用錯誤的編譯器或錯誤的編程語言,則不需要返回malloc()的返回值。

+0

I HV試圖與遊離(circQ [前] .IMG)也但仍即時得到同樣的問題 –

+0

因爲它應該是免費'(circQ [從] .IMG);'而不的'&'地址運營商。另外,如果'img'總是隻保存一個字節,不要'malloc()'使它成爲'char img [1]'。 –

+0

和分配給img成員的空間不是1它的變量'L' –