mysql API를 사용하면서 Segment Fault가 나서요..

fuerza의 이미지

mysql Ver 11.16 Distrib 3.23.49, for redhat-linux-gnu (i386)

void CFosdb::DBConnect(const char *host,const char *user, const char* passwd, const char* db)
{
    if((mysql = (MYSQL*)malloc(sizeof(MYSQL))) != NULL)
    {
        if(mysql_real_connect(mysql,host,user,passwd,db,0,NULL,0) == NULL)
        {
            free(mysql);
            exit(1);
        }
    }
    printf("connect 후..\n");
}

이런식으로 했습니다.

근데 단독으로 실행하면 잘되는데, 한번씩 다른 프로그램에서 불러다 쓰면 Segement fault가 나버립니다.

mysql_err()로 출력을 할려고 해도 출력도 안되고요..nㅜ.ㅜ

근데 execl()로 해서 사용하면 되더군요 ㅜ.ㅜ;; 왜이러는지...

도와 주세요..^^

akbar의 이미지

fuerza wrote:
mysql Ver 11.16 Distrib 3.23.49, for redhat-linux-gnu (i386)

void CFosdb::DBConnect(const char *host,const char *user, const char* passwd, const char* db)
{
    if((mysql = (MYSQL*)malloc(sizeof(MYSQL))) != NULL)
    {

        mysql_init(mysql); /* <== 이 부분을 추가하세요  *

        if(mysql_real_connect(mysql,host,user,passwd,db,0,NULL,0) == NULL)
        {
            free(mysql);
            exit(1);
        }
    }
    printf("connect 후..\n");
}

근데 단독으로 실행하면 잘되는데, 한번씩 다른 프로그램에서 불러다 쓰면 Segement fault가 나버립니다.

see : mysql_init(mysql); /* <== 이 부분을 추가하세요 *

fuerza의 이미지

CFosdb()
    {
        mysql = mysql_init(NULL);
    }

이렇게 초기화 해주었습니다.-

choissi의 이미지

fuerza wrote:
CFosdb()
    {
        mysql = mysql_init(NULL);
    }

이렇게 초기화 해주었습니다.-

Quote:
8.4.3.122 mysql_init()

MYSQL *mysql_init(MYSQL *mysql)

8.4.3.123 Description
Allocates or initialises a MYSQL object suitable for mysql_real_connect(). If mysql is a NULL pointer, the function allocates, initialises, and returns a new object. Otherwise, the object is initialised and the address of the object is returned. If mysql_init() allocates a new object, it will be freed when mysql_close() is called to close the connection.

8.4.3.124 Return Values
An initialised MYSQL* handle. NULL if there was insufficient memory to allocate a new object.

메뉴얼에서 mysql_init에 관한 부분입니다.
보시면 인수가 NULL일때는 MYSQL구조체를 할당하고,
초기화를 진행합니다..

님께서 작성하신 CFosdb 생성자에서
이미 할당되고 초기화된 MYSQL 정보가 DBConnect가 실행이 되면서
새로이 malloc이 실행이 될터이고, 그러면 생성자에서 할당된 만큼
메모리릭이 발생이 됩니다. 또, DBConnect에선 할당만 한것이지,
초기화가 되질 안았으니.. 정의되지 않은 동작을 하게되겠네요..

ps. 메뉴얼을 꼼꼼히 읽어 보셔야 겠네요...
만약 저렇게 에러가 나지않았다면 메모리 새는것은 여간해서는 발견
하기 힘들었을테니까요...

울랄라~ 호기심 천국~!!
http://www.ezdoum.com

ilanch의 이미지

쓰레드로 mysql_real_connect() 함수를 이용할 때 가끔씩 발생하는 것을 봤습니다.

관련 버글 레포트를 작성해서 MySQL 사이트에 제출했고, 관련 담당자가 분석중입니다.
아래 링크 참조하면, 제가 시험한 오류와 진행상황도 볼 수 있을 것입니다.

http://bugs.mysql.com/bug.php?id=37653

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.