digitalmars.D.learn - Scope statement
- Lars V (11/11) Feb 05 2008 Hi,
- Bill Baxter (8/22) Feb 05 2008 Yes you need to use scope if you want deterministic destruction.
- Lars V (2/27) Feb 05 2008 But, when the function ends the object should be collected by the GC (th...
- Simen Kjaeraas (17/49) Feb 05 2008 e
- Michel Fortin (14/17) Feb 05 2008 The garbage collector will generally destruct and deallocate the object
- Lars V (4/26) Feb 05 2008 OK, thanks for all the replies.
- Michel Fortin (11/14) Feb 05 2008 It'll call the destructor and free the memory, but I wouldn't say it's
Hi, I have a question: void foo() { Obj o = new Obj(); } In this code, the destructor of the object will be called ALWAYS at the end of the function? In D, the objects are not destroyed when they go out of their scopes??? Is necessary to use the "scope" statement? I really don't understand the necessity of the scope statement... could anybody explain it? Thanks, Regards
Feb 05 2008
Lars V wrote:Hi, I have a question: void foo() { Obj o = new Obj(); } In this code, the destructor of the object will be called ALWAYS at the end of the function? In D, the objects are not destroyed when they go out of their scopes??? Is necessary to use the "scope" statement? I really don't understand the necessity of the scope statement... could anybody explain it?Yes you need to use scope if you want deterministic destruction. Allocation and deallocation are expensive, and one of the nice things about a garbage collection system is that it lets you do clean-up more lazily. That way you can handle a bunch of de-allocations at once. Think of it as waiting a week to vacuum up the room instead of pulling out the vacuum cleaner every time a piece of dust hits the floor. --bb
Feb 05 2008
Bill Baxter Wrote:Lars V wrote:But, when the function ends the object should be collected by the GC (there are no more references to it) and the GC will call the destructor, no? I don't understand it.Hi, I have a question: void foo() { Obj o = new Obj(); } In this code, the destructor of the object will be called ALWAYS at the end of the function? In D, the objects are not destroyed when they go out of their scopes??? Is necessary to use the "scope" statement? I really don't understand the necessity of the scope statement... could anybody explain it?Yes you need to use scope if you want deterministic destruction. Allocation and deallocation are expensive, and one of the nice things about a garbage collection system is that it lets you do clean-up more lazily. That way you can handle a bunch of de-allocations at once. Think of it as waiting a week to vacuum up the room instead of pulling out the vacuum cleaner every time a piece of dust hits the floor. --bb
Feb 05 2008
Lars V <nospamplease nospam.com> wrote:Bill Baxter Wrote:=Lars V wrote:Hi, I have a question: void foo() { Obj o =3D new Obj(); } In this code, the destructor of the object will be called ALWAYS at=the end of the function?In D, the objects are not destroyed when they go out of their =scopes??? Is necessary to use the "scope" statement?I really don't understand the necessity of the scope statement... =could anybody explain it? Yes you need to use scope if you want deterministic destruction. Allocation and deallocation are expensive, and one of the nice things=eabout a garbage collection system is that it lets you do clean-up mor=glazily. That way you can handle a bunch of de-allocations at once. Think of it as waiting a week to vacuum up the room instead of pullin=out the vacuum cleaner every time a piece of dust hits the floor. --bbBut, when the function ends the object should be collected by the GC =(there are no more references to it) and the GC will call the =destructor, no? I don't understand it.When the object goes out of scope, there are no more pointers to it, = correct. This only means however, that the next time the GC runs a = collection cycle (which might be right after you've exited the scope, bu= t = you can't know that), the object's destructor will be called. If you instead use void foo() { scope Obj o =3D new Obj(); } You are guaranteed that the moment the object goes out of scope, its = destructor will be called. -Simen
Feb 05 2008
On 2008-02-05 18:26:50 -0500, Lars V <nospamplease nospam.com> said:But, when the function ends the object should be collected by the GC (there are no more references to it) and the GC will call the destructor, no? I don't understand it.The garbage collector will generally destruct and deallocate the object when you ask for more memory, if there are no more reference to it (it doesn't scan memory for pointers each time a function returns). This could happen the next time you call new, or in a few more calls to new. Basically, when to dispose of the object is left to the discretion of the garbage collector which may optimize things in various ways you shouldn't have to care much about. If you need deterministic destruction for your object, either use scope or call the destructor yourself (using delete). -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Feb 05 2008
Michel Fortin Wrote:On 2008-02-05 18:26:50 -0500, Lars V <nospamplease nospam.com> said:OK, thanks for all the replies. Only one more question; the deterministic destruction of the object -using scope or delete- will force the GC to collect it? or it'll only call the destructor?But, when the function ends the object should be collected by the GC (there are no more references to it) and the GC will call the destructor, no? I don't understand it.The garbage collector will generally destruct and deallocate the object when you ask for more memory, if there are no more reference to it (it doesn't scan memory for pointers each time a function returns). This could happen the next time you call new, or in a few more calls to new. Basically, when to dispose of the object is left to the discretion of the garbage collector which may optimize things in various ways you shouldn't have to care much about. If you need deterministic destruction for your object, either use scope or call the destructor yourself (using delete). -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Feb 05 2008
On 2008-02-05 20:00:38 -0500, Lars V <nospamplease nospam.com> said:Only one more question; the deterministic destruction of the object -using scope or delete- will force the GC to collect it? or it'll only call the destructor?It'll call the destructor and free the memory, but I wouldn't say it's the garbage collector that does it in this case. Note that when you use scope, the compiler may optimise things and allocate the object directly on the stack. In this case, the destructor is (implicitly) called but there is no memory that needs to be freed after the function call. -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Feb 05 2008