Question about example using ArrayOfValues smart pointers

Matthew Jew mjew at icnc.com
Tue Oct 17 18:22:20 CDT 2006


Hello,

There is some sample code in VSDK_Examples that I have a question about.
The code uses a smart pointer to an ArrayOfValues.
If I understand smart pointers (and I know that I may NOT be  
understanding them correctly),
when a smart pointer goes out of scope, it does a Release() on the  
object it points to.
If the Release() causes the reference count to go to zero, the object  
is deallocated.

In the example, the procedure FillT1Table calls the function  
BuildArrayOfBinds, which is supposed
to return an ArrayOfValues_Ptr.

At the start, BuildArrayOfBinds declares a ArrayOfValues_Ptr called  
pBinds and assigns a newly created ArrayOfValues to it.
At this point, my understanding is that the Reference Count for the  
object would be 1.

At the conclusion of the function BuildArrayOfBinds, pBinds would  
then go out of scope,
sending a Release() to the ArrayOfValues allocated at the start (and  
assigned to pBinds).
This would decrement the Reference Count to 0, and deallocate the  
object.

Upon returning to FillT1Table, the location of the ArrayOfValues that  
the function created would be returned
and assigned to the smart pointer ArrayOfValues_Ptr pBinds1, but at  
that point, isn't the object pointed to by
this value already deallocated?

Wasn't there supposed to be an AddRef() somewhere in BuildArrayOfBinds,
and a corresponding Release() somewhere in FillT1Table?

Or is there something else going on that I don't understand?
What am I missing?

- Matthew

Here is the sample code (from Bind.cpp):

/ 
************************************************************************ 
**********************/
ArrayOfValues_Ptr BuildArrayOfBinds(
	VSQL::I_SqlDatabase_Ptr pSqlDB, const char* inStr, long inLong,  
float inFloat )
{
	// create bind array
	ArrayOfValues_Ptr pBinds = new ArrayOfValues();

	I_Database_Ptr pFblDb = pSqlDB->get_BaseDatabase();

	// Add long value
	I_Value_Ptr valStr = pFblDb->CreateValue( kTypeString, fNone );
	valStr->put_String( inStr );
	pBinds->AddItem( valStr );

	// Add long value
	I_Value_Ptr valLong = pFblDb->CreateValue( kTypeLong, fNone );
	valLong->put_Long( inLong );
	pBinds->AddItem( valLong );

	// Add float value
	I_Value_Ptr valFloat = pFblDb->CreateValue( kTypeFloat, fNone );
	valFloat->put_Float( inFloat );
	pBinds->AddItem( valLong );

	return pBinds;
}

/ 
************************************************************************ 
**********************/
void CreateT1Table( VSQL::I_SqlDatabase_Ptr pSqlDB )
{
	String query = "";
	
	query += "CREATE TABLE T1 ("
						"fld_String String(40)"
						", fld_Long Long"
						", fld_Float Float"
						")";
	
	pSqlDB->SqlExecute( query );
}


/ 
************************************************************************ 
**********************/
void FillT1Table( VSQL::I_SqlDatabase_Ptr pSqlDB )
{
	// create bind array
	ArrayOfValues_Ptr pBinds1 = BuildArrayOfBinds( pSqlDB, "1", 2, 3 );
	SQL_Execute( pSqlDB, "insert into T1 ( fld_string, fld_long,  
fld_float ) values( :1, :2, :3 )", pBinds1 );

	ArrayOfValues_Ptr pBinds2 = BuildArrayOfBinds( pSqlDB, "1", 3, 2 );
	SQL_Execute( pSqlDB, "insert into T1 ( fld_string, fld_long,  
fld_float ) values( :1, :2, :3 )", pBinds2 );

	ArrayOfValues_Ptr pBinds3 = BuildArrayOfBinds( pSqlDB, "", 0, 3 );
	SQL_Execute( pSqlDB, "insert into T1 ( fld_string, fld_long,  
fld_float ) values( :1, :2, :3 )", pBinds3 );

	ArrayOfValues_Ptr pBinds4 = BuildArrayOfBinds( pSqlDB, "", 3, 0 );
	SQL_Execute( pSqlDB, "insert into T1 ( fld_string, fld_long,  
fld_float ) values( :1, :2, :3 )", pBinds4 );
}



More information about the Valentina mailing list