On Wednesday, 10 July 2013 at 17:18:09 UTC, JohnnyK wrote:
I hope you like the subject matter and I hope it is not too
simplistic or have been answered before.
Anyway I have a question about how the garbage collector
works in a very specific situation. When passing string type
to a function in a shared library or DLL and assigning it to a
variable of type string inside the function and returning the
internal string. Such as this.
export string mytest(string tstStr)
{
string st = tstStr;
/* abbreviated to protect the innocent but other operations
such as concatenating and deleting may be done to st
before the return
*/
return st;
}
Is the string type a pointer or is it something else? In the
line where tstStr is assigned to st does it copy the address in
tstStr to st or does it copy the value in tstStr? I am just a
bit confused about string types since I come from a C
background and C has no type like this. Also what is returned
by this function? Does this function return a pointer or the
contents of an array? If I do export this what does it do to
the Garbage Collection? Does the Garbage Collection collect
tstStr or st? Also notice the comment in the function.
Others have answered about how strings and other arrays work
(also, see http://dlang.org/arrays.html and
http://dlang.org/d-array-article.html), so I'll address the
garbage collection question. Shared libraries don't make a
difference here AFAIK so we won't worry about them.
The GC will only collect something that there are no live
references to. If an array gets reallocated (by e.g. appending to
it beyond it's capacity) and there's no reference anywhere to the
old data, then it can be collected (no guarantee it will be).