Thread: mysql_real_escape_string memory leak ?

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by xwielder
    Would you agree this be the best solution?
    No, it is unnecessarily complex. The point of using std::string here is so that you don't have to do manual memory management.

    Quote Originally Posted by xwielder
    The program constantly crashes when I use single quotes. (Seems to break on malloc.c "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\crt\src\malloc.c" on line 55, which is this:
    The reason is simple: you pretty much ignored what I wrote in post #6. When I posted my example in post #9, I was working on the assumption that my earlier example in post #4 mixed up the parameters, hence the compile error that you alluded to, so I should reverse the order. I was wrong, but you failed to check.

    In fact, I was wrong on 3 counts:
    • std::string does not have a constructor that takes a single parameter of integer type.
    • std::string does have a constructor that takes two parameters, with exactly what I had in mind in post #2, so reversing the order is wrong.
    • Mixing up the order would not result in a compile error, which is precisely the problem you experienced later.


    Consequently, this should be correct:
    Code:
    std::string sql(const char* dataIn)
    {
        std::size_t dataInLen = strlen(dataIn);
        std::string to(dataInLen * 2 + 1, '\0');
        mysql_real_escape_string(MySQL_Database_Connection__global, &to[0], dataIn, dataInLen);
        return to;
    }
    That said, note that at the moment, the &to[0] trick is not guaranteed by the standard to result in a pointer to the first element of a contiguous sequence of characters. However, it will be guaranteed later this year, and your standard library implementation probably does it that way.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by xwielder View Post
    The program constantly crashes when I use single quotes.
    The proper constructor is:

    string::string - C++ Reference

    The first argument denotes number of characters, the second one the character used:

    Code:
    std::string to(strlen(dataIn) * 2, ' ');

    Using single quotes constructs string containing N such characters (spaces in this case).
    Using double quotes constructs string initialized to N first characters of the given string. The: " " are two characters: space and null terminator. When N > 2 you read garbage which results in undefined behaviour and possible crashes.

  3. #18
    Registered User
    Join Date
    Nov 2006
    Posts
    55
    Got it... mostly. Thank you both for thorough reviews and helpful advice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak?
    By lovercjs in forum C Programming
    Replies: 5
    Last Post: 03-04-2011, 11:06 AM
  2. Memory Leak
    By jtullo in forum C Programming
    Replies: 7
    Last Post: 12-11-2006, 11:45 PM
  3. Replies: 2
    Last Post: 09-28-2006, 01:06 PM
  4. A memory leak (I think...)
    By Stevek in forum C++ Programming
    Replies: 7
    Last Post: 03-16-2003, 03:09 PM
  5. memory leak
    By stormbringer in forum C Programming
    Replies: 7
    Last Post: 07-17-2002, 09:56 AM