The bottom line is that some function allocates memory, but does not delete it. I also know that the problem is in the draw function of the Window class or in the functions that draw uses for itself.

The source code of possibly problematic functions I will post here, and the rest of the code on pastebin

void Window::draw(Object toDraw) { SDL_RenderCopy(coreRenderer, convertSurfaceToTexture(toDraw.getSurface(), coreRenderer), NULL, NULL); } SDL_Texture *convertSurfaceToTexture(SDL_Surface *image, SDL_Renderer *ren) { SDL_Texture *texture; texture = SDL_CreateTextureFromSurface(ren, image); if(texture == nullptr){ std::cout << "Ошибка конвертирования SDL_Surface в SDL_Texture: " << "\n\t" << SDL_GetError() << std::endl; exit(1); } return texture; } 

Since I have no reputation points, I’ll give you a link that will contain links -_-

  • 1) Learn to read the documentation. 2) Learn to debug your programs. :) - Vlad from Moscow
  • So I have been fighting for a long time already :) If I do it manually without any self-made "wrappers", everything is fine. Yes, and memory like everywhere where it is necessary to be released - witaway
  • And I use the debugger :) - witaway
  • If you have classes that have pointers as data members, then for these classes you should at least explicitly write a copy constructor, a copy assignment operator, and a destructor. - Vlad from Moscow
  • In the code, it does not work for me only the function. I just do not understand where the error is. Well, I did not bother to do the copy designer, but the problem is still not that - witaway

3 answers 3

All right, your memory is eaten, you create textures and never delete them. For each SDL_CreateTextureFromSurface call, SDL_CreateTextureFromSurface must be a corresponding SDL_DestroyTexture call.


Your code should probably look something like this:

 auto texture = convertSurfaceToTexture(toDraw.getSurface(), coreRenderer); SDL_RenderCopy(coreRenderer, texture , NULL, NULL); SDL_DestroyTexture(texture); 

I would do all this through unique_ptr , but this is up to you.

PS I do not understand the SDL, but according to the logic of things at this stage it is already possible to remove the texture.

  • After the return, the function cannot be executed. How then can I call SDL_DestroyTexture for texture if I'm already outside the function? - witaway
  • @ YegorLevonenko, updated the answer - ixSci
  • Hmm, the debugger talks about SIGSEGV and throws it on the convertSurfaceToTexture function - witaway
  • @EgorLevonenko, I made minimal changes in your code, if there was a SIGSEGV, it means it was transferred to my code. Where does it come from - deal with the debugger. - ixSci

convertSurfaceToTexture allocates a new SDL_Texture object that nobody deletes or saves - this is an obvious leak.

  • Ie, it is necessary to save what convertSurfaceToTexture returns to a separate variable, use, and then delete? - witaway
  • 'void Window :: draw (Object toDraw) {SDL_Texture * texture = convertSurfaceToTexture (toDraw.getSurface (), coreRenderer); SDL_RenderCopy (coreRenderer, texture, NULL, NULL); SDL_DestroyTexture (texture); } 'Somehow it happens? - witaway

And the answer is traditional for such questions.

Debugging does not help.

So, you need to add to the debugger more tools for tracking errors working with memory. For the C-developer, they are not just complementary to the debugger, they are used along with it as the main tool. About C ++ I will not say, I write on it a little.

  1. Valgrind (console, short description in Russian )
  2. Valgrind for Windows
  3. A large list of similar utilities on SO (Windows)
  4. Catching memory errors in Visual Studio
  5. Catching memory errors in Eclipse (Linux):

    enter image description here

Plus to them - static code analyzers, which in some cases even before starting the program can catch potentially dangerous places (for example, overwriting the value of the pointer to which the allocated memory is stored).

  • Thank you, did not know about such means :) - witaway
  • And for qtCreator under linux there is nothing like that? - witaway
  • @ YegorLevonenko, there seems to be: doc.qt.io/qtcreator/creator-valgrind-overview.html (I did not check it myself, because I don’t use the creator) PinkTux