Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Access violation with TSLCRealMatrixBuffer
06-19-2012, 08:08 AM
Post: #1
Access violation with TSLCRealMatrixBuffer
Hi

I write here because I am now debugging for hours and I do not have any clue what I am doing wrong.

I got two pointers to two TSLCRealMatrixBuffers. The first one contains little frames from a movie, each row of the buffer is one frame, and the second one contains a 2 if that frame is a movie frame and a 1 if that frame is an advertisement frame.

I am now creating two new buffers which I fill with the old data. I do not copy all rows. In the new buffers I want the same amount of movie and ad frames.

When I run the program I get every time an access violation either on newTrainigData or on newResponses. Strange is that it occurs in different lines sometimes when I copy the trainingData and sometimes when I copy the responses and rarely when I want to get the responses in the calling method.

The access violation does not occur at the first access. The data is copied in a loop and the access violation occurs at i=16 or 54 or something.

The debugger shows me that the pointer to the buffer is not NULL and the address of the pPasBuffer is not NULL too. But this FInstance property of the buffer is either NULL or it says ?????.

Are there any problems with pointers to TSLCRealMatrixBuffer? Or am I doing something wrong?

I appended the C++ Builder source code. SignalLab Version is 5.0.2 OS is Windows 7 x64

Code:
int noAdFrames = 0;
int noMovFrames = 0;
bool* frameRead = new bool[responses->GetRows()];
TSLCRealMatrixBuffer* newTrainingData = 0;
TSLCRealMatrixBuffer* newResponses = 0;
int rows = 0;

// initialise to false
for (int i = 0; i < responses->GetRows(); i++) {
    frameRead[i] = false;
}

// count frames
for (int i = 0 ; i < framesStored; i++) {
    if((*responses)[i][0] == MOVIE) {
        noMovFrames++;
    }
    else if ((*responses)[i][0] == AD) {
        noAdFrames++;
    }
}

// determine which type of frame occurs less often and create the
// new training data matrix for double the size of the less often
// frame type
if ( noAdFrames != noMovFrames) {
    if (noAdFrames < noMovFrames) {
        newTrainingData = new TSLCRealMatrixBuffer(noAdFrames * 2, trainingData->GetCols());
        newResponses = new TSLCRealMatrixBuffer(noAdFrames * 2, 1);
        rows = noAdFrames * 2;

    }
    else {
        newTrainingData = new TSLCRealMatrixBuffer(noMovFrames * 2 + 1, trainingData->GetCols());
        newResponses = new TSLCRealMatrixBuffer(noMovFrames * 2 + 1, 1);
        rows = noMovFrames * 2;
    }

    // seed random number generator
    srand( (unsigned)time( NULL ) );

    // fill the new training data matrix with the data from the old matrix

    int loopStop = rows * 0.75;
    for (int i = 0; i < loopStop; i++) {    // only till 75% of the buffer to ensure good performance
        int randomFrame = rand() % framesStored;

        // has this frame already been copied?
        // MOVIE and AD parts should alternate
        while (frameRead[randomFrame] == false
            && ((i % 2 == 0 && (*responses)[i][0] == MOVIE)
            || (i % 2 == 1 && (*responses)[i][0] == AD))) {
            randomFrame = rand() % framesStored;
        }

        // copy data
        newResponses->SetItem(i, 0,
            responses->GetItem(randomFrame, 0));

        for (int j = 0; j < trainingData->GetCols(); j++) {
            newTrainingData->SetItem(i, j,
                trainingData->GetItem(randomFrame, j));
        }

        frameRead[randomFrame] = true;
    }

    // do the remeining 25%
    for (int i = newTrainingData->GetRows() * 0.75; i < framesStored; i++) {
        for (int j = 0; j < trainingData->GetRows(); j++) {
            if (frameRead[j] == false
                && ((i % 2 == 0 && (*responses)[j][0] == MOVIE)
                || (i % 2 == 1 && (*responses)[j][0] == AD))) {
                // copy data
                (*newResponses)[i][0] = (*responses)[j][0];

                for (int v = 0; v < trainingData->GetCols(); v++) {
                    (*newTrainingData)[i][v] = (*trainingData)[j][v];
                }

                frameRead[j] = true;
                break;
            }
        }
    }

    // clean up old pointers
    delete frameRead;
    delete trainingData;
    delete responses;

    // redirect old pointers to new data
    responses = newResponses;
    trainingData = newTrainingData;
}

Feel free to ask more questions about the program or the code.
This bug drives me crazy!

benjamin
Quote this message in a reply
Post Reply 


Messages In This Thread
Access violation with TSLCRealMatrixBuffer - benjamin - 06-19-2012 08:08 AM

Forum Jump:


User(s) browsing this thread: 1 Guest(s)