Hi !
A few days ago, I tried the latest release of SDL2 (wanted to try new features, SDL_RenderGeometryRaw looks awesome!)
I’m testing it in a tiny video game i’m developping and I started to saw some strange crash, mostly at the end of the program (on SDL_Quit and TTF_Quit)
At the beginning, I was sure I messed up something somewhere; I thought I did probably a mistake a few months ago and the update just highlight it (like bad use of new and delete, memory management etc … you know what im talking about)
But after more deep testing, i’m in big trouble; I managed to code something that work on older SDL2 version (tested in 2.0.15) but not in the latest build (2.0.22)
After initalising everything, the code create a dynamic array of 1000 Struct, every struct contening a vector of shared_ptr, fill with 1000 Member.
Every Member has a string; (PS: Doing this is ok, isn’t it? I did this a lot but now I’m wondering?)
after allocating the memory, the program can crash if you click on the windows (SDL_MOUSEBUTTONDOWN). (If it did not crash, retry, It will happen)
** If you enter a key, the program will always exists without any problem.**
There is the code:
// Crash using SDL 2.0.22 and SDL_TTF 2.20.0 x64 on VS2019 (c++17, using Windows 10)
// does not crash using older version of SDL2 (testing with 2.0.15)
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <iostream>
#include <vector>
struct Member{
Member(){
someStr = "test";
}
std::string someStr;
};
struct Struct{
Struct(){
// adding 1000 Member into the shared_ptr vector of Struct
// each Member contening a simple string
for(int i = 0; i < 1000; i++){
members.push_back(std::make_shared<Member>(std::move(Member())));
}
}
std::vector<std::shared_ptr<Member> > members;
};
int main(int argc, char *argv[])
{
// initialize everything
if(SDL_Init(SDL_INIT_EVERYTHING) != 0)
return -1;
SDL_Window *window;
window = SDL_CreateWindow("test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480,
SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | SDL_WINDOW_MAXIMIZED | SDL_WINDOW_RESIZABLE);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if(renderer == nullptr)
return -1;
if(TTF_Init() == -1){
return -1;
}
bool end = false;
// creation a dynamic array of 1000 Struct
Struct *structArray = new Struct[1000];
std::cout << "memory allocated; mouse click for crash or press a key to exit the program normally \n";
SDL_Event eventSDL;
while(!end){
while(SDL_PollEvent(&eventSDL)){
// does crash half the time (more structArray is big, more often it will occur)
if(eventSDL.type == SDL_MOUSEBUTTONDOWN){
end = true;
}
// does NOT crash
if(eventSDL.type == SDL_KEYDOWN){
end = true;
}
}
}
std::cout << "freeing memory ...\n";
delete[] structArray;
std::cout << "quitting SDL / TTF ...\n";
SDL_Quit();
TTF_Quit(); // <- can crash here only if SDL_MOUSEBUTTONDOWN was activated
std::cout << "exit\n";
return 0;
}
Am I doing something wrong ? Is it bad to have a vector of shared ptr in a member of a dynamic array ?
Thanks in advance !