object scope and memory

object scope and memory

Post by Kenneth Macpherso » Tue, 11 Jan 2000 04:00:00



Hello,

If a variable or object is declared in a sub or function, when the sub or
function terminates, the memory allocated to said variable/object will be
released on the stack of the thread that called the sub/function.

Why then, is it pushed in VB groups to set objects to nothing before exiting
subs/functions?

Obviously performing object cleanup such as closing recordsets is important
to free up db resources etc. and this should also be done before terminating
the app.

However, surely there is no need to set local objects to nothing explicitly?

Global objects may need managed in this way to conserve app wide memory
resources but I do not see the need for this to be done at sub/function
level.

Can anyone explain the need for setting objects to nothing before exiting
subs/functions?

Thanks,

Kenneth

 
 
 

object scope and memory

Post by Bob Butle » Tue, 11 Jan 2000 04:00:00




Quote:> Hello,

> If a variable or object is declared in a sub or function, when the
sub or
> function terminates, the memory allocated to said variable/object
will be
> released on the stack of the thread that called the sub/function.

> Why then, is it pushed in VB groups to set objects to nothing before
exiting
> subs/functions?

It confirms that you expect the object to be released and that you did
not just misunderstand scope issues which also makes it easier to go
back to the app or to somebody else's app later for maintenance.

It does no harm if VB's automatic release is working and resolves the
problem if in any version of VB the objects are not properly released.

Quote:> Obviously performing object cleanup such as closing recordsets is
important
> to free up db resources etc. and this should also be done before
terminating
> the app.

Why do you think closing recordsets when a sub ends is obviously
important but setting the object=Nothing isn't?  Any well-behaved
object will close all alloctaed resources when it gets terminated and
it gets temrinated when it goes out of scope so neither action is
technically needed... unless something isn't "well-behaved" in which
case doing your own cleanup on all points reduces the chance of
problems.

Quote:> However, surely there is no need to set local objects to nothing

explicitly?

Need?  probably not.  Good programming practice? Absolutely.

Quote:> Global objects may need managed in this way to conserve app wide
memory
> resources but I do not see the need for this to be done at
sub/function
> level.

All objects should be managed the same way for consistency, if nothing
else.

Quote:> Can anyone explain the need for setting objects to nothing before
exiting
> subs/functions?

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

object scope and memory

Post by Kenneth Macpherso » Tue, 11 Jan 2000 04:00:00


Quote:> It confirms that you expect the object to be released and that you did
> not just misunderstand scope issues which also makes it easier to go
> back to the app or to somebody else's app later for maintenance.

> It does no harm if VB's automatic release is working and resolves the
> problem if in any version of VB the objects are not properly released.

Stack space allocation/deallocation is rather fundamental and if not working
correctly then the app will not be long for this world.

Quote:> Why do you think closing recordsets when a sub ends is obviously
> important but setting the object=Nothing isn't?  Any well-behaved
> object will close all alloctaed resources when it gets terminated and
> it gets temrinated when it goes out of scope so neither action is
> technically needed... unless something isn't "well-behaved" in which
> case doing your own cleanup on all points reduces the chance of
> problems.

Fair point.

Here is another question - I know there is a VB run time dll (forgotten its
name) so I take it when you compile your VB app, calls are combined that
call into the run-time dll?

What does this run-time dll do?

Why not make the VB .exe stand-alone with the normal calls into the WIN32
API?

Cheers,

Kenneth

 
 
 

1. object scope and memory

Hello,

If a variable or object is declared in a sub or function, when the sub or
function terminates, the memory allocated to said variable/object will be
released on the stack of the thread that called the sub/function.

Why then, is it pushed in VB groups to set objects to nothing before exiting
subs/functions?

Obviously performing object cleanup such as closing recordsets is important
to free up db resources etc. and this should also be done before terminating
the app.

However, surely there is no need to set local objects to nothing explicitly?

Global objects may need managed in this way to conserve app wide memory
resources but I do not see the need for this to be done at sub/function
level.

Can anyone explain the need for setting objects to nothing before exiting
subs/functions?

Thanks,

Kenneth

2. SQL log backup

3. ADO as application scope object in ASP

4. SQL server load tool

5. Scope of database object

6. Help! PdoxWin says table has password and Borland will not help

7. VB5 and Workspace object: BeginTrans and Rollback in different scopes

8. Referential Integrity ???

9. Scope of database object in VB6

10. VFP: createobject() during runtime and object scope

11. Connection object scope/lifetime

12. Connection Object going out of Scope

13. How to close a connection when object is out of scope