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
>