# \[SDL 3.x\] SDL\_CreateSurface and SDL\_DestroySurface = SIGSEV

**URL:** <https://discourse.libsdl.org/t/sdl-3-x-sdl-createsurface-and-sdl-destroysurface-sigsev/51608>\
**Category:** SDL Development\
**Created:** [May 29, 2024, 11:50am UTC](https://discourse.libsdl.org/t/sdl-3-x-sdl-createsurface-and-sdl-destroysurface-sigsev/51608 "2024-05-29T11:50:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mathias](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@Mathias](https://discourse.libsdl.org/u/Mathias)\
**Post date:** [May 29, 2024, 11:50am UTC](https://discourse.libsdl.org/t/sdl-3-x-sdl-createsurface-and-sdl-destroysurface-sigsev/51608/1 "2024-05-29T11:50:25Z")

</div>

Why does it crash when I try to unlock the Surface with `SDL Destroy Surface(...)`?

```auto
#include <SDL3/SDL.h>

int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);

    SDL_Surface *SrcSurface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA32);

    unsigned char pixels[] = {0xFF, 0x00, 0x00, 0x00};
    SrcSurface->pixels = &pixels;

    SDL_Log("Destroy SrcSurface");
    SDL_DestroySurface(SrcSurface); // Crash
    SDL_Log("Destroy SrcSurface");

    SDL_Quit();

    return 0;
}

```

---

<div class="post-metadata">

**Author:** ![yataro](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/y/6de8d8/32.png) [@yataro](https://discourse.libsdl.org/u/yataro)\
**Post date:** [May 29, 2024, 11:58am UTC](https://discourse.libsdl.org/t/sdl-3-x-sdl-createsurface-and-sdl-destroysurface-sigsev/51608/2 "2024-05-29T11:58:02Z")

</div>

> [@Mathias](#):
>
> `SrcSurface->pixels = &pixels;`

`SDL_DestroySurface` will try to free this pointer. You should copy memory with `memcpy`.

---

<div class="post-metadata">

**Author:** ![Mathias](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@Mathias](https://discourse.libsdl.org/u/Mathias)\
**Post date:** [May 29, 2024, 1:34pm UTC](https://discourse.libsdl.org/t/sdl-3-x-sdl-createsurface-and-sdl-destroysurface-sigsev/51608/3 "2024-05-29T13:34:34Z")

</div>

Thanks  
I even solved it more elegantly with `SDL_memcpy`.  
I suspect that I even had a memory leak with the direct assignment. The pixels that were created with `SDL_CreateSurface(...)` were then somewhere in the memory grave.

```auto
SDL_memcpy(SrcSurface->pixels, &pixels, sizeof(pixels));

```
