Click to See Complete Forum and Search --> : Access Violation w/ "new"
WBSteele
October 5th, 1999, 03:39 AM
I get an access violation when running the Debug version of the following code (MFC VC++ 4.0). The Release version runs through with no apparent problems. Help as to how to write it properly and avoid the Access Violation (which occurs in processing the last line). Please email all help to CompuBall@hotmail.com.
The Code:
struct S_LeagueDirType *p_LeagueList;
unsigned int length = m_MaxLeagueCoord + 1;
delete [] p_LeagueList;
p_LeagueList = new struct S_LeagueDirType [length];
Jack Shainsky
October 5th, 1999, 03:47 AM
It may cause an access violation if you trying to delete without assigning first. Try to change this code to:
struct S_LeagueDirType *p_LeagueList = NULL;
unsigned int length = m_MaxLeagueCoord + 1;
if (p_LeagueList)
delete [] p_LeagueList;
p_LeagueList = new struct S_LeagueDirType [length];
Hope, it helps.
Jack.
Oleg Lobach
October 5th, 1999, 04:01 AM
Hi,
The problem is that p_LeagueList is initialized by "random" number and delete[] p_LeagueList tries to delete array of S_LeagueDirType at that "random" address.
Try this:
struct S_LeagueDirType *p_LeagueList=0;
unsigned int length = m_MaxLeagueCoord + 1;
delete [] p_LeagueList;// btw this line does nothing
p_LeagueList = new struct S_LeagueDirType [length];
Good luck,
Oleg.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.