Search code examples
c++iosobjective-cchess

Stockfish Engine breaks when loaded from a view controller instead of the app delegate iOS


I trying to implement the Stockfish UCI engine into a chess game app. Originally in the Stockfish iOS game the view controller that holds the board and the pieces is loaded from the app delegate. What I am trying to do is have a few more screens before navigating to the game. The problem I have is that the as soon as I load the board screen the game breaks in bitboard.cpp file with the message EXC_BAD_ACCESS(code=1, address=0x1fa80000) and a few times I have managed to load the pieces on the board and move one of them but then it brakes in the same place shown below on this line: occupancy[size] = b;

void init_magics(Bitboard table[], Bitboard* attacks[], Bitboard magics[],
               Bitboard masks[], unsigned shifts[], Square deltas[], Fn index) {

int MagicBoosters[][8] = { {  969, 1976, 2850,  542, 2069, 2852, 1708,  164 },
                           { 3101,  552, 3555,  926,  834,   26, 2131, 1117 } };

RKISS rk;
Bitboard occupancy[4096], reference[4096], edges, b;
int i, size, booster;

// attacks[s] is a pointer to the beginning of the attacks table for square 's'
attacks[SQ_A1] = table;

for (Square s = SQ_A1; s <= SQ_H8; ++s)
{
    // Board edges are not considered in the relevant occupancies
    edges = ((Rank1BB | Rank8BB) & ~rank_bb(s)) | ((FileABB | FileHBB) & ~file_bb(s));

    // Given a square 's', the mask is the bitboard of sliding attacks from
    // 's' computed on an empty board. The index must be big enough to contain
    // all the attacks for each possible subset of the mask and so is 2 power
    // the number of 1s of the mask. Hence we deduce the size of the shift to
    // apply to the 64 or 32 bits word to get the index.
    masks[s]  = sliding_attack(deltas, s, 0) & ~edges;
    shifts[s] = (Is64Bit ? 64 : 32) - popcount<Max15>(masks[s]);

    // Use Carry-Rippler trick to enumerate all subsets of masks[s] and
    // store the corresponding sliding attack bitboard in reference[].
    b = size = 0;
    do {

        occupancy[size] = b;
        reference[size] = sliding_attack(deltas, s, b);

        if (HasPext)
            attacks[s][_pext_u64(b, masks[s])] = reference[size];

        size++;
        b = (b - masks[s]) & masks[s];
    } while (b);

    // Set the offset for the table of the next square. We have individual
    // table sizes for each square with "Fancy Magic Bitboards".
    if (s < SQ_H8)
        attacks[s + 1] = attacks[s] + size;

    if (HasPext)
        continue;

    booster = MagicBoosters[Is64Bit][rank_of(s)];

    // Find a magic for square 's' picking up an (almost) random number
    // until we find the one that passes the verification test.
    do {
        do magics[s] = rk.magic_rand<Bitboard>(booster);
        while (popcount<Max15>((magics[s] * masks[s]) >> 56) < 6);

        std::memset(attacks[s], 0, size * sizeof(Bitboard));

        // A good magic must map every possible occupancy to an index that
        // looks up the correct sliding attack in the attacks[s] database.
        // Note that we build up the database for square 's' as a side
        // effect of verifying the magic.
        for (i = 0; i < size; ++i)
        {
            Bitboard& attack = attacks[s][index(s, occupancy[i])];

            if (attack && attack != reference[i])
                break;

            assert(reference[i]);

            attack = reference[i];
        }
    } while (i < size);
}

I have not much experience and knowledge in C++ and I am struggling to figure out what is causing the issue.


Solution

  • I have managed to sort it out now! I figured that the game was still initialising while I was loading its view and this caused the whole issue, because the initialiser was running in the background! To sort it out I have made a loading screen and called the initialisers for the game there! And once it is all set up in the background I load the screen with the game!!! Hope my post helps someone in the future!