List:MySQL++« Previous MessageNext Message »
From:Byrial Jensen Date:May 25 2005 9:22pm
Subject:Re: Can you have more than one active mysqlpp::Connection object?
View as plain text  
Chris Frey wrote:
> On Wed, May 25, 2005 at 11:37:07AM -0700, Earl Miles wrote:
> 
>>From looking this over, I think your problem might be string manipulation.
>>
>>const char *text = row[1];
>>
>>I'm not sure row[] is guaranteed to return a null terminated string. 

Now I have studied the mysql++ source code. row[] returns a temporary 
ColData object and that object have a function to do conversion to 
"const char *" which indeed gives a pointer to a NUL-terminated string.

However - as Chris points out - the ColData object is destructed 
immidiately after the conversion, leaving the string pointer pointing to 
freed memory, which caused all the trouble.

> Yep, Earl is on the right track.  See the declaration of operator[] in
> row.h:
> 
> const ColData   operator [] (size_type i) const;
> 
> This returns a temporary, which disappears after that line of code.
> You need to make a copy for yourself.

I replaced the line

   const char *text = row[1];

with

    const ColData text_col = row[1];
    const char *text = text_col;

and that fixed all observed problems in my programs. Thank you very much 
for the suggestion.

I don't know if it is considered a bug that you cannot write

    const char *text = row[1];

but even if it isn't, it might be an idea to warn about the construction 
in the manual.

Thanks to all helpful people in this thread.

Best regards
Byrial
Thread
Can you have more than one active mysqlpp::Connection object?Byrial Jensen24 May
  • Re: Can you have more than one active mysqlpp::Connection object?Earl Miles24 May
  • Re: Can you have more than one active mysqlpp::Connection object?Warren Young24 May
    • Re: Can you have more than one active mysqlpp::Connection object?Byrial Jensen25 May
      • Re: Can you have more than one active mysqlpp::Connection object?Earl Miles25 May
        • Re: Can you have more than one active mysqlpp::Connection object?Chris Frey25 May
          • Re: Can you have more than one active mysqlpp::Connection object?Chris Frey25 May
            • Re: Can you have more than one active mysqlpp::Connection object?Earl Miles25 May
          • Re: Can you have more than one active mysqlpp::Connection object?Byrial Jensen25 May
            • Re: Can you have more than one active mysqlpp::Connection object?Warren Young26 May
              • Re: Can you have more than one active mysqlpp::Connection object?Byrial Jensen26 May
                • Re: Can you have more than one active mysqlpp::Connection object?Warren Young26 May