Search code examples
c++compilationsegmentation-faultruntime-error

vector<int> pushback causes runtime error?


For some reason, if I don't have a certain line commented out my code doesn't work.

Here are my three files: Maze.hpp, Kruskal.cpp, main.cpp,

#include <vector>
#include <utility>
#include <cstdlib>

using namespace std;

class KruskalGenerator{
    private:
        void GetNextDirection();
    public:
};
#include "Maze.hpp"

void KruskalGenerator::GetNextDirection(){
    vector<int> pIndex = {};

    pIndex.push_back(1); // <----- This for some goddamn reason causes runtime error
}
#include <iostream>
#include "Maze.hpp"

using namespace std;

int main(){

    //Maze picture = Maze{10};
    KruskalGenerator kSolver;
    //kSolver.Reset(picture);

    cout << "X\n";

    return 0;
}

If I don't compile the code with that line commented out pIndex.push_back(i) in Generators/Kruskal.cpp then running the executable does not return "X" as it should. Otherwise, if I do comment it out then it prints X just fine. Why could compiling that uncommented code specifically cause a run time error?

I was originally working with a vector of pairs when I found this issue, but I realized something was horribly wrong when even a vector of integers wasn't behaving. What am I doing wrong?

I've tried forcing the version with --std=c++17, and even different warning flags. Nothing. Not even a "Segmentation fault" response from running the executable, even though that error is the only one I know which is similar to this type of empty response.

Edit: Running it in gdb gives me a During startup program exited with code 0xc0000139 error code. It could be related to an environment variable issue, but I've raised MinGw bin to the highest in my path, so I'm not sure if it's that. I'm on GCC version 12.1.0 if that helps anyone.

Also of note is that my other projects compile and run fine, except for this one.


Solution

  • The issue was that there were two DLLs on my machine. One was old and outdated, the other new and updated. Windows was defaulting to the old one, so I overwrote that DLL with the new one.

    A better solution was most likely to simply delete the old DLL that way it wouldn't conflict, but since for some reason the old one was packaged with Git and I rely on Git a lot I didn't want to break anything.