Search code examples
c++for-loopdeserialization

My for-loop is getting skipped, even though the debugger says it shouldn't


Somehow, my for-loop (the first one) keeps getting skipped. even after stepping through it with the debugger, i cannot figure out why.

Grid AssetManager::deserializeGrid(std::string const& filename)
{
    std::ifstream rf(filename, std::ios::binary);
    if (!rf)
        std::cout << "Cannot open file!" << std::endl;
    //reading size of grid
    int gx, gy;
    rf.read((char*)&gx, sizeof(int));
    rf.read((char*)&gy, sizeof(int));
    Grid rGrid(gx, gy);
    for (int x = 0; x < gx; x++) 
    {
        //Deserializing code here
    }
    rf.close();
    if (!rf.good())
        std::cout << "Error occurred at reading time!" << std::endl;
    return rGrid;
}

I'm trying to read a saved file here.

While debugging, I see that gx and gy are succesfully read and have a value of 4. Somehow though, the for-loop gets skipped and it jumps straight to the return statement. Edit: I've added 3 couts: one immediately infront of the declaration of rGrid, one directly after and one before the return statement. Only the first gives me an output (verifying that gx and gy are indeed 4). The other two are also getting skipped. It jumps from rGrid directly to the return statement. In the constructor of rGrid, a 2d vector of the size gx, gy gets created, nothing else (I've also checked this and it works without error).


Solution

  • Well, I found the answer (kinda?). I realized that VisualStudio skipped all of the code (when debugging) and jumped to the return statement without actually returning anything and then going back to finish the code. So it only seemed to skip code but it really wasn't. I feel quite stupid not realising this for way too long. I don't know why VisualStudio is doing this (not only on my PC), but hey - at least it's working now.