Breakpoint triggered in SDL_FreeSurface

I got this problem while working with a SDL app of mine that occurs in one my functions the calls SDL_FreeSurface. The problem is that I get a dialog box(shown below) when debugging and Windows triggers a breakpoint in my exe.

How do I solve this problem.

bool Sprite::updateSurface()
{
	// WIP:

	if (surface)
		SDL_FreeSurface(surface); // <-- The green arrow.

	int depth;

	switch (bytesPP)
	{
	case 1:
		depth = 8;
		break;
	case 2:
		depth = 16;
		break;
	case 3:
		depth = 24;
		break;
	case 4:
		depth = 32;
		break;
	default:
		depth = 0;
		break;
	}

	surface = SDL_CreateRGBSurface(0,width,height,depth,0x00ff0000,0x0000ff00,0x000000ff,0x00000000);
	if (!surface)
		return false;

	/* Lock the screen for direct access to the pixels */
    if (SDL_MUSTLOCK(surface))
	{
        if (SDL_LockSurface(surface) < 0)
		{
            SDL_GetError();
            return false;
        }
    }

	Uint8 *pixel = (Uint8 *) surface->pixels;
	Uint32 *write;

	for (Uint32 i = 0; i < pixelCount; i++)
	{
		write = (Uint32 *) pixel;
		*write = pixels[i*bytesPP];
		pixel += bytesPP;
	}

	if (SDL_MUSTLOCK(surface))
		SDL_UnlockSurface(surface);

	return true;
}

Open in new window

dialog.jpg
callstack.jpg
sprite.h
sprite.cpp
test.cpp
AntonKarlssonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HappyCactusCommented:
Is surface been initialized? Do not rely on C++ to call default initialization on members you do not call explicitly on constructor, do it yourself explicitly!

0
AntonKarlssonAuthor Commented:
Ok, I've figured it out now. It's about memory corruption in surface->pixels which happens when writing past the allocated scope that causes this when releasing the memory at the SDL_FreeSurface call.

It was the 32bit integer 'write' in the for copy loop in updateSurface in its last assignment that caused the memory corruption.
bool Sprite::updateSurface()
{
	// WIP:

//	int check = _CrtCheckMemory();

	if (surface)
		SDL_FreeSurface(surface);

	int depth;

	switch (bytesPP)
	{
	case 1:
		depth = 8;
		break;
	case 2:
		depth = 16;
		break;
	case 3:
		depth = 24;
		break;
	case 4:
		depth = 32;
		break;
	default:
		depth = 0;
		break;
	}

	surface = SDL_CreateRGBSurface(0,width,height,depth,0x00ff0000,0x0000ff00,0x000000ff,0x00000000);
	if (!surface)
		return false;

	/* Lock the screen for direct access to the pixels */
    if (SDL_MUSTLOCK(surface))
	{
        if (SDL_LockSurface(surface) < 0)
		{
            SDL_GetError();
            return false;
        }
    }

	Uint8 *pixel = (Uint8 *) surface->pixels;
	Uint32 *write;

	for (Uint32 i = 0; i < pixelCount; i++)
	{
		write = (Uint32 *) pixel;
		if (i < pixelCount-1) // <-- The solution.
		*write = pixels[i*bytesPP];
		pixel += bytesPP;
	}

	if (SDL_MUSTLOCK(surface))
		SDL_UnlockSurface(surface);

	return true;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Software

From novice to tech pro — start learning today.