How to use SDL functions like SDL_DestroyTexture() properly?How do you set, clear, and toggle a single bit?How do I iterate over the words of a string?Dynamic texture loading in SDLWhy do we need virtual functions in C++?Tile Map Usage Much CPU With OpenGL and SDLRelative path for files in c++ app using xcodeSDL_Surface Transparency IssuesHow to blit Score on screen in SDL?Trouble converting from SDL 1.2 to 2.0, Don't know how to initialize surface properlySDL why create textures on the heap instead of stack

What is Gilligan's full Name?

Do you have to have figures when playing D&D?

How far would a landing Airbus A380 go until it stops with no brakes?

What is the reason for setting flaps 1 on the ground at high temperatures?

Housemarks (superimposed & combined letters, heraldry)

Oil draining out shortly after turbo hose detached/broke

How was the airlock installed on the Space Shuttle mid deck?

Most valuable information/technology for rebuilding after the apocalypse?

Is there a DSLR/mirorless camera with minimal options like a classic, simple SLR?

What would be the way to say "just saying" in German? (Not the literal translation)

Piano: good exercises for runs and navigation

What is the Leave No Trace way to dispose of coffee grounds?

Is Dumbledore a human lie detector?

Generate certain list from two lists

Why is long-term living in Almost-Earth causing severe health problems?

Should I refuse to be named as co-author of a low quality paper?

Cathode rays and the cathode rays tube

Seasonality after 1st differencing

How can powerful telekinesis avoid violating Newton's 3rd Law?

Part of my house is inexplicably gone

How do you play "tenth" chords on the guitar?

Remove border lines of SRTM tiles rendered as hillshade

Make Gimbap cutter

What STL algorithm can determine if exactly one item in a container satisfies a predicate?



How to use SDL functions like SDL_DestroyTexture() properly?


How do you set, clear, and toggle a single bit?How do I iterate over the words of a string?Dynamic texture loading in SDLWhy do we need virtual functions in C++?Tile Map Usage Much CPU With OpenGL and SDLRelative path for files in c++ app using xcodeSDL_Surface Transparency IssuesHow to blit Score on screen in SDL?Trouble converting from SDL 1.2 to 2.0, Don't know how to initialize surface properlySDL why create textures on the heap instead of stack






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am doing this lazyfoo SDL tutorial right now.



The render class they using has some image to texture function which is using SDL_Surface* and SDL_Texture.
At the end of the function they "free" the created surface by calling SDL_FreeSurface().



Now, I am wondering:



  1. Why exactly i have to free the Surface at all (variables are local?)?

  2. Why its ok to let the created Texture be without calling SDL_DestroyTexture?

  3. What exacty does it mean when i destroy a texture or free a surface?

 bool Tile::loadTexture(const char* path) 
SDL_Texture* newTexture = NULL;
SDL_Surface* loadedSurface = IMG_Load(path);
//...some code
Texture = newTexture;
SDL_FreeSurface(loadedSurface);
return Texture != NULL;









share|improve this question
























  • Are you familiar with fopen/fclose? The situation with those functions is similar.

    – HolyBlackCat
    Mar 24 at 21:21

















0















I am doing this lazyfoo SDL tutorial right now.



The render class they using has some image to texture function which is using SDL_Surface* and SDL_Texture.
At the end of the function they "free" the created surface by calling SDL_FreeSurface().



Now, I am wondering:



  1. Why exactly i have to free the Surface at all (variables are local?)?

  2. Why its ok to let the created Texture be without calling SDL_DestroyTexture?

  3. What exacty does it mean when i destroy a texture or free a surface?

 bool Tile::loadTexture(const char* path) 
SDL_Texture* newTexture = NULL;
SDL_Surface* loadedSurface = IMG_Load(path);
//...some code
Texture = newTexture;
SDL_FreeSurface(loadedSurface);
return Texture != NULL;









share|improve this question
























  • Are you familiar with fopen/fclose? The situation with those functions is similar.

    – HolyBlackCat
    Mar 24 at 21:21













0












0








0








I am doing this lazyfoo SDL tutorial right now.



The render class they using has some image to texture function which is using SDL_Surface* and SDL_Texture.
At the end of the function they "free" the created surface by calling SDL_FreeSurface().



Now, I am wondering:



  1. Why exactly i have to free the Surface at all (variables are local?)?

  2. Why its ok to let the created Texture be without calling SDL_DestroyTexture?

  3. What exacty does it mean when i destroy a texture or free a surface?

 bool Tile::loadTexture(const char* path) 
SDL_Texture* newTexture = NULL;
SDL_Surface* loadedSurface = IMG_Load(path);
//...some code
Texture = newTexture;
SDL_FreeSurface(loadedSurface);
return Texture != NULL;









share|improve this question
















I am doing this lazyfoo SDL tutorial right now.



The render class they using has some image to texture function which is using SDL_Surface* and SDL_Texture.
At the end of the function they "free" the created surface by calling SDL_FreeSurface().



Now, I am wondering:



  1. Why exactly i have to free the Surface at all (variables are local?)?

  2. Why its ok to let the created Texture be without calling SDL_DestroyTexture?

  3. What exacty does it mean when i destroy a texture or free a surface?

 bool Tile::loadTexture(const char* path) 
SDL_Texture* newTexture = NULL;
SDL_Surface* loadedSurface = IMG_Load(path);
//...some code
Texture = newTexture;
SDL_FreeSurface(loadedSurface);
return Texture != NULL;






c++ sdl






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 24 at 22:17









Quentin

47.8k694152




47.8k694152










asked Mar 24 at 21:18









KingKoopaKingKoopa

42




42












  • Are you familiar with fopen/fclose? The situation with those functions is similar.

    – HolyBlackCat
    Mar 24 at 21:21

















  • Are you familiar with fopen/fclose? The situation with those functions is similar.

    – HolyBlackCat
    Mar 24 at 21:21
















Are you familiar with fopen/fclose? The situation with those functions is similar.

– HolyBlackCat
Mar 24 at 21:21





Are you familiar with fopen/fclose? The situation with those functions is similar.

– HolyBlackCat
Mar 24 at 21:21












1 Answer
1






active

oldest

votes


















1















Why exactly i have to free the Surface at all (variables are local?)?




The pointer loadedSurface is local. The actual surface isn't: there is something similar to malloc inside of IMG_Load. The same way you use free on memory allocated with malloc, you use SDL_FreeSurface on surfaces allocated with IMG_Load (or SDL_CreateRGBSurface and so on).




Why its ok to let the created Texture be without calling SDL_DestroyTexture?




SDL_DestroyTexture is called, inside of LTexture::free, which is called by the destructor of LTexture. So SDL_DestroyTexture is pretty much guaranteed to be called at some point if loadFromFile was called.




What exacty does it mean when i destroy a texture or free a surface?




It means the same thing as using free on memory allocated with malloc, or using delete on memory allocated with new, or calling std::unique_ptr::reset (without argument), and so on. Each variant does something sligthly different. If you want to know what exactly differs between SDL_DestroyTexture, SDL_FreeSurface, free, etc, you can look at the source code: SDL is open source and there are quite a few open source implementations of free out there.






share|improve this answer























  • the free funktion uses SDL_DestroyTexture for the one texture "mTexture" declared inside of the class but not for the texture declared inside of the loadFromFile function "newTexture". i will take a other look on the memory allocation stuff. thank yo so far

    – KingKoopa
    Mar 26 at 18:37












  • The second last instruction in loadFromFile, mTexture = newTexture;, makes mTexture hold the texture before newTexture goes out of scope. It's still the same texture though, only its address is being copied. I actually don't know why the author uses a local variable here instead of directly using mTexture. It's functionally the same, but would be less confusing.

    – Nelfeal
    Mar 27 at 14:53












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55328678%2fhow-to-use-sdl-functions-like-sdl-destroytexture-properly%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1















Why exactly i have to free the Surface at all (variables are local?)?




The pointer loadedSurface is local. The actual surface isn't: there is something similar to malloc inside of IMG_Load. The same way you use free on memory allocated with malloc, you use SDL_FreeSurface on surfaces allocated with IMG_Load (or SDL_CreateRGBSurface and so on).




Why its ok to let the created Texture be without calling SDL_DestroyTexture?




SDL_DestroyTexture is called, inside of LTexture::free, which is called by the destructor of LTexture. So SDL_DestroyTexture is pretty much guaranteed to be called at some point if loadFromFile was called.




What exacty does it mean when i destroy a texture or free a surface?




It means the same thing as using free on memory allocated with malloc, or using delete on memory allocated with new, or calling std::unique_ptr::reset (without argument), and so on. Each variant does something sligthly different. If you want to know what exactly differs between SDL_DestroyTexture, SDL_FreeSurface, free, etc, you can look at the source code: SDL is open source and there are quite a few open source implementations of free out there.






share|improve this answer























  • the free funktion uses SDL_DestroyTexture for the one texture "mTexture" declared inside of the class but not for the texture declared inside of the loadFromFile function "newTexture". i will take a other look on the memory allocation stuff. thank yo so far

    – KingKoopa
    Mar 26 at 18:37












  • The second last instruction in loadFromFile, mTexture = newTexture;, makes mTexture hold the texture before newTexture goes out of scope. It's still the same texture though, only its address is being copied. I actually don't know why the author uses a local variable here instead of directly using mTexture. It's functionally the same, but would be less confusing.

    – Nelfeal
    Mar 27 at 14:53
















1















Why exactly i have to free the Surface at all (variables are local?)?




The pointer loadedSurface is local. The actual surface isn't: there is something similar to malloc inside of IMG_Load. The same way you use free on memory allocated with malloc, you use SDL_FreeSurface on surfaces allocated with IMG_Load (or SDL_CreateRGBSurface and so on).




Why its ok to let the created Texture be without calling SDL_DestroyTexture?




SDL_DestroyTexture is called, inside of LTexture::free, which is called by the destructor of LTexture. So SDL_DestroyTexture is pretty much guaranteed to be called at some point if loadFromFile was called.




What exacty does it mean when i destroy a texture or free a surface?




It means the same thing as using free on memory allocated with malloc, or using delete on memory allocated with new, or calling std::unique_ptr::reset (without argument), and so on. Each variant does something sligthly different. If you want to know what exactly differs between SDL_DestroyTexture, SDL_FreeSurface, free, etc, you can look at the source code: SDL is open source and there are quite a few open source implementations of free out there.






share|improve this answer























  • the free funktion uses SDL_DestroyTexture for the one texture "mTexture" declared inside of the class but not for the texture declared inside of the loadFromFile function "newTexture". i will take a other look on the memory allocation stuff. thank yo so far

    – KingKoopa
    Mar 26 at 18:37












  • The second last instruction in loadFromFile, mTexture = newTexture;, makes mTexture hold the texture before newTexture goes out of scope. It's still the same texture though, only its address is being copied. I actually don't know why the author uses a local variable here instead of directly using mTexture. It's functionally the same, but would be less confusing.

    – Nelfeal
    Mar 27 at 14:53














1












1








1








Why exactly i have to free the Surface at all (variables are local?)?




The pointer loadedSurface is local. The actual surface isn't: there is something similar to malloc inside of IMG_Load. The same way you use free on memory allocated with malloc, you use SDL_FreeSurface on surfaces allocated with IMG_Load (or SDL_CreateRGBSurface and so on).




Why its ok to let the created Texture be without calling SDL_DestroyTexture?




SDL_DestroyTexture is called, inside of LTexture::free, which is called by the destructor of LTexture. So SDL_DestroyTexture is pretty much guaranteed to be called at some point if loadFromFile was called.




What exacty does it mean when i destroy a texture or free a surface?




It means the same thing as using free on memory allocated with malloc, or using delete on memory allocated with new, or calling std::unique_ptr::reset (without argument), and so on. Each variant does something sligthly different. If you want to know what exactly differs between SDL_DestroyTexture, SDL_FreeSurface, free, etc, you can look at the source code: SDL is open source and there are quite a few open source implementations of free out there.






share|improve this answer














Why exactly i have to free the Surface at all (variables are local?)?




The pointer loadedSurface is local. The actual surface isn't: there is something similar to malloc inside of IMG_Load. The same way you use free on memory allocated with malloc, you use SDL_FreeSurface on surfaces allocated with IMG_Load (or SDL_CreateRGBSurface and so on).




Why its ok to let the created Texture be without calling SDL_DestroyTexture?




SDL_DestroyTexture is called, inside of LTexture::free, which is called by the destructor of LTexture. So SDL_DestroyTexture is pretty much guaranteed to be called at some point if loadFromFile was called.




What exacty does it mean when i destroy a texture or free a surface?




It means the same thing as using free on memory allocated with malloc, or using delete on memory allocated with new, or calling std::unique_ptr::reset (without argument), and so on. Each variant does something sligthly different. If you want to know what exactly differs between SDL_DestroyTexture, SDL_FreeSurface, free, etc, you can look at the source code: SDL is open source and there are quite a few open source implementations of free out there.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 26 at 17:41









NelfealNelfeal

5,2921826




5,2921826












  • the free funktion uses SDL_DestroyTexture for the one texture "mTexture" declared inside of the class but not for the texture declared inside of the loadFromFile function "newTexture". i will take a other look on the memory allocation stuff. thank yo so far

    – KingKoopa
    Mar 26 at 18:37












  • The second last instruction in loadFromFile, mTexture = newTexture;, makes mTexture hold the texture before newTexture goes out of scope. It's still the same texture though, only its address is being copied. I actually don't know why the author uses a local variable here instead of directly using mTexture. It's functionally the same, but would be less confusing.

    – Nelfeal
    Mar 27 at 14:53


















  • the free funktion uses SDL_DestroyTexture for the one texture "mTexture" declared inside of the class but not for the texture declared inside of the loadFromFile function "newTexture". i will take a other look on the memory allocation stuff. thank yo so far

    – KingKoopa
    Mar 26 at 18:37












  • The second last instruction in loadFromFile, mTexture = newTexture;, makes mTexture hold the texture before newTexture goes out of scope. It's still the same texture though, only its address is being copied. I actually don't know why the author uses a local variable here instead of directly using mTexture. It's functionally the same, but would be less confusing.

    – Nelfeal
    Mar 27 at 14:53

















the free funktion uses SDL_DestroyTexture for the one texture "mTexture" declared inside of the class but not for the texture declared inside of the loadFromFile function "newTexture". i will take a other look on the memory allocation stuff. thank yo so far

– KingKoopa
Mar 26 at 18:37






the free funktion uses SDL_DestroyTexture for the one texture "mTexture" declared inside of the class but not for the texture declared inside of the loadFromFile function "newTexture". i will take a other look on the memory allocation stuff. thank yo so far

– KingKoopa
Mar 26 at 18:37














The second last instruction in loadFromFile, mTexture = newTexture;, makes mTexture hold the texture before newTexture goes out of scope. It's still the same texture though, only its address is being copied. I actually don't know why the author uses a local variable here instead of directly using mTexture. It's functionally the same, but would be less confusing.

– Nelfeal
Mar 27 at 14:53






The second last instruction in loadFromFile, mTexture = newTexture;, makes mTexture hold the texture before newTexture goes out of scope. It's still the same texture though, only its address is being copied. I actually don't know why the author uses a local variable here instead of directly using mTexture. It's functionally the same, but would be less confusing.

– Nelfeal
Mar 27 at 14:53




















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55328678%2fhow-to-use-sdl-functions-like-sdl-destroytexture-properly%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript