I'm getting a error:
First-chance exception at 0x0021F4F2 in Tetris.exe: 0xC0000005: Access violation reading location 0x000000D8.
Unhandled exception at 0x0021F4F2 in Tetris.exe: 0xC000041D: An unhandled exception was encountered during a user callback.
Here is my ColorGrid.cpp file, error happens when I run the application at function name const COLORREF ColorGrid::Index(int iRow, int iCol)
& line return m_buffer[iRow * COLS + iCol];
#include "StdAfx.h"
#include "ColorGrid.h"
#include <exception>
ColorGrid::ColorGrid()
{
}
COLORREF& ColorGrid::Index(int iRow, int iCol)
{
bool flag = false;
if ((iRow >= 0) && (iRow < ROWS) && (iCol >= 0) && (iCol < COLS))
{
flag = true;
}
if (!flag)
{
TRACE("iRow:", iRow, " iCol:", iCol);
}
return m_buffer[iRow + COLS + iCol];
}
const COLORREF ColorGrid::Index(int iRow, int iCol) const
{
try
{
bool flag = false;
if ((iRow >= 0) && (iRow < ROWS) && (iCol >= 0) && (iCol < COLS))
{
flag = true;
}
if (!flag)
{
TRACE("iRow:", iRow, " iCol:", iCol);
}
return m_buffer[iRow * COLS + iCol];
}
catch (CException* e)
{
TCHAR szCause[255];
CString strFormatted;
e->GetErrorMessage(szCause, 255);
TRACE("exception GetErrorMessage:", szCause);
TRACE("iRow:", iRow, " iCol:", iCol, "COLS:", COLS);
e->Delete();
}
}
Related header file ColorGrid.h:
#ifndef COLOR_GRID_H
#define COLOR_GRID_H
const int ROWS = 20;
const int COLS = 10;
class ColorGrid
{
public:
ColorGrid();
void Clear();
COLORREF& Index(int iRow, int iCol);
const COLORREF Index(int iRow, int iCol) const;
void Serialize(CArchive& archive);
private:
COLORREF m_buffer[ROWS * COLS];
};
#endif
Is this because I pre-allocated/initialized m_buffer[ROWS * COLS]
in header & later I want to return m_buffer[iRow * COLS + iCol];
return a different size?
Thanks
return m_buffer[iRow * COLS + iCol];
, which makes NO sense...+
instead of a*
inreturn m_buffer[iRow + COLS + iCol];
your indexing looks ok to me. I'd suggest using a debugger. The problem could be elsewhere and is manifesting here.const
version ofIndex
to return aconst COLORREF
instead of aconst COLORREF&
?