Re: How to get Interface reference counting in ATL

Tech-Archive recommends: Fix windows errors by optimizing your registry




"Jaime Stuardo" <JaimeStuardo@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:8E08CA9A-3A5F-4B4D-91A7-DB42DC6E68BA@xxxxxxxxxxxxxxxx
Hi Brian,

I have replaced all pointers by smart ones using your preference, but
nothing works now :-( strange things started to happen, unexplainable
crashes, processes don't dissappear when I destroy the instance of the
component, and so on.

Since I don't know when objects are finally released, I think that some
objects aren't released when it should be, or they are released when they
shouldn't be.

Smart pointers are there to assist the programmer. There is nothing magical
about what they do; they are completely deterministic. The main advantage is
that they will call Release automatically when they go out of scope. This is
good news, since if the variable has gone out of scope you clearly can't use
the underlying interface pointer any more, and COM rules dictate that
Release() should be called.

Most programmers find there may be a little "trickiness" when assigning
interfaces pointers to smart pointers, but if there is any uncertaintly,
simply single-step right into the constructor or assignment operator of the
smart pointer and you can see exactly what is going on.

As a final note, no one is forcing you to use smart pointers. It is my
personal preference but not necessarily yours. Using raw interface pointers
is perfectly fine. And more importantly, don't expect smart pointers to find
bugs in your program. You are going to have to do that yourself.



.



Relevant Pages

  • Re: Need design advice
    ... But smart pointers ... avoid copies as much as possible by passing objects ... reference counting, with or without automated COW. ...
    (comp.lang.cpp)
  • Re: pMedialControl->Stop() hangs indefinitely (sometimes)
    ... >>> the pointers they're holding, because COM doesn't like pointers being ... > So before CoUninitialize, do I release or not all my smart Com pointers? ... If your classes are well-designed your destructor will call the destructors ... on the smart pointers when the graph is deleted, ...
    (microsoft.public.win32.programmer.directx.video)
  • Re: IDispatch question ...
    ... In this case we would be talking about smart pointers instead of (plain) old interface pointers. ... I strongly advise you to use smart pointers instead of raw COM pointers, as they take care of a lot of things. ... If you like inside the CreateInstance method of the template _com_ptr_t, you'll see that this method is performing exactly the steps that are necessary to instantiate an object with the given class ID and retrieve the requested interface pointer. ...
    (microsoft.public.vc.language)
  • Re: simple delete question
    ... >>pointers in your code, so why did you consider using new? ... > was not aware that dynamic allocation had a high cost at all. ... point of smart pointers is that they are much less error prone. ...
    (comp.lang.cpp)
  • Global Interface Table Problem.
    ... I have a com dll which is basically a com wrapper over STL map. ... I store the GIT interface in the global variable. ... If the pointers are created in A and if I close A and record in B my GIT ... create GIT and other interface pointer in any thread apart from the primary ...
    (microsoft.public.vc.atl)