DOC: SQL Server 2000 Books Online OLE Automation Example May Leak Memory (320130)



The information in this article applies to:

  • Microsoft SQL Server 2000 (all editions)

This article was previously published under Q320130
BUG #: 356957 (SHILOH_BUGS)

SUMMARY

The OLE Automation Sample Script in Microsoft SQL Server 2000 Books Online is coded so that any error causes the SQL Server Distributed Management Objects (DMO) object to be orphaned in memory after it has been created. Use of the OLE Automation Sample Script code as a template may result in code that is likely to leak memory, even when a minor error occurs with getting or setting a property or calling a method on a COM object.

With the OLE Automation Sample Script, the COM objects are created correctly. However, any error the code experiences may potentially leave the COM object in memory and cause memory leaks because the script does not call the sp_OADestroy stored procedure for the COM object.

STATUS

Microsoft has confirmed that this is a problem in SQL Server 2000 Books Online.

MORE INFORMATION

Here is a revised sample that alleviates the potential memory leak:
DECLARE @object int

DECLARE @hr int

DECLARE @property varchar(255)

DECLARE @return varchar(255)

DECLARE @src varchar(255), @desc varchar(255)

 

-- Create an object.

EXEC @hr = sp_OACreate 'SQLDMO.SQLServer', @object OUT

IF @hr <> 0

BEGIN

   EXEC sp_OAGetErrorInfo @object, @src OUT, @desc OUT 

   SELECT hr=convert(varbinary(4),@hr), Source=@src, Description=@desc

END

 

-- Set a property.

EXEC @hr = sp_OASetProperty @object, 'HostName', 'Gizmo'

IF @hr <> 0

BEGIN

   EXEC sp_OAGetErrorInfo @object, @src OUT, @desc OUT 

   SELECT hr=convert(varbinary(4),@hr), Source=@src, Description=@desc

   GOTO Destructor

END

 

-- Get a property by using an output parameter.

EXEC @hr = sp_OAGetProperty @object, 'HostName', @property OUT

IF @hr <> 0

BEGIN

   EXEC sp_OAGetErrorInfo @object, @src OUT, @desc OUT 

   SELECT hr=convert(varbinary(4),@hr), Source=@src, Description=@desc

   GOTO Destructor

END

PRINT @property

 

-- Get a property by using a result set.

EXEC @hr = sp_OAGetProperty @object, 'HostName'

IF @hr <> 0

BEGIN

   EXEC sp_OAGetErrorInfo @object, @src OUT, @desc OUT 

   SELECT hr=convert(varbinary(4),@hr), Source=@src, Description=@desc

   GOTO Destructor

END

 

-- Get a property by calling the method.

EXEC @hr = sp_OAMethod @object, 'HostName', @property OUT

IF @hr <> 0

BEGIN

   EXEC sp_OAGetErrorInfo @object, @src OUT, @desc OUT 

   SELECT hr=convert(varbinary(4),@hr), Source=@src, Description=@desc

   GOTO Destructor

END

PRINT @property

 

-- Call a method.

EXEC @hr = sp_OAMethod @object, 'Connect', NULL, 'my_server', 'my_login', 'my_password'

IF @hr <> 0

BEGIN

   EXEC sp_OAGetErrorInfo @object, @src OUT, @desc OUT 

   SELECT hr=convert(varbinary(4),@hr), Source=@src, Description=@desc

   GOTO Destructor

END

 

-- Call a method that returns a value.

EXEC @hr = sp_OAMethod @object, 'VerifyConnection', @return OUT

IF @hr <> 0

BEGIN

   EXEC sp_OAGetErrorInfo @object, @src OUT, @desc OUT 

   SELECT hr=convert(varbinary(4),@hr), Source=@src, Description=@desc

   GOTO Destructor

END

PRINT @return

 

-- Destroy the object.

Destructor:

EXEC @hr = sp_OADestroy @object

IF @hr <> 0

BEGIN

   EXEC sp_OAGetErrorInfo @object, @src OUT, @desc OUT 

   SELECT hr=convert(varbinary(4),@hr), Source=@src, Description=@desc

   RETURN

END
				

Modification Type:MajorLast Reviewed:10/3/2003
Keywords:kbbug kbpending KB320130