Re: Error Raising and Memory in VB (general question)
From: Vlad Kozin (VladKozin_at_hotmail.com)
Date: 02/14/04
- Next message: Bob Butler: "Re: VB 6.0"
- Previous message: Joe \: "Re: VB 6.0"
- In reply to: Mark Alexander Bertenshaw: "Re: Error Raising and Memory in VB (general question)"
- Next in thread: Joe \: "Re: Error Raising and Memory in VB (general question)"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 14 Feb 2004 00:32:13 GMT
Hi Mark,
"Mark Alexander Bertenshaw" <mark.bertenshaw@virgin.net> wrote in message
> > What I was told about VB6, it normally releases all allocated memory
> > on program exit only.
>
> If you have an object variable allocated on the stack, and it goes out of
> scope - for any reason, including errors - the object terminates and the
> memory is released. Likewise, any class-level variables that exist when
the
> object is terminated go out of scope, and the memory is also released.
What
> you have probably been told must refer to all module-level object
variables,
> and global references.
>
In general, I agree with you.
But why are you saying it uses stack? I know that was true for 16-bit apps
that if you call a function it's supposed to push the function parameters to
the stack, then call the function, etc...
I think we are dealing with heap memory here. A process in NT-family
environment is assigned a process-level heap. All BSTR type strings (read:
VB strings) are allocated on the process heap.
MSDN says:
"COM's IMalloc allocator (or CoTaskMemAlloc / CoTaskMemFree): Functions use
the default per-process heap. Automation uses the Component Object Model
(COM)'s allocator, and the requests use the per-process heap."
So I guess when we call COM or Automation (ActiveX) objects, we use the heap
memory.
When we do Set myobj = New clsObject, the OS allocates a part of the heap
space for the object instance.
I tried to search MSDN for the stack use in VB, and I didn't find any
mentioning of it...
> > So if your program is supposed to run or stay in memory for some
> > time, or it calls a function in a loop, make sure you correctly set
> > all object references to Nothing within your program, or function.
>
> You almost never have to explicitly *have to* set object variables to
> nothing. And you *never* have to do that with object variables allocated
on
> the stack, because the VB compiler automatically adds explicit Set =
Nothing
> at the end of procedures - as well as in Terminate events.
I believe you, but I would not state that, unless I am a MS developer who
actually designed or wrote the VB6 compiler (or, to be precise, MSVC++ 1.0
compiler)...
> > If your VB program calls any WinAPI functions (including those like
> > FindFirstFile, etc.) do not rely on the VB runtime to release memory
> > or to close the file handles for you. Do it yourself.
>
> I would completely agree with this. This is why the Terminate event is so
> damn useful.
>
Thank you :)
> --
> Mark Bertenshaw
> Kingston upon Thames
> UK
>
regards,
Vlad
- Next message: Bob Butler: "Re: VB 6.0"
- Previous message: Joe \: "Re: VB 6.0"
- In reply to: Mark Alexander Bertenshaw: "Re: Error Raising and Memory in VB (general question)"
- Next in thread: Joe \: "Re: Error Raising and Memory in VB (general question)"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|