xerpi / libvita2d

Simple and Fast (using the GPU) 2D library for the PSVita
MIT License
137 stars 52 forks source link

vita2d_free_texture crash when texture loaded with vita2d_load_PNG_file #28

Open Cpasjuste opened 7 years ago

Cpasjuste commented 7 years ago

Hi,

it seems there's a bug when we try to free a texture loaded with vita2d_load_PNG_file, the device crash.

Here's an example :

vita2d_texture *tex = vita2d_load_PNG_file("app0:data/test.png");

...draw...

vita2d_free_texture(tex); -> crash

xerpi commented 7 years ago

That's because the GPU is still using the texture while you are freeing it.

Cpasjuste commented 7 years ago

Hum hum ok, so we have to use "vita2d_fini" before freeing them. It's not really convenient !

xerpi commented 7 years ago

After you free them, do you pretend to continue drawing other things or just exit the application?

joel16 commented 7 years ago

Yeah I noticed this happened to me as well. I agree with @Cpasjuste, calling vit2d_fini before freeing isn't really convenient. Is there any way to overcome this? And in my case, yes my program keeps drawing new textures @xerpi

TheOfficialFloW commented 7 years ago

Wait vblank should do it.

Dak0r commented 7 years ago

@Cpasjuste and @joel16 You don't need to call vita2d_fini, you just shouldn't free your texture while you are drawing.

You should have a look at START_DRAWING() and END_DTAWING in @TheOfficialFloW s VitaShell: https://github.com/henkaku/VitaShell/blob/master/main.h

Then you can take a look at photo.c, which uses free_texture in a way that works, without calling vita2d_fini: https://github.com/henkaku/VitaShell/blob/master/photo.c

You will notice it can be important where you call it:

.... while(1){ // you can free your textures here START_DRAWING(); // you can't free your textures here, if you also draw them here END_DRAWING(); // you can free your textures here } vita2d_fini(); ....

When you have issues, try looking at how it's done in working examples like VitaShell

xerpi commented 7 years ago

@Cpasjuste if you want to continue using vita2d after freeing your images, you should call vita2d_wait_rendering_done(); before freeing them, this will block until the GPU has finished rendering.

noword commented 4 months ago

I encountered the same issue in my own project and found that the problem lies within the function _vita2d_create_empty_texture_format_advanced in vita2d_texture.c.

After vita2d_texture *texture = malloc(sizeof(*texture));, there was no memset performed, causing texture->gxm_rtgt to be non-zero.

This resulted in an error when vita2d_free_texture was called.