Sign in to follow this  
zappernapper

another attempt with pointers and access violations

Recommended Posts

ok.... my last thread kinda died, and i still have an issue. Alright, here's what's going on: Tetris game, four classes. 1st class - Sprite. 2nd class - (derived from Sprite) Square. 3rd class - Block (holds vector of pointers to square). 4th class - Pile (holds vector of pointers to square that it gets from block) << which is what i'm having probs with
void GameCycle() //main
{
    if (g_pPlayerBlock == NULL)
        g_pPlayerBlock = new Block(PLAYER_LOC.x, PLAYER_LOC.y,
            SQ_IMAGE, playField, O_PLAYER);
    
    if (g_pPlayerBlock != NULL)
        if(!g_pPlayerBlock->MoveBlock(0, 4, *(g_pPile->GetPile()) ) )
            if(g_pPile->GiveBlock(g_pPlayerBlock, 30, 20)) // Access violation
                g_pPlayerBlock = NULL;
    
    InvalidateRect(g_pGame->GetWindow(), NULL, false);
}

//from Pile.cpp
bool Pile::GiveBlock(Block *bl, int xAdj, int yAdj)
{
    if (bl == NULL)
        return false;
    for (int i = 0; i < bl->m_squares.size(); ++i)
    {
        if (bl->m_squares[i] != NULL)
        {
            POINT pt = bl->m_squares[i]->GetPoint(TL); //causes actual violation
            RECT rect = bl->m_squares[i]->GetPosition(); //causes violation too
            pt.x -= xAdj;
            pt.y -= yAdj;*/
            
            int f = bl->m_squares[i]->GetHeight(); // doesn't cause violation
            
            m_Pile.push_back(bl->m_squares[i]); // doesn't cause violation
            m_grid[pt.y][pt.x] = true;
        }
    }
    return true;
}

// from Square
POINT Square::GetPoint(SQPTS pt) //SQPTS is an enum defined in Square
{
    
    
    switch (pt)
    {
        case TL:
        {
            POINT tl = {m_rcPosition.left, m_rcPosition.top};
            return tl;
        }
        
        case TR:
        {
            POINT tr = {m_rcPosition.right, m_rcPosition.top};
            return tr;
        }
        
        case BL:
        {
            POINT bl = {m_rcPosition.left, m_rcPosition.bottom};
            return bl;
        }
        
        case BR:
        {
            POINT br = {m_rcPosition.right, m_rcPosition.bottom};
            return br;
        }
    }
}

// from Sprite (defined inside class definition)
RECT&   GetPosition()             { return m_rcPosition; };

//also in main
void GamePaint(HDC hDC) //all runs fine
{
    g_pBackground->Draw(hDC, 0, 0);
    if (g_pPlayerBlock != NULL)
        g_pPlayerBlock->DrawBlock(hDC);
    g_pPile->DrawPile(hDC); //will not draw if empty, does draw fine otherwise
}
hopefully that's enough info for ne1, i could really use some help. i don't know if it's because the one function uses an enum (which worked fine when the Block class was calling it), or the other one is defined inside of the class (which doesn't make sense).

Share this post


Link to post
Share on other sites
I notice both the functions that cause AVs are returning structures.

Generally, returning structures like that is best avoided, because it is inefficient, and can result in weird bugs if the default compiler behaviour doesn't do what you expect it to. Better to pass a pointer or a reference into the function, and let it fill in the values directly, e.g


void Square::GetPoint(SQPTS ptIn, POINT& ptOut)

{
switch (pt)
{
case TL:
{
ptOut.x = m_rcPosition.left;
ptOut.y = m_rcPosition.top;
return;
}
// etc...
}
}




I don't know if this will solve your problem though - returning structures should work properly, as the compiler should generate a default assignment operator for the structure which should work fine for simple structures like RECTs and POINTs.

However, it's worth a try, at least you'll be relying less on the compiler being sensible and generating correct code for you.

Share this post


Link to post
Share on other sites
of course..... that now causes the program to not even load the blocks without causing an error.... hmm... i'll tinker w/ it a bit more.... but i need to sleep now, not functioning good.... ne other suggestions r welcome!!!!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this