Q:

Image processing error

I must implement in C++ using diblok The Seeded Region Growing algorithm due to Adams and Bischof which can be found here http://bit.ly/1nIxphj. 
It is the fig.2 pseudocode.
After I choose the seeded points using the mouse , it throws this message : Unhandled exception at 0x00416ca0 in diblook.exe: 0xC0000005: Access violation reading location 0x3d2f6e68. 
This is the code of the function:
void CDibView::OnLButtonDblClk(UINT nFlags, CPoint point)

{   BEGIN_SOURCE_PROCESSING;
int** labels = new int* [dwHeight];
for(int k = 0;k < dwHeight; k++)
    labels[k] = new int[dwWidth];
int noOfRegions = 2;
double meanRegion[2];
double noOfPointsInRegion[2];
for(int i = 0; i < dwHeight ; i++)
    for(int j = 0; j < dwWidth ; j++)
    {
        labels[i][j] = -1;
    }

if(noOfPoints < 6)
{
    CPoint p = GetScrollPosition() + point;
    pos[noOfPoints].x = p.x;
    pos[noOfPoints].y = p.y;
    int regionLabel = 0;
    if(noOfPoints <= noOfPoints / 2)
        labels[p.x][p.y] = regionLabel;
    else 
        labels[p.x][p.y] = regionLabel + 1;
    noOfPoints++;
}
else
{
    // Calculate the mean of each region

    for(int i = 0; i < noOfRegions; i++)
    {
        for(int j = 0 ; j < noOfPoints; j++)
        {
            if(labels[pos[j].x][pos[j].y] == i)
            {
                meanRegion[i] += lpSrc[pos[j].x * w + pos[j].y];
            }
        }
        meanRegion[i] /= 3;
        noOfPointsInRegion[i] = 3;
    }

    for(int seedPoint = 0; seedPoint < noOfPoints; seedPoint++)
    {

        // define list
        node *start, *temp;
        start = (node *) malloc (sizeof(node));
        temp = start;
        temp -> next = NULL;

        for(int i = -1; i <= 1; i++)
            for(int j = -1; j<= 1; j++)
            {
                if(i == 0 && j == 0) continue;
                int gamma = lpSrc[(pos[seedPoint].x + i) *  + pos[seedPoint].y + j] - lpSrc[pos[seedPoint].x * w + pos[seedPoint].y];
                push(start, pos[seedPoint].x + i, pos[seedPoint].y + j, gamma);

            }

        sort(start);

        if(start != NULL)
        {
            node *y = start; 
            pop(start);
            int sameNeighbour = 1;
            int neighValue = -1;
            for(int k = -1; k <= 1; k++)
                for(int l = -1; l <= 1;l++)
                {
                    if(k ==0 && l==0) continue;
                    if(labels[y -> x + k][y -> y + l] != -1)
                    {
                        neighValue = labels[y -> x + k][y -> y + l];
                        break;
                    }
                }
                for(int k = -1; k <= 1; k++)
                    for(int l = -1; l <= 1;l++)
                    {
                        if(k == 0 && l==0) continue;
                        if(labels[y -> x + k][y -> y = 1] != -1 && labels[y -> x + k][y -> y + l] != neighValue)
                            sameNeighbour = 0;
                    }
                    if(sameNeighbour == 1)
                    {
                        labels[y -> x][y -> y] = neighValue;
                        meanRegion[neighValue] = meanRegion[neighValue] * noOfPointsInRegion[neighValue] / noOfPointsInRegion[neighValue] + 1;
                        noOfPointsInRegion[neighValue]++;

                        for(int k = -1; k <= 1; k++)
                            for(int l = -1; l <= 1;l++)
                            {
                                if(k == 0 && l == 0) continue;
                                if(labels[y -> x + k][y -> y + l] == -1 && find(start, y->x + k, y->y + l) == 0)
                                {
                                    int gammak = meanRegion[neighValue] - lpSrc[(y->x +k) * w + (y->y + l)];
                                    push(start, y->x + k, y->y + l, gammak);
                                    sort(start);
                                }
                            }

                    }
                    else
                    {
                        labels[y->x][y->y] = -1;
                    }
        }

    }

    int noOfRegionOne = 0;
    int noOfRegionTwo = 0;
    int noOfBoundary = 0;

    for(int i = 0; i< dwHeight; i++)
        for(int j = 0;j<dwWidth; j++)
        {
            if(labels[i][j] == -1)
                noOfBoundary++;
            else if(labels[i][j] == 0)
                noOfRegionOne++;
            else if(labels[i][j] == 1)
                noOfRegionTwo++;
        }

        CString info;
        info.Format("Boundary %d, One %d, Two %d", noOfBoundary, noOfRegionOne, noOfRegionTwo);
        AfxMessageBox(info);

    noOfPoints = 0;
}

CScrollView::OnLButtonDblClk(nFlags, point);

END_SOURCE_PROCESSING;

}
After a choose to break the running, this is what is shown http://postimg.org/image/j2sh9k0a1/
Can anybody tell what is wrong and why it doesn't work?
Thanks.

A:

Your screenshot shows that your node (Y is a terrible name, incidentally) has garbage values in it.  Offhand, I suspect that 'sort' is overwriting your node values, resulting in garbage.  I would create a static copy of your current node to prevent it from changing during processing:
Change
    node *y = start; 
    pop(start);

to
    node y = *start; 
    pop(start);

