We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 5013

Summary: exception when using SDL_FreeSurface
Product: SDL Reporter: Hugh McLenaghan <hughmcl>
Component: *don't know*Assignee: Ryan C. Gordon <icculus>
Status: RESOLVED INVALID QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: hughmcl, sylvain.becker
Version: 2.0.10   
Hardware: x86   
OS: Windows 10   
Attachments: example code
modified version for linux
buildmenu screenshot

Description Hugh McLenaghan 2020-03-05 05:26:34 UTC
Here is some sample code I'm using:-

SDL_Surface* textSurface = TTF_RenderText_Shaded(myFont, textureText.c_str(), textColor, bgColor);
	if (textSurface == NULL) {
		printf("Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError());
		printf("text: %s\n",textureText.c_str());
		exit(2);
	} else {
		//Create texture from surface pixels
		mTexture = SDL_CreateTextureFromSurface(myRenderer, textSurface);
		if (mTexture == NULL) {
			printf("Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError());
			exit(1);
		} else {
			//Get image dimensions
			mWidth = textSurface->w;
			mHeight = textSurface->h;
			setBlendMode(SDL_BLENDMODE_NONE);
		}
		//Get rid of old surface
		SDL_FreeSurface(textSurface);
	}


When SDL_FreeSurface is called it causes an exception:-

    SDL_Malloc.c
         (void)SDL_AtomicDecRef(&s_mem.num_allocations);

test.exe has triggered a breakpoint. occurred
Critical error detected c0000374
Comment 1 Sylvain 2020-03-06 11:06:32 UTC
Strange, I bet the malloc are done correctly.

Can you provide a test-case ?
You can also both update SDL2 and SDL2_ttf.
Comment 2 Hugh McLenaghan 2020-03-12 01:41:19 UTC
I've got the latest SDL & SDL_ttf libraries.    It's very possible I'm doing something wrong too.

What I can do if you're interested is give you a copy of my source code (i've got it cut down to somewhat minimal, and it's still showing the bug.

I've got my libraries in D:\SDL2\SDL2\

i know that if i take out the SDL_FreeSurface command, it will run, but after about 5-10 minutes, it stops with an out of memory error, so I know the memory is being assigned and used.   Just for some reason i'm not able to free it.

I'm using Visual Studio 2019 to compile.


http : // www.mclenaghan.com/HughSDL2.zip


If you don't feel comfortable downloading, then let me know what you'd like me to do for troubleshooting.

I'm not a C++, Windows, or SDL expert, so it is possible I've not done something right.
Comment 3 Sylvain 2020-03-12 08:49:29 UTC
I can't download it, so please attach it.

Hope your code is quite reduced. Maybe you can also check for invalid read/write before, with visual studio.
Comment 4 Hugh McLenaghan 2020-03-12 13:48:01 UTC
Created attachment 4250 [details]
example code
Comment 5 Hugh McLenaghan 2020-03-12 13:48:30 UTC
Code attached.   total code size is only about 14K.
Comment 6 Sylvain 2020-03-12 15:04:20 UTC
Created attachment 4251 [details]
modified version for linux

Hi, I quickly looked but I have no Window version updated.
So I modified it to run on linux (see the files attached, remove the build menu, should be minor), 
and it worked well, without invalid memory r/w.

So. Can you test it and see if you still have the issue with this version ?
Maybe I removed unintentionally the issue ..., or maybe the issue is with the window menu set up.
Comment 7 Hugh McLenaghan 2020-03-12 17:23:56 UTC
Created attachment 4252 [details]
buildmenu screenshot
Comment 8 Hugh McLenaghan 2020-03-12 17:24:33 UTC
It didn't appear to help removing the buildmenu call.  Screenshot attached of the run.
Comment 9 Sylvain 2020-03-12 17:32:56 UTC
So this still crashes with my files. Whereas, on linux it runs fine.

I'd say: 
- I believe this has nothing to do with SDL_ttf, you can try to remove this dependency. (remove the string text renderering, use a dummy surface).

- update to latest SDL2 sources also ?

- I see you use std:mutex, could this be releated ?
Comment 10 Hugh McLenaghan 2020-03-12 23:32:16 UTC
If i replace this line:-

    SDL_Surface* textSurface = TTF_RenderText_Shaded(myFont, textureText.c_str(), textColor, bgColor);


with the following:-

SDL_Surface* textSurface = SDL_CreateRGBSurface(0, 30, 30, 32, 0, 0, 0, 0);
SDL_FillRect(textSurface, NULL, SDL_MapRGB(textSurface->format, 255, 0, 0));


Then it works, and i see a red box on the screen.

So this indicates to me that it appears to be an issue with the TTF_RenderText_Shaded function in some way.
Comment 11 Hugh McLenaghan 2020-03-12 23:52:44 UTC
Ok, call me stupid *heh*

I just worked out what the issue was.   It's not a bug!  It was me!

I forgot to copy the SDL_ttf.dll to the debug/release directory.

I re-downloaded the SDL_TTF libraries, and when i was copying them into where all the libraries/includes etc. go, I noticed that the SDL_ttf was a .dll and not a .lib.  Then i thought, well crap, that means it's a runtime and needs to be in the same dir as the .exe.    Once I did that the program no longer crashed and did what it was supposed to.


Thanks for the time you spent on this.   Sorry if I caused any issues.

At least maybe my forgetfulness will help others :)
Comment 12 Hugh McLenaghan 2020-03-13 00:13:03 UTC
It does have SDL2_ttf.lib, so if i'm building a static program with /MT, why is it that the program fails to run unless I have the SDL2_ttf.dll in the runtime directory?    Shouldn't it statically build that library into the .exe?
Comment 13 Sam Lantinga 2020-03-13 02:37:11 UTC
It sounds like the original issue is resolved, thanks!