CBS_ResTrack Class Reference

This class keep tracks on the allocated resources. More...

List of all members.

Public Types

enum  BS_RES_OUTPUT {
  BS_RES_OUTPUT_NONE = 0x00, BS_RES_OUTPUT_LOAD_LIBRARY = 0x01, BS_RES_OUTPUT_MEM_ALLOC = 0x02, BS_RES_OUTPUT_CRITICAL_SECTION = 0x04,
  BS_RES_OUTPUT_ALL = BS_RES_OUTPUT_MEM_ALLOC|BS_RES_OUTPUT_LOAD_LIBRARY|BS_RES_OUTPUT_CRITICAL_SECTION, BS_RES_OUTPUT_DEFAULT = BS_RES_OUTPUT_ALL
}
enum  BS_RES_TYPE { BS_RES_UNKNOWN = 0x00, BS_RES_LOAD_LIBRARY = 0x01, BS_RES_MEM_ALLOC = 0x02, BS_RES_CRITICAL_SECTION = 0x03 }

Public Member Functions

void Activate ()
DWORD AppendRef (LPVOID pPtr, LPCSTR lpFileName, int nLine, BOOL bDelete, LPCSTR lpComment="No Additional Comment")
 CBS_ResTrack (BOOL bActivate=FALSE)
void Check ()
void CheckNFree ()
DWORD CountRef (DWORD dwPtr, LPDWORD lpdwDeleted)
 DECLARE_OPERATORS (BS_RES_OUTPUT)
 DECLARE_SET_GET_MODE (BS_RES_OUTPUT, m_bsOutput, Output)
DWORD Find (DWORD dwFind)
void Free ()
DWORD GetMBTime ()
BOOL IsActive ()
DWORD Merge (CBS_ResTrack *pMerge)
DWORD Register (BS_RES_TYPE resType, LPVOID pPtr, int iSize, BOOL bEnableRefCount, LPCSTR lpFileName=NULL, int nLine=0, LPCSTR lpComment="No Additional Comment")
DWORD RemoveThreadReferances (DWORD dwPtr)
DWORD SetMBTime (DWORD dwNewTime)
BOOL SetOutputFile (LPCSTR lpcFileName)
DWORD UnRegister (LPVOID pPtr)
virtual ~CBS_ResTrack ()


Detailed Description

This class keep tracks on the allocated resources.

Author:
Dror Otmi
Date:
12.07.2003
Version:
V 1.0 Initial version CBS_ResTrack is design to keep track on:
Memory allocation / deallocation
Loading and freeing libraries
Entering and leaving critical sections
It is design to support also "multiple allocation" (e.g smart pointers or multiple critical section entring ) by counting each allocation and deallocation. The file name and line are also saved in the list. When the object is destructed or if the application invoked Check() explicitly, all objects found in the list are assumed to be "unfreed resources", all will be listed to the debug output window, if the debugger is active, and to a file called BS_ResTrack.txt in the directory of the running application. The user can control the behavior of the class by starting the application with the command line switch:
-BSRT-f MySettingFile.ini.
This command will cause the object to behave according to the values saved in that file.
Sample for a setting file:
#######################
[Bitspot Resources Track Info]
OutputType = 7
OutputFile = C:\Bitspot\ResTrackSample.txt
Timeout = 10000
ResTrack = 1
#######################
OutputType := a bit combination of the member BS_RES_OUTPUT (see enumeration member bellow).
Timeout := the message box's timeout, -1 = infinite and 0 means the message box will not be displayed at all.
OutputFile := the output file into which the failure will be written.
ResTrack := tells the object if the check should be enabled (1 = enable), this key is only considered in the release version.
Note:
Important: In the release version, the check is per default disabled. If a failure is detected the object will beep twice and skip the reporting. To enable the reporting in the release version, use the command line switch "-ResTrack=On" or -BSRT-f MySettingFile.ini as described above. -ALRT-f will override -ResTrack=On.
The following output shows that GSupport library was loaded and 2 references where added. Freeing, however, was not completed, one free call is missing.

****************************************
Bitspot's Resources Track found failure in the resources allocation!!! Date: Tuesday, September 28, 2004 12:24:22.936
****************************************



////////////////////////////////////////
Loading Library at:
c:\bitspot\MyApp\Sample.cpp(147): Loading GSupport lib.

Added (2) references
c:\bitspot\MyLib\SampleLib.cpp(548): Incrementing GSupport Library Reference Count.
c:\bitspot\MyLib\AnotherSampleLib.cpp(434): Incrementing GSupport Library Reference Count.

Deleted (2) references
c:\bitspot\MyLib\AnotherSampleLib.cpp(406): Decrementing GSupport Library Reference Count.
c:\bitspot\MyApp\Sample.cpp(214): Decrementing GSupport Library Reference Count.



****************************************
End of Bitspot's Resources Track Report.
****************************************


Member Enumeration Documentation

enum CBS_ResTrack::BS_RES_OUTPUT
 

A mask that will tell the object during the check if a detected failure type should be printed out or not.

Enumeration values:
BS_RES_OUTPUT_NONE  No output.
BS_RES_OUTPUT_LOAD_LIBRARY  Print out load library failures.
BS_RES_OUTPUT_MEM_ALLOC  Print out memory allocation failures.
BS_RES_OUTPUT_CRITICAL_SECTION  Print out Critical Section failures.
BS_RES_OUTPUT_ALL  Print out all the failure.
BS_RES_OUTPUT_DEFAULT  The default setting, equals BS_RES_OUTPUT_ALL.

enum CBS_ResTrack::BS_RES_TYPE
 

The type of resource, the type will be saved with the information when the resource is registered.

Enumeration values:
BS_RES_UNKNOWN  Unknown resource type
BS_RES_LOAD_LIBRARY  Loading a library
BS_RES_MEM_ALLOC  Allocating memory
BS_RES_CRITICAL_SECTION  A Critical Section


Constructor & Destructor Documentation

CBS_ResTrack::CBS_ResTrack BOOL  bActivate = FALSE  ) 
 

Constructor.

Parameters:
bActivate TRUE if the object should be activated, default = FALSE.

CBS_ResTrack::~CBS_ResTrack  )  [virtual]
 

Destructor. CheckNFree( ) is invoked to check if unfreed are still registered.

Returns:


Member Function Documentation

void CBS_ResTrack::Activate  ) 
 

Activates the object. The registration function will assert if the application will try to add a reference before this function is invoked either by instatiating the object with bActivate = TRUE or by an excplicit call.

Returns:
Nothing.

DWORD CBS_ResTrack::AppendRef LPVOID  pPtr,
LPCSTR  lpFileName,
int  nLine,
BOOL  bDelete,
LPCSTR  lpComment = "No Additional Comment"
 

Appneds an added or deleted reference of a registered resource.

Parameters:
pPtr A pointer to the registered resource.
lpFileName The file in which the reference is added/deleted.
nLine The line in lpFileName.
bDelete TRUE if the reference is deleted, FALSE if it is added.
lpComment An application defined comment.
Returns:
The index of the resource element in the list or -1 if a failure occurred.

void CBS_ResTrack::Check  ) 
 

Checks and report unfreed resources. In the debug version a detected unfreed resource will open a message box and ask the user if it wants a detailed report to be saved in a file (and debug output window, if the debugger is present). In the release version 2 beeps will be generated in case an unfreed resource is found. To activate the reporting in release version the application should start with the parameter "-ResTrack=On".

Returns:
Nothing.

void CBS_ResTrack::CheckNFree  ) 
 

Checks and free the resource list.

Returns:
Nothing.

DWORD CBS_ResTrack::CountRef DWORD  dwPtr,
LPDWORD  lpdwDeleted
 

Counts the number of references of a certain object saved in the resource list.

Parameters:
dwPtr A pointer of the resource to search.
lpdwDeleted the number of the deleted references, if this member is NULL, it will be ingnored.
Returns:
The number of referances added and deleted to the resource.

CBS_ResTrack::DECLARE_OPERATORS BS_RES_OUTPUT   ) 
 

Declares the operators for the enum member CBS_ResTrack::BS_RES_OUTPUT in order to avoid compiler error (or casting).

CBS_ResTrack::DECLARE_SET_GET_MODE BS_RES_OUTPUT  ,
m_bsOutput  ,
Output 
 

Declares the folowing members function for manipulating the enum member m_bsOutput:

CBS_ResTrack::BS_RES_OUTPUT GetOutputMode( )
Return the value saved in the member m_bsOutput.
BOOL IsOutputModeSet( CBS_ResTrack::BS_RES_OUTPUT check )
Return TRUE if the trace mode saved in check is on.
CBS_ResTrack::BS_RES_OUTPUT SetOutputMode( CBS_ResTrack::BS_RES_OUTPUT )
Sets a new resource track output mode, returns the old output mode.
CBS_ResTrack::BS_RES_OUTPUT AddOutputMode( CBS_ResTrack::BS_RES_OUTPUT )
Adds a new resource track output flag to the member m_bsOutput, returns the old output mode.
CBS_ResTrack::BS_RES_OUTPUT RemoveOutputMode( CBS_ResTrack::BS_RES_OUTPUT )
Removed a resource track output flag from the member m_bsOutput, returns the old output mode.
CBS_ResTrack::BS_RES_OUTPUT SetDefaultOutputMode()
Sets the default resource track output mode to the member m_bsOutput, returns the old output mode.

DWORD CBS_ResTrack::Find DWORD  dwFind  ) 
 

Finds the index of the node with the given resource address.

Parameters:
dwFind The resource address we search.
Returns:
The index the the resource with the address saved in dwFind or -1 if not found.

void CBS_ResTrack::Free  ) 
 

Frees the resource list and the resources allocated by the current object.

Returns:
Nothing.

DWORD CBS_ResTrack::GetMBTime  ) 
 

See return.

Returns:
The message box timeout in milliseconds.

BOOL CBS_ResTrack::IsActive  ) 
 

See return.

Returns:
TRUE if the object is active.

DWORD CBS_ResTrack::Merge CBS_ResTrack pMerge  ) 
 

Merges two Resource Track objects. This is usfull when an application uses more then one DLL that uses Resource Track (e.g. GSupport and BS_Crypto). Those DLL could Merge both Resource Track and use only one.

Parameters:
pMerge The resource trace to merge.
Returns:
The number of element popped out of pMerge.

DWORD CBS_ResTrack::Register BS_RES_TYPE  resType,
LPVOID  pPtr,
int  iSize,
BOOL  bEnableRefCount,
LPCSTR  lpFileName = NULL,
int  nLine = 0,
LPCSTR  lpComment = "No Additional Comment"
 

Registers a new resource in the track list.

Parameters:
resType The resource type;
pPtr The pointer of the resource (or any other unique identifier).
iSize The size of the allocated resource.
bEnableRefCount TRUE if reference count should be enabled.
lpFileName The file name in which the call was done.
nLine The line in lpFileName.
lpComment An application defined comment.
Returns:
The number of element saved in the resource track list.

DWORD CBS_ResTrack::RemoveThreadReferances DWORD  dwPtr  ) 
 

Clears references of an object that belongs to the current thread. This feature is used with the Critical Section objects when a thread gives up the ownership on it.

Parameters:
dwPtr A pointer to the reasource we search in the list;
Returns:
The number of elements removed or -1 if the resource was not found in the list.

DWORD CBS_ResTrack::SetMBTime DWORD  dwNewTime  ) 
 

Sets the message box timeout (-1 = infinite timeout).

Parameters:
dwNewTime The new timeout.
Returns:
The old timeout associated with the object.

BOOL CBS_ResTrack::SetOutputFile LPCSTR  lpcFileName  ) 
 

Sets an output file.

Parameters:
lpcFileName The output file name.
Returns:
TRUE if the file name is valid.

DWORD CBS_ResTrack::UnRegister LPVOID  pPtr  ) 
 

Removes a resource node from the list.

Parameters:
pPtr A pointer to the resource that should be removed.
Returns:
The number of element saved in the resources list or -1 if a failure occurred.
Bitspot AG


Bitspot AG
Rietlistrasse 1
CH-6345 Neuheim

Bitspot AG Logo

Phone:

+41 41 755 11 22

Fax:

+41 41 755 11 31

e-mail:

mail@bitspot.com

Web:

www.bitspot.com