Every project I use sdo is set the Multi-threaded Debug DLL (/MDd) option.

I modified the getColumnName function to free the allocated memory:

std::string getColumnName(HSTMT statement, int column) {
   SQLCHAR* sqlPtr = 0;
   char strAux[1];
   SQLSMALLINT length = 0;
   SQLColAttributeA(statement, column, SQL_DESC_BASE_COLUMN_NAME, &strAux,
1, (SQLSMALLINT*) &length, NULL);
   length++;
   sqlPtr = (SQLCHAR*) new SQLCHAR[length];
   SQLColAttributeA(statement, column, SQL_DESC_BASE_COLUMN_NAME, sqlPtr,
length, (SQLSMALLINT*) &length, NULL);

   std::string ret((char*) sqlPtr);
   delete [] sqlPtr; // free the allocated memory
   return ret;

}


As I fore mentioned, the string returned by the getColumnName function is
correct, for example, the column named "ID" is returning the string "ID".
But further when I try to get the property name from the type the string
returned has 4 random character in its beginning:

int m;
commonj::sdo::PropertyList list = myType.getProperties();
for (m = 0 ; m < list.size() ; m++) {
   std::cout << list[m].getName() << "\n";
}

Output:

xèH►ID            // should be "ID"
xèH►DESCR    // should be "DESCR"
xèH►UNITS      // should be "UNITS"

Adriano Crestani


On 3/16/07, Pete Robbins <[EMAIL PROTECTED]> wrote:

Is this on Windows? I'm wondering whether you have 2 different
implementations of std::string by using a different runtime library... or
some such difference. Can you make sure your program is linked with
Multi-threaded Debug dll ( /Mdd ) or Multi-threaded dll ( /Md) as this is
what sdo is built with. This is in the C++ Code Generation setting in VC
Express.

Cheers,


On 16/03/07, Adriano Crestani <[EMAIL PROTECTED]> wrote:
>
> Pete,
>
> I changed the way I'm allocating the string in the function
getColumnName:
>
> std::string getColumnName(HSTMT statement, int column) {
>    SQLCHAR* sqlPtr = 0;
>    char strAux[1];
>    SQLSMALLINT length = 0;
>    SQLColAttributeA(statement, column, SQL_DESC_BASE_COLUMN_NAME,
&strAux,
> 1, (SQLSMALLINT*) &length, NULL);
>    length++;
>    sqlPtr = (SQLCHAR*) new char(length);
>    SQLColAttributeA(statement, column, SQL_DESC_BASE_COLUMN_NAME,
sqlPtr,
> length, (SQLSMALLINT*) &length, NULL);
>
>    std::string ret((char*) sqlPtr);


     *delete (char*)sqlPtr; // free up allocated memory*

   return ret;
>
> }
>
> And it's now adding the all properties, but when I use a for...
>
> int m;
> commonj::sdo::PropertyList list = myType.getProperties();
> for (m = 0 ; m < list.size() ; m++) {
>    std::cout << list[m].getName() << "\n";
> }
>
> ...to read the properties' names to check if everything was correctly
> added,
> I always get four random characters now instead of for '=' character.
> Something changed, but it's still not working as expected : (
>
> Adriano Crestani
>
>
> On 3/15/07, Adriano Crestani <[EMAIL PROTECTED]> wrote:
> >
> > OK, I found it: new char[<length>], right?! ; )
> >
> > Adriano Crestani
> >
> > On 3/15/07, Adriano Crestani < [EMAIL PROTECTED]> wrote:
> > >
> > > OK, thanks Pete, but how do I alloc memory with new? I will search
for
>
> > > it.
> > >
> > > Adriano Crestani
> > >
> > > On 3/15/07, Pete Robbins < [EMAIL PROTECTED]> wrote:
> > > >
> > > > I'm not sure what is going on but using malloc in C++ is not a
good
> > > > idea.
> > > > You are mixing 2 different memory management systems. You could
try
> > > > using
> > > > the C++ new to allocate memory for the sqlPtr. You are also
> allocating
> > > > memory with malloc that is never freed.
> > > >
> > > > I can't recreate this with adding Properties with strings so I
> suspect
> > > > that
> > > > there is something odd in the malloc/char*/string handling.
> > > >
> > > > Cheers,
> > > >
> > > > On 14/03/07, Adriano Crestani < [EMAIL PROTECTED]> wrote:
> > > > >
> > > > > Hey,
> > > > >
> > > > > Now I was checking if the properties were correctly added to the
> > > > type and
> > > > > I
> > > > > got something strange:
> > > > >
> > > > > dataFactory->addPropertyToType(myType, *(new
> > > > > std::string(getColumnName(statement, 1)), intType);
> > > > > dataFactory->addPropertyToType(myType, *(new
> > > > > std::string(getColumnName(statement, 2)), intType);
> > > > >
> > > > > // on my test the column names returned by the getColumnName
> > > > function are
> > > > > respectively "ID" and "DESCR"
> > > > >
> > > > > ...
> > > > >
> > > > > //then I checked if the properties were correctly added with
this
> > > > loop:
> > > > >
> > > > > int m;
> > > > > commonj::sdo::PropertyList list = myType.getProperties();
> > > > > for (m = 0 ; m < list.size() ; m++) {
> > > > >    std::cout << list[m].getName() << "\n";
> > > > > }
> > > > >
> > > > >
> > > > > The output should be:
> > > > >
> > > > > ID
> > > > > DESCR
> > > > >
> > > > > But it's outputting:
> > > > >
> > > > > ====ID
> > > > > ====DESCR
> > > > >
> > > > > I don't know from where this four "=" are coming from, cause I
> > > > already
> > > > > have
> > > > > checked with a debugger and the strings "ID" and "DESCR" are
being
> > > > added
> > > > > and
> > > > > there is no '=' ('\205' as shown on the debugger) character in
it.
>
> > > > >
> > > > > Is this a bug?
> > > > >
> > > > > Adriano Crestani
> > > > >
> > > > > //as I've already said I'm adding the properties this way:
> > > > >
> > > > > On 3/14/07, Adriano Crestani <[EMAIL PROTECTED]> wrote:
> > > > > >
> > > > > > I created a new type on a DataFactory, for example
> > > > > >
> > > > > > ...
> > > > > > dataFactory->addType("MyNameSpace", "MyRoot");
> > > > > > const Type& myType = dataFactory->getType("MyNameSpace",
> > > > "MyRoot");
> > > > > > const Type& intType = dataFactory->getType(" commonj.sdo",
> > > > "Integer");
> > > > > >
> > > > > >    //then I try to add some integer properties to this type
> > > > > >
> > > > > > dataFactory->addPropertyToType(myType,
getColumnName(statement,
> > > > 1),
> > > > > > intType);
> > > > > > std::cout << "Properties count = " << myType.getProperties
> ().size()
> > > > <<
> > > > > > "\n";
> > > > > >
> > > > > > dataFactory->addPropertyToType(myType,
getColumnName(statement,
> > > > 2),
> > > > > > intType);
> > > > > > std::cout << "Properties count = " << myType.getProperties
> ().size()
> > > > <<
> > > > > > "\n";
> > > > > >
> > > > > > ...
> > > > > > }
> > > > > >
> > > > > > //where getColumnName function is defined as:
> > > > > >
> > > > > > std::string getColumnName(HSTMT statement, int column) {
> > > > > >     SQLPOINTER sqlPtr = 0;
> > > > > >     char strAux[1];
> > > > > >     SQLSMALLINT length = 0;
> > > > > >     SQLColAttributeA(statement, column,
> SQL_DESC_BASE_COLUMN_NAME,
> > > > > > &strAux, 1, (SQLSMALLINT*) &length, NULL); //get the string
> length
> > > > > >     length++;
> > > > > >     sqlPtr = (char*) malloc(length*sizeof(char)); //alloc the
> > > > enough
> > > > > > memory to place the string characters
> > > > > >     SQLColAttributeA(statement, column,
> SQL_DESC_BASE_COLUMN_NAME,
> > > > > sqlPtr,
> > > > > > length, (SQLSMALLINT*) &length, NULL); //get the string from
the
> > > > db
> > > > > >
> > > > > >     std::string ret = (char*) sqlPtr; //create a new string
> object
> > > > from
> > > > > a
> > > > > > char pointer
> > > > > >     return ret;
> > > > > >
> > > > > > }
> > > > > >
> > > > > >
> > > > > > Unfortunately the output is:
> > > > > >
> > > > > > Properties count = 1
> > > > > > Properties count = 1
> > > > > >
> > > > > > instead of
> > > > > >
> > > > > > Properties count = 1
> > > > > > Properties count = 2
> > > > > >
> > > > > > I've checked if the function getColumnName isn't returning
> strings
> > > > with
> > > > > > the same value, but it is not, they are different strings!!
> > > > > >
> > > > > > Though I tried an strange approach and it's solving the
problem
> > > > for
> > > > > while,
> > > > > > that is to instantiate a new string on memory:
> > > > > >
> > > > > >  dataFactory->addPropertyToType(myType, *(new
> > > > > > std::string(getColumnName(statement, 1)), intType);
> > > > > >  dataFactory->addPropertyToType(myType, *(new
> > > > > > std::string(getColumnName(statement, 2)), intType);
> > > > > >
> > > > > > Can anybody tell why it's happening?
> > > > > >
> > > > > > Adriano Crestani
> > > > > >
> > > > >
> > > >
> > > >
> > > >
> > > > --
> > > > Pete
> > > >
> > >
> > >
> >
>



--
Pete

Reply via email to