# Does SDL\_RWops make an internal copy of memory?

**URL:** <https://discourse.libsdl.org/t/does-sdl-rwops-make-an-internal-copy-of-memory/15490>\
**Category:** SDL Development\
**Created:** [July 20, 2008, 8:01pm UTC](https://discourse.libsdl.org/t/does-sdl-rwops-make-an-internal-copy-of-memory/15490 "2008-07-20T20:01:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luke\_J\_Crook](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/luke_j_crook/32/166_2.png) [@Luke\_J\_Crook](https://discourse.libsdl.org/u/Luke_J_Crook)\
**Post date:** [July 20, 2008, 8:01pm UTC](https://discourse.libsdl.org/t/does-sdl-rwops-make-an-internal-copy-of-memory/15490/1 "2008-07-20T20:01:53Z")

</div>

In the function SDL\_RWFromMem(void \*mem, int size)

Will SDL\_RWFromMem make a copy of ‘mem’, or is the array referenced from the  
SDL\_RWops object and cannot therefore be freed?

Thanks,

- Luke

---

<div class="post-metadata">

**Author:** ![Peter\_Mackay](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/p/b77776/32.png) [@Peter\_Mackay](https://discourse.libsdl.org/u/Peter_Mackay)\
**Post date:** [July 20, 2008, 9:32pm UTC](https://discourse.libsdl.org/t/does-sdl-rwops-make-an-internal-copy-of-memory/15490/2 "2008-07-20T21:32:57Z")

</div>

Hello,

It’s referenced, so you’ll need to keep it allocated until you call  
SDL\_RWclose (or the loading function does).

On Windows at least, it wouldn’t be safe for the SDL DLL to free  
memory allocated outside of it, as they can have different runtime  
libraries (and associated memory managers).

It’s the same sort of deal with FILE\*s too if I remember correctly. I  
seem to recall getting bitten by that one time in a non-SDL project.

Hope this helps,

Peter

2008/7/20 Luke Crook :\>

> In the function SDL\_RWFromMem(void \*mem, int size)
> 
> Will SDL\_RWFromMem make a copy of ‘mem’, or is the array referenced from the  
> SDL\_RWops object and cannot therefore be freed?
> 
> Thanks,
> 
> - Luke
> 
> * * *
> 
> SDL mailing list  
> SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)

---

<div class="post-metadata">

**Author:** ![Luke\_J\_Crook](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/luke_j_crook/32/166_2.png) [@Luke\_J\_Crook](https://discourse.libsdl.org/u/Luke_J_Crook)\
**Post date:** [July 21, 2008, 5:25pm UTC](https://discourse.libsdl.org/t/does-sdl-rwops-make-an-internal-copy-of-memory/15490/3 "2008-07-21T17:25:09Z")

</div>

Peter Mackay \<mackay.pete+sdl [gmail.com](http://gmail.com)\> writes:

> Hello,
> 
> It’s referenced, so you’ll need to keep it allocated until you call  
> SDL\_RWclose (or the loading function does).
> 
> On Windows at least, it wouldn’t be safe for the SDL DLL to free  
> memory allocated outside of it, as they can have different runtime  
> libraries (and associated memory managers).
> 
> It’s the same sort of deal with FILE\*s too if I remember correctly. I  
> seem to recall getting bitten by that one time in a non-SDL project.

Thanks. This confirms the behaviour I am seeing in my code.

- Luke
