samedi 18 avril 2020

C++ random engine crashing due to seemingly unrelated array

I'm developing a terrain generator in OpenGL and encounter a strange bug. I can define how long the triangles that the terrain is made from are like this:

#define TRIANGLE_WIDTH 0.25f 
#define INVERSE_TRIANGLE_WIDTH 4

With those sizes I create arrays, that are used to store the data of the terrain. I have made some typedefs for the array that look like this:

typedef std::array<std::array<glm::vec3, CHUNK_SIDE_LENGHT>, CHUNK_SIDE_LENGHT> MapDataVec3Type;
typedef std::array<std::array<glm::vec2, CHUNK_SIDE_LENGHT>, CHUNK_SIDE_LENGHT> MapDataVec2Type;

This all works but when the TRIANGLE_WIDTH gets smaller then 0.125 I get a crash when running the program.

I am using Xcode and whenever a program crashes, Xcode shows me where the error is in the assembly code:

SDL-OpenGL-Tests-3`main:
    0x100030760 <+0>:     pushq  %rbp
    0x100030761 <+1>:     movq   %rsp, %rbp
    0x100030764 <+4>:     subq   $0x807f60, %rsp           ; imm = 0x807F60 
    0x10003076b <+11>:    movq   0x5b936(%rip), %rax       ; (void *)0x00007fff92ccdd40: __stack_chk_guard
    0x100030772 <+18>:    movq   (%rax), %rax
    0x100030775 <+21>:    movq   %rax, -0x8(%rbp)
    0x100030779 <+25>:    movl   $0x0, -0x291c(%rbp)
    0x100030783 <+35>:    movl   %edi, -0x2920(%rbp)
    0x100030789 <+41>:    movq   %rsi, -0x2928(%rbp)
    0x100030790 <+48>:    leaq   0x4357b(%rip), %rsi       ; "/dev/urandom"
    0x100030797 <+55>:    leaq   -0x2948(%rbp), %rax
    0x10003079e <+62>:    movq   %rax, %rdi
->  0x1000307a1 <+65>:    movq   %rax, -0x807178(%rbp)
    0x1000307a8 <+72>:    callq  0x100030090               ; std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::basic_string<std::nullptr_t> at string:817
    0x1000307ad <+77>:    leaq   -0x2930(%rbp), %rdi

The error is marked by the arrow. From my limited knowledge of assembly I think this part is corespondent to the start of my int main() that looks like this:

int main(int argc, const char * argv[]) {
    std::random_device randomDevice;
    std::mt19937 randomEngine(randomDevice());

    bool capFps = true;
    int fpsCap = 60;
    double frameTimeCap = double(1e6) / double(fpsCap);
    std::chrono::steady_clock::time_point start, end;


    std::cout << "Main Thread ID: " << std::this_thread::get_id() << std::endl;

I really don't get how my terrain generation is affecting the random functions of C++ since I don't use the random engine in my terrain generation, I use it to randomly place some light sources.




Aucun commentaire:

Enregistrer un commentaire