|
|
Re: Autosizing string
[Re: Uhrwerk]
#413383
12/12/12 23:34
12/12/12 23:34
|
Joined: Nov 2012
Posts: 62 Istanbul
Talemon
Junior Member
|
Junior Member
Joined: Nov 2012
Posts: 62
Istanbul
|
So how should they be defined locally? Not at all. A STRING* is an engine object. Allocation is costly. You should use global or static strings whereever possible. If you have good reason to create a local string with str_create don't forget to free them afterwards, or you'll create a memory leak. Well when you define them globally, they will still be allocated. Also, if you don't free them it won't technically be a memory leak since the engine frees them on exit. It would be memory bloat though in the course of the game's runtime.
|
|
|
Re: Autosizing string
[Re: Talemon]
#413386
12/13/12 02:19
12/13/12 02:19
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
When defining strings globally they are allocated exactly one time. When you create strings in functions with str_create they will be created as many times as the function is called.
I don't think it's of any interest if the engine frees them before exiting or if the memory gets freed when the process is being killed by the operating system. In fact it's memory that was allocated and still cannot be used. Also note that the number of engine objects is limited. So not freeing strings when creating them in a function is a rather bad idea, regardless if that counts technically as a memory leak or not.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: Autosizing string
[Re: Uhrwerk]
#413387
12/13/12 04:19
12/13/12 04:19
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
It technically is a memory leak since you lost all references to it. It doesn't matter if there are still references to it somewhere else, it becomes a memory leak as soon as you lose the last reference to it. Obviously you can have indirect references to an object (ie store it in an array and throw the pointer away but keep a reference to the array), but the engine doesn't count as indirect reference but as blackbox that you don't have a control over.
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Moderated by mk_1, Perro, rayp, Realspawn, Rei_Ayanami, rvL_eXile, Spirit, Superku, Tobias, TSG_Torsten, VeT
|
|
|