Skip to main content
gamedev.net
🔒 Locked

Memory Corruption bug - Need Help

Started by utilae 0 replies 400+ views
Original Post
utilae
utilae
Hi, I am using C++ and Visual Studio .NET 2003. I am getting the following errors (in debug mode) when I try to access a variable:
HEAP[Project.exe]: Invalid allocation size - FEEEFF14 (exceeded 7ffdefff) First-chance exception at 0x7c81eb33 in Project.exe: Microsoft C++ exception: std::bad_alloc @ 0x0012f4e0. HEAP[Project.exe]: Invalid allocation size - FEEEFF13 (exceeded 7ffdefff) First-chance exception at 0x7c81eb33 in Project.exe: Microsoft C++ exception: std::bad_alloc @ 0x0012ee70. First-chance exception at 0x7c81eb33 in Project.exe: Microsoft C++ exception: [rethrow] @ 0x00000000. Unhandled exception at 0x7c81eb33 in Project.exe: Microsoft C++ exception: std::bad_alloc @ 0x0012ee70. The program '[776] Project.exe: Native' has exited with code 0 (0x0). [/Quote] I have a variable called m_sFontName, which is readable in the function 'AddInterfaceTextbox', but is not readable outside that function (app crashes). Here is the function 'AddInterfaceTextbox':

//add interface item textbox
//this function basically creates a textbox item according to user needs and
//adds the textbox item to a vector container in my InterfaceManager class.
void CInterfaceManager::AddInterfaceTextbox(const string &sTextboxName,const string &sWindowName,const long &lPosRecTop,const long &lPosRecLeft,const long &lPosRecRight,const long &lPosRecBottom,const int &nCornerIndent,const bool &bEditable,const int &nNumSelections,const string &sDisplayText,const int &nMaxTextLength,const string &sFontName,const DWORD &dwColor,const bool &bWrapText,const bool &bBorder,const string &sAlignment)
{
	CTextBox newTextBox;
	newTextBox.m_sTextBoxName=sTextboxName;//textbox name
	newTextBox.m_bEditableTextBox=bEditable;//user can enter text into textbox
	newTextBox.m_sDisplayText=sDisplayText;//stores text entered by the user and displays it
	newTextBox.m_bCaretVisible=false;
	newTextBox.m_sFontName=sFontName;//this variable become corrupt
	newTextBox.m_dwTextColor=dwColor;//color of text
	newTextBox.m_bWrapText=bWrapText;//wrap text to the next line
	newTextBox.m_bBorder=bBorder;//draw textbox border
	newTextBox.m_nMaxTextLength=nMaxTextLength;//max length of display text
	newTextBox.m_dwLastTimeCaret=0;
	newTextBox.m_dwLastTimeHighlight=0;
	newTextBox.m_nNumSelections=nNumSelections;//>1 for selectable textboxes

	//set text alignment to: left (1), center (2), right (3)
	if(sAlignment=="Left")
		newTextBox.m_nAlignment=1;//Left
	else if(sAlignment=="Center")
		newTextBox.m_nAlignment=2;//Center
	else if(sAlignment=="Right")
		newTextBox.m_nAlignment=3;//Right
	else
		newTextBox.m_nAlignment=1;//Left if mistake is made

	//set textbox tiles
	newTextBox.m_sTextboxTileTopLeft="TextboxTopLeft";
	newTextBox.m_sTextboxTileTopRight="TextboxTopRight";
	newTextBox.m_sTextboxTileBottomLeft="TextboxBottomLeft";
	newTextBox.m_sTextboxTileBottomRight="TextboxBottomRight";
	newTextBox.m_sTextboxTileTop="TextboxTop";
	newTextBox.m_sTextboxTileLeft="TextboxLeft";
	newTextBox.m_sTextboxTileRight="TextboxRight";
	newTextBox.m_sTextboxTileBottom="TextboxBottom";
	newTextBox.m_sTextboxTileCenter="TextboxCenter";
	//set caret tile
	newTextBox.m_sCaretTile="Caret";
	newTextBox.m_bSelected=false;
	
	//store corner indent amount
	newTextBox.m_nCornerIndent=nCornerIndent;

	//overall textbox rect
	newTextBox.m_recTextboxPos.top=lPosRecTop;//top rect coordinate
	newTextBox.m_recTextboxPos.left=lPosRecLeft;//left rect coordinate
	newTextBox.m_recTextboxPos.right=lPosRecRight;//right rect coordinate
	newTextBox.m_recTextboxPos.bottom=lPosRecBottom;//bottom rect coordinate

	//topleft rect
	newTextBox.m_recTextboxTopLeft.top=lPosRecTop;
	newTextBox.m_recTextboxTopLeft.left=lPosRecLeft;
	newTextBox.m_recTextboxTopLeft.right=lPosRecLeft+9;
	newTextBox.m_recTextboxTopLeft.bottom=lPosRecTop+9;
	//topright rect
	newTextBox.m_recTextboxTopRight.top=lPosRecTop;
	newTextBox.m_recTextboxTopRight.left=lPosRecRight-9;
	newTextBox.m_recTextboxTopRight.right=lPosRecRight;
	newTextBox.m_recTextboxTopRight.bottom=lPosRecTop+9;
	//bottomleft rect
	newTextBox.m_recTextboxBottomLeft.top=lPosRecBottom-9;
	newTextBox.m_recTextboxBottomLeft.left=lPosRecLeft;
	newTextBox.m_recTextboxBottomLeft.right=lPosRecLeft+9;
	newTextBox.m_recTextboxBottomLeft.bottom=lPosRecBottom;
	//bottomright rect
	newTextBox.m_recTextboxBottomRight.top=lPosRecBottom-9;
	newTextBox.m_recTextboxBottomRight.left=lPosRecRight-9;
	newTextBox.m_recTextboxBottomRight.right=lPosRecRight;
	newTextBox.m_recTextboxBottomRight.bottom=lPosRecBottom;
	//top rect
	newTextBox.m_recTextboxTop.top=lPosRecTop;
	newTextBox.m_recTextboxTop.left=lPosRecLeft+9;
	newTextBox.m_recTextboxTop.right=lPosRecRight-9;
	newTextBox.m_recTextboxTop.bottom=lPosRecTop+9;
	//left rect
	newTextBox.m_recTextboxLeft.top=lPosRecTop+9;
	newTextBox.m_recTextboxLeft.left=lPosRecLeft;
	newTextBox.m_recTextboxLeft.right=lPosRecLeft+9;
	newTextBox.m_recTextboxLeft.bottom=lPosRecBottom-9;
	//right rect
	newTextBox.m_recTextboxRight.top=lPosRecTop+9;
	newTextBox.m_recTextboxRight.left=lPosRecRight-9;
	newTextBox.m_recTextboxRight.right=lPosRecRight;
	newTextBox.m_recTextboxRight.bottom=lPosRecBottom-9;
	//bottom rect
	newTextBox.m_recTextboxBottom.top=lPosRecBottom-9;
	newTextBox.m_recTextboxBottom.left=lPosRecLeft+9;
	newTextBox.m_recTextboxBottom.right=lPosRecRight-9;
	newTextBox.m_recTextboxBottom.bottom=lPosRecBottom;
	//center rect
	newTextBox.m_recTextboxCenter.top=lPosRecTop+9;
	newTextBox.m_recTextboxCenter.left=lPosRecLeft+9;
	newTextBox.m_recTextboxCenter.right=lPosRecRight-9;
	newTextBox.m_recTextboxCenter.bottom=lPosRecBottom-9;

	//calculate coordinates for each corner point of the textbox
	newTextBox.m_ptTopLeftCorner.x=lPosRecLeft+nCornerIndent;
	newTextBox.m_ptTopLeftCorner.y=lPosRecTop+nCornerIndent;
	newTextBox.m_ptTopRightCorner.x=lPosRecRight-nCornerIndent;
	newTextBox.m_ptTopRightCorner.y=lPosRecTop+nCornerIndent;
	newTextBox.m_ptBottomLeftCorner.x=lPosRecLeft+nCornerIndent;
	newTextBox.m_ptBottomLeftCorner.y=lPosRecBottom-nCornerIndent;
	newTextBox.m_ptBottomRightCorner.x=lPosRecRight-nCornerIndent;
	newTextBox.m_ptBottomRightCorner.y=lPosRecBottom-nCornerIndent;

	//find window
	vector<CWindow>::iterator itWindow=m_lstWindows.begin();
	while(itWindow!=m_lstWindows.end())
	{
		if(itWindow->m_sWindowName==sWindowName)
		{
			//find font
			list<LOADEDFONT*>::iterator itFont=m_lstFonts.begin();
			bool bBreak=false;
			while(itFont!=m_lstFonts.end() && bBreak==false)
			{
				if((*itFont)->m_sFilename==sFontName)
				{
					//store font iterator
					newTextBox.m_itFont=itFont;
					bBreak=true;
				}
				else
					++itFont;
			}

			if(newTextBox.m_bEditableTextBox==true)
			{
				//get width and length of text in pixels
				HDC dcFont=(*newTextBox.m_itFont)->m_Font->GetDC();//get font dc
				SIZE SizeStructure;//size structure to fill with info about size of font
				GetTextExtentPoint32(dcFont,newTextBox.m_sDisplayText.c_str(),newTextBox.m_sDisplayText.length(),&SizeStructure);
				//set caret rect
				newTextBox.m_recCaretPos.top=newTextBox.m_recTextboxCenter.top+1;
				newTextBox.m_recCaretPos.left=newTextBox.m_recTextboxCenter.left+SizeStructure.cx+1;
				newTextBox.m_recCaretPos.right=newTextBox.m_recCaretPos.left+(*newTextBox.m_itFont)->m_FontSize+1;
				newTextBox.m_recCaretPos.bottom=newTextBox.m_recCaretPos.top+(*newTextBox.m_itFont)->m_FontSize-1;
			}

			//z coordinates
			newTextBox.m_fZCoodinate=itWindow->m_fWindowZCoodinate-0.01f;//textbox z value
			newTextBox.m_fCaretZCoodinate=newTextBox.m_fZCoodinate-0.001f;//caret z value

			//record greatest width and height of textbox
			long lWidth=lPosRecRight-lPosRecLeft;
			long lHeight=lPosRecBottom-lPosRecTop;
			if(lWidth>itWindow->m_lMaxWidth)
				itWindow->m_lMaxWidth=lWidth;
			if(lHeight>itWindow->m_lMaxHeight)
				itWindow->m_lMaxHeight=lHeight;

			//add textbox to list
			itWindow->m_lstTextboxes.push_back(newTextBox);
			return;
		}
		else ++itWindow;
	}
}

//this function shows an example of the crash

void CInterfaceManager::DrawInterface(const string &sTextboxName,const string &sWindowName)
{
     //find window
     vector<CWindow>::iterator itWindow=m_lstWindows.begin();
     while(itWindow!=m_lstWindows.end())
     {
          if(itWindow->m_sWindowName==sWindowName)//found window
          {
               //find textbox
               vector<CTextBox>::iterator itTextBox=itWindow->m_lstTextboxes.begin();
               while(itTextBox!=itWindow->m_lstTextboxes.end())
               {
                    if(itTextBox->m_sTextBoxName==sTextboxName)//found textbox
                    {
                         //this line would cause a crash        
                         g_log<<"m_sFontName"<<itTextBox->m_sFontName<<endl;
                         
                         //even if I did this:
                         //itTextBox->m_sFontName="OtherFont";
                         //g_log<<"m_sFontName"<<itTextBox->m_sFontName<<endl;
                         //it would still crash
                    }
                    else
                         ++itTextBox;
               }
          }
          else
               ++itWindow;
     }
}

Anyone able to help?
HTML5, iOS and Android Game Development using Corona SDK and moai SDK

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.