CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 1999
    Location
    Laughlin, NV
    Posts
    10

    Access Violation w/ "new"

    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 [email protected].
    The Code:


    struct S_LeagueDirType *p_LeagueList;


    unsigned int length = m_MaxLeagueCoord + 1;
    delete [] p_LeagueList;
    p_LeagueList = new struct S_LeagueDirType [length];







  2. #2
    Join Date
    May 1999
    Location
    Jerusalem, Israel
    Posts
    251

    Re: Access Violation w/ "new"

    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.

  3. #3
    Join Date
    Jul 1999
    Location
    Moscow, Russia
    Posts
    667

    Re: Access Violation w/ "new"

    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.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured