Like Tree1Likes

mysql_real_escape_string memory leak ?

This is a discussion on mysql_real_escape_string memory leak ? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by xwielder Would you agree this be the best solution? No, it is unnecessarily complex. The point of ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    681
    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.
    I never put signature, but I decided to make an exception.

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

Page 2 of 2 FirstFirst 12
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, 10:06 AM
  2. Memory Leak
    By jtullo in forum C Programming
    Replies: 7
    Last Post: 12-11-2006, 10: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, 02:09 PM
  5. memory leak
    By stormbringer in forum C Programming
    Replies: 7
    Last Post: 07-17-2002, 09:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21