maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   [resolved] C programming - need help with malloc and free (https://talk.maemo.org/showthread.php?t=57942)

dwaradzyn 2010-07-06 21:43

[resolved] C programming - need help with malloc and free
 
I use the following code to convert 32bit RGBA bitmap to 16bit and upload it to graphic hardware:

Code:

int i, j;
GLubyte* pixels8888 = surfaceRGBA->pixels;
GLushort * pixels4444 =
    malloc(sizeof(GLushort) * surfaceRGBA -> w * surfaceRGBA -> h);

for (i = 0; i < surfaceRGBA -> w * surfaceRGBA -> h; i++) {
    j = i << 2;
    pixels4444[i] = (pixels8888[j+3] >> 4)
                    + ((pixels8888[j+2] >> 4) << 4)
                    + ((pixels8888[j+1] >> 4) << 8)
                    + ((pixels8888[j] >> 4) << 12);
}

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surfaceRGBA->w, surfaceRGBA->h, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, pixels4444);
//free(pixels4444);

The conversion totally works. I have a problem with last line. When it is uncommented my app crashes with following error:

Code:

*** glibc detected *** /opt/cloudgps/cloudgps: free(): invalid next size (normal): 0x00052788 ***
What is wrong with this free() call? How should I correctly free memory allocated for conversion (pixels4444)?

Thanks in advance.

Resolution:
The error in the code snippet above is that pixels4444 should not be freed right after calling glTexImage2D because pixels4444 can be used by OpenGL later. According to this:
Quote:

OpenGL loads textures in a lazy fashion - that is, the actual loading of the texture to be used doesn't occur until the texture is used. In this case it doesn't happen until the call to glDrawArrays that you've measured.

Joorin 2010-07-06 22:17

Re: C programming - need help with malloc and free
 
Just to help debugging:

Always check the return value of malloc calls.

Have you tried writing a program that does just this operation? If not, do so.

dwaradzyn 2010-07-07 07:59

Re: C programming - need help with malloc and free
 
You are right about checking return value - I should always do that. But in this particular issue malloc succeeds (if it would fail there will be immediate segfault because of NULL pointer). I will try to isolate this from whole program and investigate further. Anyway thanks for your input.

As the case is not closed yet more thoughts are welcome.

Rob1n 2010-07-07 08:18

Re: C programming - need help with malloc and free
 
Looking online, the usual cause of this error seems to be either using the variable after freeing it, or overrunning the malloced buffer when assigning to it.

Joorin 2010-07-07 08:20

Re: C programming - need help with malloc and free
 
My gut feeling in this case is that you've done a double free or something similar somewhere else so isolating this functionality is just the first step.

fnordianslip 2010-07-07 09:05

Re: C programming - need help with malloc and free
 
You could try using valgrind.

shocking 2010-07-07 11:33

Re: C programming - need help with malloc and free
 
Like Rob1n, I suspect that it's a buffer overrun, Valgrind might help in its detection as fnordianslip suggested. There's a possibility that the bounds check in the for loop might not work the way you think (compiler dependent I believe). It might be evaulating i < surfaceRGBA -> w and then multiplying that by surfaceRGBA -> h, which you could eliminate with i < (surfaceRGBA -> w * surfaceRGBA -> h), but I'm not sure that'd cause the problem.

juise- 2010-07-07 11:40

Re: C programming - need help with malloc and free
 
It seems that the buffer is given as parameter to glTexImage2D(). Check the documentation to see exactly what that function does to the buffer.

Does the free() work if you comment out the call to glTexImage2D()?

Does it work if you place it immediately after malloc()?

manu_dibango 2010-07-07 12:07

Re: C programming - need help with malloc and free
 
try his one: (comment out the function call glTexImage2D(...), uncomment the free(...)
Code:

int i, j;
GLubyte* pixels8888 = surfaceRGBA->pixels;
GLushort * pixels4444 =
    malloc(sizeof(GLushort) * surfaceRGBA -> w * surfaceRGBA -> h);

for (i = 0; i < surfaceRGBA -> w * surfaceRGBA -> h; i++) {
    j = i << 2;
    pixels4444[i] = (pixels8888[j+3] >> 4)
                    + ((pixels8888[j+2] >> 4) << 4)
                    + ((pixels8888[j+1] >> 4) << 8)
                    + ((pixels8888[j] >> 4) << 12);
}

// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surfaceRGBA->w, surfaceRGBA->h, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, pixels4444);

free(pixels4444);

if it works, you know where to search....

dwaradzyn 2010-07-07 14:48

Re: C programming - need help with malloc and free
 
Quote:

Originally Posted by juise- (Post 743584)
It seems that the buffer is given as parameter to glTexImage2D(). Check the documentation to see exactly what that function does to the buffer.

Does the free() work if you comment out the call to glTexImage2D()?

Does it work if you place it immediately after malloc()?

free() works when glTextImage2D is commented. Khronos API docs say nothing about memory management, but standard OpenGL docs say that I should free my pixels as they are copied to video memory after call to glTextImage2D.

Thanks everyone for your help.


All times are GMT. The time now is 07:22.

vBulletin® Version 3.8.8