Thread overview
question about the garbage collector
Mar 04, 2007
doob
Mar 04, 2007
Sean Kelly
Mar 04, 2007
doob
Mar 05, 2007
Sean Kelly
Mar 05, 2007
Daniel Keep
Mar 05, 2007
Saaa
Mar 05, 2007
Frits van Bommel
March 04, 2007
I have a small question. I've just started to use D and I've been looking at this example:

http://www.digitalmars.com/d/windows.html

I see in the example that it initializes the garbage collector using this function:
gc_init();

My question is then: is this just for D stuff or is it also for the C stuff?
March 04, 2007
doob wrote:
> I have a small question. I've just started to use D and I've been looking at this example:
> 
> http://www.digitalmars.com/d/windows.html
> 
> I see in the example that it initializes the garbage collector using this function:
> gc_init();
> 
> My question is then: is this just for D stuff or is it also for the C stuff?

You only need to do this if you're creating a D dynamic library, because in that case the default startup process is not executed and the GC is therefore not initialized.


Sean
March 04, 2007
"Sean Kelly" <sean@f4.ca> wrote in message news:esesnv$di1$1@digitalmars.com...

> You only need to do this if you're creating a D dynamic library, because in that case the default startup process is not executed and the GC is therefore not initialized.

You can also do it for regular Windows programs (i.e. in your WinMain), but in that case you can also just use the standard D "int/void main()" and use some Windows API functions to get the values which are usually passed as parameters to WinMain.  This way you don't have to do the D initialization crap; D will handle that for you.


March 04, 2007
Jarrett Billingsley Wrote:

> "Sean Kelly" <sean@f4.ca> wrote in message news:esesnv$di1$1@digitalmars.com...
> 
> > You only need to do this if you're creating a D dynamic library, because in that case the default startup process is not executed and the GC is therefore not initialized.
> 
> You can also do it for regular Windows programs (i.e. in your WinMain), but in that case you can also just use the standard D "int/void main()" and use some Windows API functions to get the values which are usually passed as parameters to WinMain.  This way you don't have to do the D initialization crap; D will handle that for you.
> 
> 

So you're saying that if I want to create a regular windows program I don't need to initialize the garbage collector because it will do that by default and it will handle the C stuff also?
March 05, 2007
doob wrote:
> Jarrett Billingsley Wrote:
> 
>> "Sean Kelly" <sean@f4.ca> wrote in message news:esesnv$di1$1@digitalmars.com...
>>
>>> You only need to do this if you're creating a D dynamic library, because in that case the default startup process is not executed and the GC is therefore not initialized.
>> You can also do it for regular Windows programs (i.e. in your WinMain), but in that case you can also just use the standard D "int/void main()" and use some Windows API functions to get the values which are usually passed as parameters to WinMain.  This way you don't have to do the D initialization crap; D will handle that for you. 
> 
> So you're saying that if I want to create a regular windows program I don't need to initialize the garbage collector because it will do that by default and it will handle the C stuff also? 

If you want to write a normal D program for Windows then you don't need to do anything special.  What do you mean by C stuff?
March 05, 2007

doob wrote:
> Jarrett Billingsley Wrote:
> 
>> "Sean Kelly" <sean@f4.ca> wrote in message news:esesnv$di1$1@digitalmars.com...
>>
>>> You only need to do this if you're creating a D dynamic library, because in that case the default startup process is not executed and the GC is therefore not initialized.
>> You can also do it for regular Windows programs (i.e. in your WinMain), but in that case you can also just use the standard D "int/void main()" and use some Windows API functions to get the values which are usually passed as parameters to WinMain.  This way you don't have to do the D initialization crap; D will handle that for you.
>>
>>
> 
> So you're saying that if I want to create a regular windows program I don't need to initialize the garbage collector because it will do that by default and it will handle the C stuff also?

D's garbage collector only applies to things that are allocated via the 'new' keyword in D code*.  C typically uses malloc, which the D garbage collector can't even see, much less mess with.  If you use malloc and free from D code, then what you allocate using malloc won't be garbage collected, either.

As for initialising it, I think this is what they're saying:

+) If you write your own WinMain function, you need to handle initialising and shutting down the D runtime stuff like the GC, unit tests, module ctors, etc.

+) If your program begins with a main function, on the other hand, you don't need to worry about doing the setup manually, since D will have handled it for you.  You just need to figure out a way to get the information that's usually passed to WinMain in some other manner.

Hope this helps.

	-- Daniel

* Of course, this doesn't apply if you happen to override a particular class' new and delete methods, but you hopefully won't be doing *that* unless you already know how the GC works :P

-- 
Unlike Knuth, I have neither proven or tried the above; it may not even make sense.

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/
March 05, 2007
And dynamic arrays, right?

(rant below)

I had some really strange problems with the gc.
In my main loop I had to read in .png files quite alot (using SDL_Image) and
place them on a SDL_Surface.
I had this problem of sometimes iso the png a black screen would appear.
(always the same place(changing the actual picture didn't change a thing))

I traced the problem down to this:
array.length=1800000;

Without this line (the array wasn't even in use) there were no black
screens.
My best solution still > first line in main > disable(); > no problems and a
big array :D

I'll report my bug when I'm sure it is (making the program minimal somehow
:)
Somehow setting the array length enables the gc and somehow the gc redirects
the pointer, I think... more info later :)


>
> D's garbage collector only applies to things that are allocated via the 'new' keyword in D code*.  C typically uses malloc, which the D garbage collector can't even see, much less mess with.  If you use malloc and free from D code, then what you allocate using malloc won't be garbage collected, either.
>
> As for initialising it, I think this is what they're saying:
>
> +) If you write your own WinMain function, you need to handle initialising and shutting down the D runtime stuff like the GC, unit tests, module ctors, etc.
>
> +) If your program begins with a main function, on the other hand, you don't need to worry about doing the setup manually, since D will have handled it for you.  You just need to figure out a way to get the information that's usually passed to WinMain in some other manner.
>
> Hope this helps.
>
> -- Daniel
>
> * Of course, this doesn't apply if you happen to override a particular class' new and delete methods, but you hopefully won't be doing *that* unless you already know how the GC works :P
>
> -- 
> Unlike Knuth, I have neither proven or tried the above; it may not even make sense.
>
> v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/


March 05, 2007
Saaa wrote:
>> D's garbage collector only applies to things that are allocated via the
>> 'new' keyword in D code*.  C typically uses malloc, which the D garbage
>> collector can't even see, much less mess with.  If you use malloc and
>> free from D code, then what you allocate using malloc won't be garbage
>> collected, either.
> 
> And dynamic arrays, right?

Yes, he forgot to mention using operators '~' or '~=' on (dynamic) arrays, and setting their .length to a larger value.

> (rant below)
> 
> I had some really strange problems with the gc.
> In my main loop I had to read in .png files quite alot (using SDL_Image) and place them on a SDL_Surface.
> I had this problem of sometimes iso the png a black screen would appear.
> (always the same place(changing the actual picture didn't change a thing))
> 
> I traced the problem down to this:
> array.length=1800000;
> 
> Without this line (the array wasn't even in use) there were no black screens.

You need to be careful when passing references to GC-allocated data to non-D libraries (like SDL). (setting an array length to a larger value makes it GC-allocated even if it wasn't previously)
If references are (only) stored where the GC can't see them (e.g. in malloc()ed memory), the data will be deleted on the next GC run. This may be what's happening here...

> My best solution still > first line in main > disable(); > no problems and a big array :D
> 
> I'll report my bug when I'm sure it is (making the program minimal somehow :)
> Somehow setting the array length enables the gc and somehow the gc redirects the pointer, I think... more info later :)

The current GC doesn't move objects. It just deletes them if it can't find any references to them.