#seg fault

1 messages · Page 1 of 1 (latest)

stoic herald
#

for some reason when trying to use a member variable of a class i get a seg fault. i get the seg fault 100% of the time in debug and only sometimes normally

obsidian plankBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

#

@stoic herald

Screenshots!

Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!

stoic herald
#
void Bitboard::setPiece(int square)
{
    board |= (1ULL << square);
}

void Bitboard::removePiece(int square)
{
    board &= ~(1ULL << square);
}
lilac gorge
#

In the debugger, look at what this is 😛

#

You probably have a void pointer or otherwise garbage pointer to your class instance

stoic herald
#
class Bitboard
{
public:
    Bitboard() : board(0ULL) {};

    void setPiece(int square);
    void removePiece(int square);
    bool isOccupied(int square);
    void print();

private:
    uint64_t board;
};

also heres the bitboard class

stoic herald
lilac gorge
#

what OS are you on

#

and what compiler

stoic herald
#

windows

stoic herald
#

yea g++

lilac gorge
#

ok

#

have you ever used WSL?

stoic herald
#

no?

lilac gorge
#

there's a tool called address sanitizer that will help you debug this but it doesn't work well for gcc on windows

#

it does, however, work in WSL

stoic herald
#

is there some other way to check why i get a seg fault?

tranquil rain
#

Why aren't you using something like std::bitset @stoic herald

tranquil rain
#

It does what youre currently doing manually

#

its a bitfield

#

pieces.set(N) pieces.reset(N) pieces.flip(N) where N is the bit index

#

!cppref bitset

obsidian plankBOT
stoic herald
#

thx

#

uh
now i got a seg fault in bitset

tranquil rain
#

the segfault is not an issue of this class

#

its caused by something else

#

show the usage of the class please

#

i told u about bitset so u dont do the bitwise stuff manually and do something wrong somewhere:

class Bitboard
{
public:
    Bitboard() : board{} {};

    void setPiece(int square) {
      board.set(square);
    }
    void removePiece(int square) {
      board.reset(square);
    }
    bool isOccupied(int square) {
      return board.test(square);
    }
    void print();

private:
    std::bitset<8*8> board{};
};
stoic herald
#
std::vector<Move> MoveGen::generatePawnMoves(Board& board, bool isWhite)
{
    legalMoves.clear();

    Bitboard bitboard = board.getBitboards()[isWhite ? PieceTypes::WhitePawn : PieceTypes::BlackPawn];
    // bitboard.toString();
    
    for (int square = 0; square < 64; square++)
    {
        int file = square % 8;
        int rank = (square / 8);

        if (isWhite)
        {
            if (!bitboard.isOccupied(square))
            {
                continue;
            }

            //std::cout << bitboard.isOccupied(square + 8) << '\n';
            if (rank == 1 && !bitboard.isOccupied(square + 8) && !bitboard.isOccupied(square + 16))
            {
                //std::cout << "this called" << '\n';
                legalMoves.push_back(Move{ square, square + 16 });
            }
            if (!bitboard.isOccupied(square + 8) && rank < 6)
            {
                legalMoves.push_back(Move{ square, square + 8 });
            }
            //if (bitboard.isOccupied(square)) std::cout << "Test: " << bitboard.isOccupied(50) << ' ' << square << '\n';

        }
    }

    return legalMoves;
}
#

heres a function that uses the bitboards

#

(ignore that what its supposed to do is false)

tranquil rain
#

show me where the board& gets passed in

#

Bitboard bitboard = board.getBitboards()[isWhite ? PieceTypes::WhitePawn : PieceTypes::BlackPawn];

stoic herald
tranquil rain
#

ensure this is correct also

stoic herald
tranquil rain
#

theres an issue somewhere somehow

stoic herald
#

i can send a bunch of code related to the bitboards

lilac gorge
#

But I recommend address sanitizer because memory errors manifest far from their source, address sanitizer helps you find the problem at its source

#

You can step around in a debugger and try to see where things go wrong

#

If your project is small you can use address sanitizer on godbolt.org otherwise I would try to setup WSL and build/run with gcc there

stoic herald
stoic herald
# stoic herald i can send a bunch of code related to the bitboards
std::array<Bitboard, 12> bitboards;
std::array<Bitboard, 12> Board::getBitboards()
{
    return bitboards;
}
void Board::makeMove(int fromFile, int fromRank, int toFile, int toRank, PieceTypes pieceType)
{
    int square = (8 * fromRank) + fromFile;

    

    board[square]->move(toFile, toRank);
    board[(8 * toRank) + toFile] = std::move(board[square]);
    bitboards[pieceType].removePiece(square);
    bitboards[pieceType].setPiece((8 * toRank) + toFile);
}
lilac gorge
#

how much code is it total?

#

could you send us everything?

stoic herald
#

i could also live share if you use vscode

tranquil rain
#

do u have a memcpy or a memset anywhere?

stoic herald
tranquil rain
#

weird

stoic herald
lilac gorge
#

it would be best if you could either send a few files directly or send a minimal reproducible portion

stoic herald
#

not really minimal but yea

lilac gorge
#

It would be good if we could reproduct with just the bitboard stuff, none of the graphics things

#

It might not be possible, memory errors manifest far from their source

stoic herald
#

i can try

lilac gorge
#

But it would be helpful yeah

stoic herald
#

with a quick test i did where it just kinda looped from 0 to 63 and set and reset each bit there were no seg faults

#

oh

#

i think i found the bug

#

im apparently trying to access bitboards[3131961357]

stoic herald
#
Pawn::Pawn(bool isWhitePiece, int rank, int file, TextureLoader& textureLoader) : Piece(isWhitePiece ? PieceTypes::WhitePawn : PieceTypes::BlackPawn, isWhitePiece, rank, file)

for some reason the first parameter of Piece gets initialized with garbage

#

ok i fixed

#

thanks for the help

obsidian plankBOT
#

@stoic herald Has your question been resolved? If so, type !solved :)

stoic herald
#

!solved

obsidian plankBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

lilac gorge
tranquil rain