Hey guys, I have a problem with a chess game I'm creating using Windows Forms.
The purpose of this code is that I have a load of panels with picture boxes inside them. When the user moves their cursor over the panel or the picture box, the panel lights up blue. When they move their cursor away, it changes back to its default colour.
Although this code appears to be working correctly (as the panels are changing blue and back again when they are supposed to), if I just run the application and simply move my cursor over each panel (or image), if I do this enough times, the application will crash, giving me the following error:
Problem Event Name: APPCRASH
Application Name: Chess.exe
Application Version: 0.0.0.0
Application Timestamp: 4d3b8d5d
Fault Module Name: clr.dll
Fault Module Version: 4.0.30319.1
Fault Module Timestamp: 4ba1d9ef
Exception Code: c0000005
Exception Offset: 000b9b41
OS Version: 6.1.7600.2.0.0.256.48
Locale ID: 2057
Additional Information 1: 9b33
Additional Information 2: 9b337a60891deea949636df6511bca4e
Additional Information 3: 44f4
Additional Information 4: 44f48f929ff30f1f00f6b46040669596
and this error message on the Visual C++ debugger:
Managed Debugging Assistant 'FatalExecutionEngineError' has detected a problem in 'C:\Users\David\Projects\C++ Projects\Chess\Development Builds\Current Build\Debug\Chess.exe'.
Additional Information: The runtime has encountered a fatal error. The address of the error was at 0x6e689b41, on thread 0x6e4. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.
So my question is, what is causing these errors? Here's my code:
this->SquareID[i]->MouseEnter += gcnew System::EventHandler(this,&Form1::Square_MouseEnter);
this->SquareImageID[i]->MouseEnter += gcnew System::EventHandler(this,&Form1::Square_MouseEnter);
this->SquareID[i]->MouseLeave += gcnew System::EventHandler(this,&Form1::Square_MouseLeave);
this->SquareImageID[i]->MouseLeave += gcnew System::EventHandler(this,&Form1::Square_MouseLeave);
private: System::Void Square_MouseEnter(System::Object^ sender, System:: EventArgs^ e)
{
if(BoardFrozen == 1) return;
int a = -1;
for(int i=0;i<MAX_SQUARES;i++)
{
if(sender == SquareID[i] || sender == SquareImageID[i])
{
a = i;
break;
}
}
if(!IsValidSquareID(a)) return;
if(PreviousSquareSelected == -1)
{
if(!IsSquareOccupiedByAllyOfPlayer(a)) return;
}
if(PreviousSquareSelected > -1)
{
if(a == PreviousSquareSelected) return;
if(!IsValidMove(PreviousSquareSelected,a))
{
this->SquareID[a]->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(230)), static_cast<System::Int32>(static_cast<System::Byte>(0)),
static_cast<System::Int32>(static_cast<System::Byte>(0)));
return;
}
else
{
this->SquareID[a]->BackColor = System::Drawing::Color::LimeGreen;
return;
}
}
this->SquareID[a]->BackColor = System::Drawing::Color::DodgerBlue;
return;
}
private: System::Void Square_MouseLeave(System::Object^ sender, System:: EventArgs^ e)
{
if(BoardFrozen == 1) return;
int a = -1;
for(int i=0;i<MAX_SQUARES;i++)
{
if(sender == SquareID[i] || sender == SquareImageID[i])
{
a = i;
break;
}
}
if(!IsValidSquareID(a)) return;
if(a == PreviousSquareSelected) return;
SetSquareToDefaultColour(a);
return;
}
Thank you very much in advance for any assistance that anyone provides.