简体   繁体   中英

Memory allocation c++

I have a problem with memory allocation, when running the following while loop the occupied RAM of my computer continue to increase, I don't understand what I have to allocate and how to do it

while(cvWaitKey(1)<0)
{  
    img = cvQueryFrame(capture);
    height = img->height;         
    width = img->width;           
    step = img->widthStep;          
    channels = img->nChannels;  
    gray_arr = (uchar * * )malloc(sizeof(uchar * )*(height + 1));
    for(int i=0; i<height; i++)
        {
            gray_arr[i] = (uchar * )malloc(sizeof(uchar)*(width + 1));
            for(int j=0; j<width; j++)
                {
                    gray_arr[i][j] = (0.11*data[i*step + j*channels] + 0.56*data[i*step + j*channels + 1] + 0.33*data[i*step + j*channels + 2]);
                }
        }
                    for(int j=0; j<width; j++)
                    {
                       sum=0;
                       for(int i=first_pixel; i<last_pixel+1; i++)
                       {
                       value= gray_arr[i][j];
                       sum = sum + value;
                       }
                       sum=sum/sample_number; 
                       store[j]= sum;
                     }
        for(int i=0; i<width; i++)
        {
           intensity_inv[i]=270- store[i];
        }
        graph = cvCreateImage(cvSize(1280,300),IPL_DEPTH_8U,1);
        cvRectangle(graph, cvPoint(256,0), cvPoint(255,1), CV_RGB(255,255,255),1);
        for (int i=0; i<width; i++)
        {
            cvLine(graph,cvPoint(i,270),cvPoint(i,intensity_inv[i]),CV_RGB(0,0,0)); 
        }
cvNamedWindow("Sp",CV_WINDOW_AUTOSIZE);
cvMoveWindow("Sp",0,550);
cvNamedWindow("Original Image", CV_WINDOW_NORMAL);
cvMoveWindow("Original Image", 500, 0);
cvShowImage("Original Image", img);
cvShowImage("Sp",graph);
free(gray_arr); 
}

Sorry for my bad english

Thanks

You're calling malloc for the gray_arr array:

gray_arr = (uchar * * )malloc(sizeof(uchar * )*(height + 1));

and also for elements within that array:

gray_arr[i] = (uchar * )malloc(sizeof(uchar)*(width + 1));

However, you're only calling free for the allocation of the entire array. You need to also free up the allocation of each element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM