Re: various objects in my VB6 project - Calling IUnknown

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Mark G. Meyers (mmeyers[at]hydromilling.com)
Date: 06/30/04


Date: Wed, 30 Jun 2004 16:48:53 -0400


"Peter Young" <youngpa@comcast.no.net.spam.please> wrote in message
news:Od$SzktXEHA.3112@tk2msftngp13.phx.gbl...
> "Mark G. Meyers" <mmeyers[at]hydromilling.com> wrote in message
news:eddEfbtXEHA.1144@TK2MSFTNGP10.phx.gbl...
> Well, even a pointer takes 4 bytes. Are you saying the C++ compiler is
generating the cleanup code for you? My
> experience is limited to C, so I apparently I'm not giving C++ its due. In
C, memory allocation and deallocation is half
> the battle. VB just about eliminates that for you (as do most modern
languages).
>

The pointer as a local variable takes 4 bytes on the stack, which is gone
when the function exits.

In past jobs, much of my life (C++ troubleshooting) has been devoted to
fixing other people's memory leaks. No doubts about it. I might have even
had one or two of my own :-). First off, I can see how VB makes the memory
issue less-so is by re-asigning the same reference to something new...

dim o as Object
set o = New Object
set o = New Object ' deallocates the first

Doing something like this with a pointer would be a permanent leak in C++.
Also, as far as using the stack instead of the heap is a matter of
programming practices. I could have coded Foo another way...

Variant Foo()
{
    Collection * c; ' just a pointer, no object creation here
    Object * pObj; ' just another pointer
    c = New Collection; ' object allocated on heap
    pObj = c; ' no allocation here, just another pointer
} ' exits with c still allocated, and nobody has a pointer to it
      ' this would be fixed by adding: delete c; or delete pObj; at the end

Another thing I have become accustomed to in C++ is memory checking code or
tools. I've seen a couple of in-house memory management libraries, each of
which had the responsibility of overriding New and Delete (or equivalent),
and keeping track of allocations and deletions, and providing a report of
leaks at the end of execution. Actually, Visual C++ has heap checking built
right into it as well. Of course, the leak still has to be located. Now
this goes back a bit, but I recall OS/2 having a unique memory tool - a
thing called Theseus. It would show blocks of memory allocated in a
process - and which module did the allocation! (some complications can
arise) But I was able to find a leak in another companies DLL that I didn't
have the source for, in a process that my DLLs were running in. I think I
can see a way to do this for Windows processes, but maybe someone already
has. It would involve creating mapping DLL(s) by the same name as Windows
DLL(s), renaming the windows DLL(s), passing calls through to them,
gathering info from the stack, etc, and recording where the allocations are
coming from. Could be very cool? It could produce which module, the code
address, how many bytes allocated, and a trace log.

I think the other major advantage with VB is handling all of it's intrinsic
objects internally. The C++ equivalent might be having to manually
create/destroy objects that otherwise just aren't there. The VB environment
has alot under the hood.

It's an interesting comparison, now that we're going into this much detail.
It occurs to me that VB, with automatically shellable class Init/Term
methods is simplified, serialization-like, and helps. Like more natural to
code properly.

Also, in your example, when your reference pops off the stack, the object it
references (heap) is also destroyed. VB doesn't use pointers, which have
been regarded as one of the foremost tools for blowing your leg off. In my
two examples, one puts the object on the stack, and the other on the heap,
so the programmer has more of an ability to create leaks and other problems
by using pointers. I see the relative confusion-creating power here. In my
first example, pObj does not coorespond to a heap allocation. In my second
example, c does refer to a heap allocation. Each is a pointer. It is not
so straightforward as to which pointers do and do not have to be deleted.

A bit of typing and thinking, but I think there it is! I'll have to
consider a C++ convention (perhaps naming?) for making what needs to be
deleted more distinct. ;-)

Or, in VB or C++, just delete freakin everything.

The Java garbage collector is frequently misunderstood, misconfigured, and
underrated, but it's come a long way. Java apps freeze at times from
full-sweep garbage collections when the settings aren't as they should be.
Also it's profiler re: speed. Ok, I digress, but I have to admit, some of
the Java complaints, such as freeze-ups or performance, have been mitigated
pretty well. This is a garbage collector in the truest sense of the word.
I guess I'd have to say it is references in general that stand out as an
improvement over pointers.

- Mark



Relevant Pages

  • Re: Whats the difference between the heap and the freestore?
    ... >> In general, when discussing dynamically allocated memory, I hear people ... As of a couple of days ago, I though the heap ... > When you use the term heap in the context of memory allocation it annoys ... > without saying that they have to be allocated on a stack or anything else. ...
    (comp.lang.cpp)
  • Memory problems - WinDbg and SOS: Who recognizes this pattern?
    ... Last night I managed to get a memory dump using ADPlus and I analyzed it ... ephemeral segment allocation context: none ... Large object heap starts at 0x0a0d0030 ...
    (microsoft.public.dotnet.framework.performance)
  • Re: Tip Required on Dynamic Memory Allocation in Server Applications ...
    ... a memory allocation failure after 300 usages of the string ... Suggestions where given to pre-reserve the size of the heap, ... My program works untill 200-300 requests are ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Competing criteria
    ... solution to "not heap allocation" is to define a reasonable maximum count ... memory allocation will not fail due to memory fragmentation, ... If you have some memory left over, you might want to switch to making a heap request when your preallocated buffers are exhausted, in this case since you are now allowed to fail, you are ok. ... To the degree that you can't preallocate as efficiently as you think you might be able to use the heap, consider that the cost for getting the guarantee. ...
    (comp.lang.c)
  • Re: C++ Garbage Collector on VMS?
    ... case of a reference counting system, have a non-zero reference count) as ... to the beginning of the heap, updating the pointers to the objects as it ... (segregating allocation areas by type/size can help a lot there, ... any way with GC-controlled memory). ...
    (comp.os.vms)