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;
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:
- Why exactly i have to free the Surface at all (variables are local?)?
- Why its ok to let the created Texture be without calling
SDL_DestroyTexture
? - 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
add a comment |
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:
- Why exactly i have to free the Surface at all (variables are local?)?
- Why its ok to let the created Texture be without calling
SDL_DestroyTexture
? - 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
Are you familiar withfopen
/fclose
? The situation with those functions is similar.
– HolyBlackCat
Mar 24 at 21:21
add a comment |
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:
- Why exactly i have to free the Surface at all (variables are local?)?
- Why its ok to let the created Texture be without calling
SDL_DestroyTexture
? - 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
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:
- Why exactly i have to free the Surface at all (variables are local?)?
- Why its ok to let the created Texture be without calling
SDL_DestroyTexture
? - 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
c++ sdl
edited Mar 24 at 22:17
Quentin
47.8k694152
47.8k694152
asked Mar 24 at 21:18
KingKoopaKingKoopa
42
42
Are you familiar withfopen
/fclose
? The situation with those functions is similar.
– HolyBlackCat
Mar 24 at 21:21
add a comment |
Are you familiar withfopen
/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
add a comment |
1 Answer
1
active
oldest
votes
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.
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 inloadFromFile
,mTexture = newTexture;
, makesmTexture
hold the texture beforenewTexture
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 usingmTexture
. It's functionally the same, but would be less confusing.
– Nelfeal
Mar 27 at 14:53
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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 inloadFromFile
,mTexture = newTexture;
, makesmTexture
hold the texture beforenewTexture
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 usingmTexture
. It's functionally the same, but would be less confusing.
– Nelfeal
Mar 27 at 14:53
add a comment |
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.
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 inloadFromFile
,mTexture = newTexture;
, makesmTexture
hold the texture beforenewTexture
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 usingmTexture
. It's functionally the same, but would be less confusing.
– Nelfeal
Mar 27 at 14:53
add a comment |
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.
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.
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 inloadFromFile
,mTexture = newTexture;
, makesmTexture
hold the texture beforenewTexture
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 usingmTexture
. It's functionally the same, but would be less confusing.
– Nelfeal
Mar 27 at 14:53
add a comment |
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 inloadFromFile
,mTexture = newTexture;
, makesmTexture
hold the texture beforenewTexture
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 usingmTexture
. 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Are you familiar with
fopen
/fclose
? The situation with those functions is similar.– HolyBlackCat
Mar 24 at 21:21