Safe, High performance, reuseable

Wednesday, August 30, 2006

What's new in ATL7

This article doesn't cover new ATL7 Server Classes and Attributes. This is not a complete list - only what I have found so far.

Strings
Collections
Threading
Memory Management
Files
Security
Miscellaneous


Strings
ATL3:
String conversion macros had a few limitations. It was allocating memory from stack, with potential to overflow the stack with large strings. The string was freed when the function exited and not at any introduced scope inside the function. It was using outdated OLE2ANSI define. And if you look at the remarks for _alloca (which was used for the conversions) on MSDN, it says that it has limitations when used inside SEH or C++ EH.

For example look at the ATL3 String Conversion Macros:

USES_CONVERSION;
try
{
for(int i=0; i< str =" OLE2T(L" str =" OLE2T(L" i="0;" str =" CW2A(L" szr =" CW2AEX<2">(L"some string");

szr; // at this line szr should point to garbage
CString:
Another Addition is the CStringT class for manipulating strings. Even though this class exposes the same methods as previous MFC CString. It's not the old MFC CString. This class is templated. It has few typedefs to work with char, wchar_t, and TCHAR. CString is a typedef:

CStringT<> > CAtlString;
which is redefined based on MFC being used or not to:

// #ifdef _AFX
typedef CAtlString CString;
The CStringT itself is shared between MFC/ATL. By default it uses CRT, but that can be changed by defining either _ATL_MIN_CRT or _ATL_CSTRING_NO_CRT. Which in turn makes the appropriate typedef visible to the compiler, for example either:

// use CRT and have more functionality
CStringT<> >
or

// don't use CRT, but use win32API string functions, (wvsprintf, lstrlen, CharNext, etc)
CStringT<>, ChTraitsOS >
Another class CFixedStringT provides fixed pre-allocated contiguous buffer for optimized memory management.

The memory management for either one can be customized through implementation of IAtlStringMgr and that implementation already exists CAtlStringMgr. Its ctor takes a pointer to IAtlMemMgr, which can be any of predefined implementations or your own:

CCRTHeap - use CRT heap
CWin32Heap - use win32 heap
CComHeap - use COM Task memory allocator

etc. So for example:

No comments: