# Is IMG\_Load\_RW freeing buffer created with new\[\]?

**URL:** <https://discourse.libsdl.org/t/is-img-load-rw-freeing-buffer-created-with-new/21298>\
**Category:** SDL Development\
**Created:** [March 22, 2015, 8:58pm UTC](https://discourse.libsdl.org/t/is-img-load-rw-freeing-buffer-created-with-new/21298 "2015-03-22T20:58:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sanitowi](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/a183cd/32.png) [@sanitowi](https://discourse.libsdl.org/u/sanitowi)\
**Post date:** [March 22, 2015, 8:58pm UTC](https://discourse.libsdl.org/t/is-img-load-rw-freeing-buffer-created-with-new/21298/1 "2015-03-22T20:58:48Z")

</div>

Hi, in some code I load images from a zip file via Physfs.

This Method loads the image from the zip file into a RWops

Code:

SDL\_RWops\* GameEngine::LoadFromZip(std::string target)  
{  
// Archive is mounted in GameEngine::Init()

// Check if target file is in archive  
if(PHYSFS\_exists(target.c\_str()) == 0)  
{  
// handle error  
}

// Open target file  
PHYSFS\_file \*file = PHYSFS\_openRead(target.c\_str());  
if(file == nullptr)  
{  
// handle error  
}

// Read target into memory  
PHYSFS\_sint64 filesize = PHYSFS\_fileLength(file);  
char \*buffer = new char[filesize];  
if(PHYSFS\_read(file, buffer, sizeof(char), filesize) != filesize)  
{  
// handle error  
}  
PHYSFS\_close(file);

// Create RWops  
SDL\_RWops \*rwops = SDL\_RWFromMem(buffer, filesize);  
if(rwops == nullptr)  
{  
// handle error  
}

// delete[] buffer; // CRASH  
return rwops;  
}

and the returned RWops is used in some functions to load create s Surface, Texture ect…  
e.g.

Code:

SDL\_Surface\* GameEngine::LoadSurface(std::string filename)  
{  
// Load image from memory  
SDL\_RWops \*rwops = LoadFromZip(filename);  
SDL\_Surface \*surface = IMG\_Load\_RW(rwops, 1);  
if(surface == nullptr)  
{  
// handle error  
}

return surface;  
}

I’m not sure what happens with the buffer allocated in the LoadFromZip Function.  
Is it freed by

SDL\_Surface \*surface = IMG\_Load\_RW(rwops, 1);

?

The code works, but when I put a delete[] buffer just before the return of the LoadFromZip function,  
the programm crashes.

What’s the right way to do this, if I don’t want to write the code of the LoadFromZip funtion in every function I use for loading  
surfaces, textures, mix\_chunks ect. from the zip file.

P.S.

I didn’t post the error checkings in the code … 😉

---

<div class="post-metadata">

**Author:** ![JonnyD](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/jonnyd/32/1604_2.png) [@JonnyD](https://discourse.libsdl.org/u/JonnyD)\
**Post date:** [March 22, 2015, 11:04pm UTC](https://discourse.libsdl.org/t/is-img-load-rw-freeing-buffer-created-with-new/21298/2 "2015-03-22T23:04:16Z")

</div>

The rwops is only a handle for accessing the buffer data in a common way.  
It does not keep an extra copy of the buffer, so if you delete the buffer  
before the rwops, then you’re effectively pulling the rug out from under  
the rwops. You will have to manually delete the buffer when you are all  
done with the rwops.

There’s a PhysFS extension, physfsrwops, that you should use to avoid that  
extra buffer. That might help your use case here.

Jonny DOn Sunday, March 22, 2015, sanitowi \<michael.straube1 at [gmx.de](http://gmx.de)\> wrote:

> Hi, in some code I load images from a zip file via Physfs.
> 
> This Method loads the image from the zip file into a RWops
> 
> Code:
> 
> SDL\_RWops\* GameEngine::LoadFromZip(std::string target)  
> {  
> // Archive is mounted in GameEngine::Init()
> 
> // Check if target file is in archive  
> if(PHYSFS\_exists(target.c\_str()) == 0)  
> {  
> // handle error  
> }
> 
> // Open target file  
> PHYSFS\_file \*file = PHYSFS\_openRead(target.c\_str());  
> if(file == nullptr)  
> {  
> // handle error  
> }
> 
> // Read target into memory  
> PHYSFS\_sint64 filesize = PHYSFS\_fileLength(file);  
> char \*buffer = new char[filesize];  
> if(PHYSFS\_read(file, buffer, sizeof(char), filesize) != filesize)  
> {  
> // handle error  
> }  
> PHYSFS\_close(file);
> 
> // Create RWops  
> SDL\_RWops \*rwops = SDL\_RWFromMem(buffer, filesize);  
> if(rwops == nullptr)  
> {  
> // handle error  
> }
> 
> // delete buffer; // CRASH  
> return rwops;  
> }
> 
> and the returned RWops is used in some functions to load create s Surface,  
> Texture ect…  
> e.g.
> 
> Code:
> 
> SDL\_Surface\* GameEngine::LoadSurface(std::string filename)  
> {  
> // Load image from memory  
> SDL\_RWops \*rwops = LoadFromZip(filename);  
> SDL\_Surface \*surface = IMG\_Load\_RW(rwops, 1);  
> if(surface == nullptr)  
> {  
> // handle error  
> }
> 
> return surface;  
> }
> 
> I’m not sure what happens with the buffer allocated in the LoadFromZip  
> Function.  
> Is it freed by
> 
> SDL\_Surface \*surface = IMG\_Load\_RW(rwops, 1);
> 
> ?
> 
> The code works, but when I put a delete buffer just before the return of  
> the LoadFromZip function,  
> the programm crashes.
> 
> What’s the right way to do this, if I don’t want to write the code of the  
> LoadFromZip funtion in every function I use for loading  
> surfaces, textures, mix\_chunks ect. from the zip file.
> 
> P.S.
> 
> I didn’t post the error checkings in the code … 😉

---

<div class="post-metadata">

**Author:** ![sanitowi](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/a183cd/32.png) [@sanitowi](https://discourse.libsdl.org/u/sanitowi)\
**Post date:** [March 23, 2015, 9:43am UTC](https://discourse.libsdl.org/t/is-img-load-rw-freeing-buffer-created-with-new/21298/3 "2015-03-23T09:43:02Z")

</div>

Thanks for your hint.  
Are there some docs available for the physfsrwops extension?  
I have not found any yet…

For now I use a std::vector as buffer in the LoadFromZip function, return a copy of it and create the rwops  
in the calling function…

---

<div class="post-metadata">

**Author:** ![sanitowi](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/a183cd/32.png) [@sanitowi](https://discourse.libsdl.org/u/sanitowi)\
**Post date:** [March 23, 2015, 1:23pm UTC](https://discourse.libsdl.org/t/is-img-load-rw-freeing-buffer-created-with-new/21298/4 "2015-03-23T13:23:27Z")

</div>

Ok, I was able to get it working with physfsrwops,  
but there are two little problems.

I opend another thread for that.  
[https://forums.libsdl.org/viewtopic.php?p=47383#47383](https://forums.libsdl.org/viewtopic.php?p=47383#47383)
