Why SDL_SaveBMP crashes my code?

Hello, I’m trying to use this code to load a bmp, modify the data and save it:

    SDL_Surface * s = SDL_LoadBMP(fichorigen.c_str());
if (superficie)
{
	int w = superficie->w, h = superficie->h;

	Uint8 * arraypixels = static_cast<Uint8 *>(s->pixels);
	std::cout << "Imagen " << fichorigen << " ancho -> " << w << " alto

-> " << h << std::endl;
int j = 0;

	for (std::size_t i = 0; i < (w * h * 4); i += 4)
	{
		if (colorkeyb != arraypixels[i] || colorkeyg != arraypixels[i + 1]

|| colorkeyr != arraypixels[i + 2])
{
arraypixels[i + 2] = 255;
arraypixels[i + 1] = 0;
arraypixels[i] = 0;
j += 1;
}
}
const char * nombrefichmascara = (pathdestino + “mascara_” +
bf::basename(fichorigen) + bf::extension(fichorigen)).c_str();

            //The code crashes here
	SDL_SaveBMP(superficie, nombrefichmascara);
	SDL_FreeSurface(superficie);
}

I use this “script” to process images and generate masks of those
images. I don’t know why, but sometimes, and just for some
images, my code crashes. It always crashes for the same images and
works for the same images. I googled a lot but can’t
find an answer.
The error is the following one:

*** glibc detected *** ./maskframe free(): invalid next size (normal):
0x09b2ef90 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7c3e3f4]
/lib/tls/i686/cmov/libc.so.6(cfree+0x96)[0xb7c40456]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0xb7e12dc1]
/usr/lib/libstdc++.so.6(_ZNSs4_Rep10_M_destroyERKSaIcE+0x1d)[0xb7df115d]
./mascarafotograma[0x804ae03]
./mascarafotograma[0x804b0f7]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe5)[0xb7be5685]
./mascarafotograma[0x804a2d1]
======= Memory map: ========
08048000-0804d000 r-xp 00000000 08:08 1253690
/home/german/Documentos/followtherabbitworkspace/mascarafotograma/mascarafotograma
0804d000-0804e000 r–p 00004000 08:08 1253690
/home/german/Documentos/followtherabbitworkspace/mascarafotograma/mascarafotograma
0804e000-0804f000 rw-p 00005000 08:08 1253690
/home/german/Documentos/followtherabbitworkspace/mascarafotograma/mascarafotograma
09b2d000-09b4e000 rw-p 09b2d000 00:00 0 [heap]
b7400000-b7421000 rw-p b7400000 00:00 0
b7421000-b7500000 —p b7421000 00:00 0
b75f9000-b75fc000 rw-p b75f9000 00:00 0
b75fc000-b7600000 r-xp 00000000 08:08 4210720 /usr/lib/libXdmcp.so.6.0.0
b7600000-b7601000 rw-p 00003000 08:08 4210720 /usr/lib/libXdmcp.so.6.0.0
b7601000-b7603000 r-xp 00000000 08:08 2368313 /usr/lib/libXau.so.6.0.0
b7603000-b7604000 rw-p 00001000 08:08 2368313 /usr/lib/libXau.so.6.0.0
b7604000-b7605000 rw-p b7604000 00:00 0
b7605000-b761c000 r-xp 00000000 08:08 4210722 /usr/lib/libxcb.so.1.0.0
b761c000-b761d000 r–p 00016000 08:08 4210722 /usr/lib/libxcb.so.1.0.0
b761d000-b761e000 rw-p 00017000 08:08 4210722 /usr/lib/libxcb.so.1.0.0
b761e000-b761f000 r-xp 00000000 08:08 4210724 /usr/lib/libxcb-xlib.so.0.0.0
b761f000-b7620000 r–p 00000000 08:08 4210724 /usr/lib/libxcb-xlib.so.0.0.0
b7620000-b7621000 rw-p 00001000 08:08 4210724 /usr/lib/libxcb-xlib.so.0.0.0
b7621000-b7626000 r-xp 00000000 08:08 2369719 /usr/lib/libgpm.so.2.0.0
b7626000-b7627000 r–p 00004000 08:08 2369719 /usr/lib/libgpm.so.2.0.0
b7627000-b7628000 rw-p 00005000 08:08 2369719 /usr/lib/libgpm.so.2.0.0
b7628000-b76c3000 r-xp 00000000 08:08 18655 /lib/libslang.so.2.1.3
b76c3000-b76c6000 r–p 0009a000 08:08 18655 /lib/libslang.so.2.1.3
b76c6000-b76d3000 rw-p 0009d000 08:08 18655 /lib/libslang.so.2.1.3
b76d3000-b7709000 rw-p b76d3000 00:00 0
b7709000-b7736000 r-xp 00000000 08:08 16464 /lib/libncurses.so.5.6
b7736000-b7739000 rw-p 0002c000 08:08 16464 /lib/libncurses.so.5.6
b7739000-b773a000 rw-p b7739000 00:00 0
b773a000-b773d000 r-xp 00000000 08:08 16426 /lib/libcap.so.1.10
b773d000-b773e000 rw-p 00002000 08:08 16426 /lib/libcap.so.1.10
b773e000-b7829000 r-xp 00000000 08:08 2369389 /usr/lib/libX11.so.6.2.0
b7829000-b782a000 r–p 000ea000 08:08 2369389 /usr/lib/libX11.so.6.2.0
b782a000-b782c000 rw-p 000eb000 08:08 2369389 /usr/lib/libX11.so.6.2.0
b782c000-b782d000 rw-p b782c000 00:00 0
b782d000-b7842000 r-xp 00000000 08:08 2369061 /usr/lib/libICE.so.6.3.0
b7842000-b7843000 rw-p 00014000 08:08 2369061 /usr/lib/libICE.so.6.3.0
b7843000-b7845000 rw-p b7843000 00:00 0
b7845000-b784c000 r-xp 00000000 08:08 4210716 /usr/lib/libSM.so.6.0.0
b784c000-b784d000 r–p 00006000 08:08 4210716 /usr/lib/libSM.so.6.0.0
b784d000-b784e000 rw-p 00007000 08:08 4210716 /usr/lib/libSM.so.6.0.0
b784e000-b7876000 r-xp 00000000 08:08 16459 /lib/libpcre.so.3.12.1
b7876000-b7877000 r–p 00027000 08:08 16459 /lib/libpcre.so.3.12.1
b7877000-b7878000 rw-p 00028000 08:08 16459 /lib/libpcre.so.3.12.1
b7878000-b7879000 rw-p b7878000 00:00 0
b7879000-b7891000 r-xp 00000000 08:08 2369963 /usr/lib/libaa.so.1.0.4
b7891000-b7892000 r–p 00018000 08:08 2369963 /usr/lib/libaa.so.1.0.4
b7892000-b7893000 rw-p 00019000 08:08 2369963 /usr/lib/libaa.so.1.0.4
b7893000-b7895000 rw-p b78930Cancelado

Thanks in advance

Sorry, I noticed that the line that is really causing the problem is
arraypixels[i + 2] = 255.
But just sometimes. And always for the same images.

2009/2/3 Germ?n Diago <@German_Diago_Gomez>:

Hello, I’m trying to use this code to load a bmp, modify the data and save it:

   SDL_Surface * s = SDL_LoadBMP(fichorigen.c_str());
   if (superficie)
   {
           int w = superficie->w, h = superficie->h;

           Uint8 * arraypixels = static_cast<Uint8 *>(s->pixels);
           std::cout << "Imagen " << fichorigen << " ancho -> " << w << " alto

-> " << h << std::endl;
int j = 0;

           for (std::size_t i = 0; i < (w * h * 4); i += 4)
           {
                   if (colorkeyb != arraypixels[i] || colorkeyg != arraypixels[i + 1]

|| colorkeyr != arraypixels[i + 2])
{

                               ///THIS IS WHAT CAUSES TROUBLE>                                arraypixels[i + 2] = 255;
                           arraypixels[i + 1] = 0;
                           arraypixels[i] = 0;
                           j += 1;
                   }
           }
           const char * nombrefichmascara = (pathdestino + "mascara_" +

bf::basename(fichorigen) + bf::extension(fichorigen)).c_str();

           SDL_SaveBMP(superficie, nombrefichmascara);
           SDL_FreeSurface(superficie);
   }

Did you check the surface format? It may be that your source image is 3
bytes per pixel and not 4 bytes per pixel. This would be the case if your
image doesn’t have an alpha channel.

  • Ken Rogoway> ----- Original Message -----

From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of Germ?n Diago
Sent: Tuesday, February 03, 2009 5:02 PM
To: sdl at lists.libsdl.org
Subject: Re: [SDL] Why SDL_SaveBMP crashes my code?

Sorry, I noticed that the line that is really causing the problem is
arraypixels[i + 2] = 255.
But just sometimes. And always for the same images.

2009/2/3 Germ?n Diago :

Hello, I’m trying to use this code to load a bmp, modify the data and save
it:

   SDL_Surface * s = SDL_LoadBMP(fichorigen.c_str());
   if (superficie)
   {
           int w = superficie->w, h = superficie->h;

           Uint8 * arraypixels = static_cast<Uint8 *>(s->pixels);
           std::cout << "Imagen " << fichorigen << " ancho -> " << w

<< " alto

-> " << h << std::endl;
int j = 0;

           for (std::size_t i = 0; i < (w * h * 4); i += 4)
           {
                   if (colorkeyb != arraypixels[i] || colorkeyg !=

arraypixels[i + 1]

|| colorkeyr != arraypixels[i + 2])
{

                               ///THIS IS WHAT CAUSES TROUBLE
                           arraypixels[i + 2] = 255;
                           arraypixels[i + 1] = 0;
                           arraypixels[i] = 0;
                           j += 1;
                   }
           }
           const char * nombrefichmascara = (pathdestino + "mascara_"

bf::basename(fichorigen) + bf::extension(fichorigen)).c_str();

           SDL_SaveBMP(superficie, nombrefichmascara);
           SDL_FreeSurface(superficie);
   }

SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 8.0.233 / Virus Database: 270.10.17/1933 - Release Date: 02/03/09
17:48:00

It was that. There are some that have 4 bytes per pixel and
some that have 3 bytes per pixel. But now I have another problem.
There are some (very few) images, that
when I pass the algorithm through it, they look strange. It looks like
I’m not modifying the
pixels I should, because it doesn’t get red the area I want to make
red. They have vertical lines in between and
in the border they have some pixels with different colors, and some of
them remain white.
I checked for the offset of the pixels and it’s correct. Maybe the
data for color is not interleaved? I don’t know. Or could it be
row-aligned? Thanks in advance.

2009/2/4 Ken Rogoway :> Did you check the surface format? It may be that your source image is 3

bytes per pixel and not 4 bytes per pixel. This would be the case if your
image doesn’t have an alpha channel.

  • Ken Rogoway

-----Original Message-----
From: sdl-bounces at lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of Germ?n Diago
Sent: Tuesday, February 03, 2009 5:02 PM
To: sdl at lists.libsdl.org
Subject: Re: [SDL] Why SDL_SaveBMP crashes my code?

Sorry, I noticed that the line that is really causing the problem is
arraypixels[i + 2] = 255.
But just sometimes. And always for the same images.

2009/2/3 Germ?n Diago <@German_Diago_Gomez>:

Hello, I’m trying to use this code to load a bmp, modify the data and save
it:

   SDL_Surface * s = SDL_LoadBMP(fichorigen.c_str());
   if (superficie)
   {
           int w = superficie->w, h = superficie->h;

           Uint8 * arraypixels = static_cast<Uint8 *>(s->pixels);
           std::cout << "Imagen " << fichorigen << " ancho -> " << w

<< " alto

-> " << h << std::endl;
int j = 0;

           for (std::size_t i = 0; i < (w * h * 4); i += 4)
           {
                   if (colorkeyb != arraypixels[i] || colorkeyg !=

arraypixels[i + 1]

|| colorkeyr != arraypixels[i + 2])
{

                              ///THIS IS WHAT CAUSES TROUBLE
                           arraypixels[i + 2] = 255;
                           arraypixels[i + 1] = 0;
                           arraypixels[i] = 0;
                           j += 1;
                   }
           }
           const char * nombrefichmascara = (pathdestino + "mascara_"

bf::basename(fichorigen) + bf::extension(fichorigen)).c_str();

           SDL_SaveBMP(superficie, nombrefichmascara);
           SDL_FreeSurface(superficie);
   }

SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 8.0.233 / Virus Database: 270.10.17/1933 - Release Date: 02/03/09
17:48:00


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Color data is definitely interleaved. SDL does not support anything else.

Provide some source code and/or some PNG examples and maybe we can
help you diagnose what you’re doing wrong.

Are you aware of the ‘pitch’ member variable of SDL_Surface?On Wed, Feb 4, 2009 at 5:41 AM, Germ?n Diago wrote:

It was that. There are some that have 4 bytes per pixel and
some that have 3 bytes per pixel. But now I have another problem.
There are some (very few) images, that
when I pass the algorithm through it, they look strange. It looks like
I’m not modifying the
pixels I should, because it doesn’t get red the area I want to make
red. They have vertical lines in between and
in the border they have some pixels with different colors, and some of
them remain white.
I checked for the offset of the pixels and it’s correct. Maybe the
data for color is not interleaved? I don’t know. Or could it be
row-aligned? Thanks in advance.


http://codebad.com/

Behalf Of Donny Viszneki

It was that. There are some that have 4 bytes per pixel and
some that have 3 bytes per pixel. But now I have another problem.
There are some (very few) images, that
when I pass the algorithm through it, they look strange. It looks like
I’m not modifying the
pixels I should, because it doesn’t get red the area I want to make
red. They have vertical lines in between and
in the border they have some pixels with different colors, and some of
them remain white.
I checked for the offset of the pixels and it’s correct. Maybe the
data for color is not interleaved? I don’t know. Or could it be
row-aligned? Thanks in advance.

Color data is definitely interleaved. SDL does not support anything
else.

Provide some source code and/or some PNG examples and maybe we can
help you diagnose what you’re doing wrong.

Are you aware of the ‘pitch’ member variable of SDL_Surface?

The garbage pixels at the end of each line are definitely due to your not
using the “pitch” of the image.

I suspect the color issues with regard to red are because of differences in
your source and dest pixel format. There are a number of different 16 bit
pixel formats (565, 555, etc.)

As Donny suggests, if you can provide a small sample app with one of the PNG
files that is problematic we can help you figure out what is going on.

  • Ken Rogoway

----- Original Message -----
From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Sent: Wednesday, February 04, 2009 10:32 AM
To: A list for developers using the SDL library. (includes SDL-announce)
Subject: Re: [SDL] Why SDL_SaveBMP crashes my code?
On Wed, Feb 4, 2009 at 5:41 AM, Germ?n Diago wrote:

The error is the following one:

*** glibc detected *** ./maskframe free(): invalid next size (normal):

I assume you did use valgrind? Make sure your binary is compiled with
-g (debug info, and it’d help if your SDL library was also compiled
with this option, but it’s not required).

Also, both libX11 and things like libslang in the same program? That’s
rather exotic and intriguing (but shouldn’t cause crashes)! :-)On Tue, Feb 3, 2009 at 5:45 PM, Germ?n Diago wrote:


http://pphaneuf.livejournal.com/

good eye! what is he working on?!On Wed, Feb 4, 2009 at 4:16 PM, Pierre Phaneuf wrote:

Also, both libX11 and things like libslang in the same program? That’s
rather exotic and intriguing (but shouldn’t cause crashes)! :slight_smile:


http://codebad.com/