October 28, 2016
Hot Topics:

Small Memory Allocation, Part 3

  • April 15, 2004
  • By Radu Braniste
  • Send Email »
  • More Articles »

There are a couple of differences from a regular COM smart pointer (CComPtr, for example [6]):

  • BREW interfaces can be created by using a constructor taking UID as the only parameters. Alternative implementations, more in the spirit of CComPtr using dedicated methods or factories, are equally possible (and marginally better if considering the additional failure support).
  • BrewIPtr takes immediate ownership of raw interface pointers when used with BREW_Policy. This encourages the use of smart pointers and avoids all direct reference counting manipulation. COM_Policy allows the same behavior as a smart COM pointer (that's why the attach/detach pair).
  • BREW interfaces are used in the C style, making pointer operators overloading superfluous.
  • Taking into account the C implementation of BREW, there is no way to prohibit the use of AddRef or Release on a BrewIPtr. BREW_Policy makes interfaces access more uniform and doesn't encourage this kind of mistakes.

The use of BrewIPtr is illustrated in two contrasting examples, one using BREW_Policy and the other COM_Policy.

Writer writer;
   BrewIPtr<IDisplay> d(writer.getDisplay());
   BrewIPtr<IDisplay> f(AEECLSID_DISPLAY);
   f = d;
   BrewIPtr<IFileMgr> fmgr(AEECLSID_FILEMGR);
   const char fn[]="tmpf39";
   IFILEMGR_Remove(fmgr, fn);
   BrewIPtr<IFile> fl( IFILEMGR_OpenFile(fmgr, fn , _OFM_CREATE));
   if (fl)
      IFILE_Write(fl, fn, sizeof(fn));

The preceding code uses Brew_Policy. Here's the same code using COM_Policy:

Writer writer;
   BrewIPtr<IDisplay, COM_Policy> d(writer.getDisplay());
   BrewIPtr<IDisplay, COM_Policy> f(AEECLSID_DISPLAY);
   f = d;
   BrewIPtr<IFileMgr, COM_Policy> fmgr(AEECLSID_FILEMGR);
   const char fn[]="tmpfl9";
   IFILEMGR_Remove(fmgr, fn);
   BrewIPtr<IFile, COM_Policy> fl;
   fl.attach( IFILEMGR_OpenFile(fmgr, fn , _OFM_CREATE));
   if (fl)
      IFILE_Write(fl, fn, sizeof(fn));

Note how we have to attach the IFile interface in this case.

Class Writer takes a BrewIPtr<IDisplay> as a member:

class Writer
   BrewIPtr<IDisplay>& getDisplay()
      return m_pIDisplay;
............//other member functions
   BrewIPtr<IDisplay>    m_pIDisplay;    // display instance
                                         // instead of
                                         // IDisplay* m_pIDisplay;
  ..........//other members


[1] James Noble. Charles Weirr: Small Memory software - Patterns for systems with limited memory, Addison Wesley. 2001

[2] Maciej Sobczak. "STL Sequences & the View Concept," C/C++ Users Journal, April 2004.

[3] Richard Jones. Rafael Lins - Garbage Collection: Algorithms for Automatic Dynamic Memory Management. John Wiley & Sons, 1997

[4] Scott Meyers. More Effective C++. Addison Wesley, 1996

[5] Don Box. Essential COM. Addison Wesley, 1998

[6] Brent Rector, Chris Sells. ATL Internals. Addison Wesley, 1999.


Download the accompanying program here (14 Kb).

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel