Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[C++] Deadlock while connecting to mysql

Junior Spellweaver
Joined
Feb 24, 2007
Messages
189
Reaction score
1
Well I started working on a server sdk so nubs who don't know how to code can make their own servers in their own way.

But anyways here is my problem. I am using libmysql to connect to mysql. and when I start the main executable[included some sample code]

I don't get reffered to a file / line # but in Disassembly I get this.

PHP:
0040104E  xor         eax,esp 
00401050  mov         dword ptr [esp+58h],eax 
00401054  push        esi  
00401055  movzx       edx,word ptr ds:[40D3F8h] 
0040105C  mov         eax,dword ptr ds:[0040D3F0h] 
00401061  mov         ecx,dword ptr ds:[40D3F4h] 
00401067  mov         dword ptr [esp+8],eax 
0040106B  mov         eax,dword ptr ds:[0040D3FCh] 
00401070  mov         dword ptr [esp+0Ch],ecx 
00401074  mov         cx,word ptr ds:[40D400h] 
0040107B  mov         word ptr [esp+10h],dx 
00401080  mov         dl,byte ptr ds:[40D402h] 
00401086  mov         dword ptr [esp+38h],eax 
0040108A  mov         dword ptr [esp+28h],eax 
0040108E  mov         eax,dword ptr [esp+4Ch] 
00401092  mov         word ptr [esp+3Ch],cx 
00401097  mov         word ptr [esp+2Ch],cx 
0040109C  mov         byte ptr [esp+3Eh],dl 
004010A0  mov         byte ptr [esp+2Eh],dl 
004010A4  mov         byte ptr [eax],0 ///// Break point here to mark where error starts
004010A7  mov         ecx,dword ptr ds:[40D3FCh] 
004010AD  movzx       edx,word ptr ds:[40D400h] 
004010B4  mov         al,byte ptr ds:[0040D402h] 
004010B9  mov         dword ptr [esp+18h],ecx 
004010BD  mov         ecx,dword ptr [esp+54h] 
004010C1  push        ecx  
004010C2  mov         dword ptr [esp+4Ch],0CEAh 
004010CA  mov         word ptr [esp+20h],dx 
004010CF  mov         byte ptr [esp+22h],al 
004010D3  call        004011B6 
004010D8  mov         esi,eax 
004010DA  test        esi,esi

But the code that im using is the following.

PHP:
/***************************************/
/* Coded by Night2Dark2             */
/* www.revgamers.net               */
/* File : config_mysql.h                 */
/***************************************/
#ifndef __CONFIG_MYSQL_H_
#define __CONFIG_MYSQL_H_

////// Includes are here /////////////
#include <windows.h>
#include <stdio.h>
#include <conio.h>

#include "config-win.h"
#include "my_global.h"
#include "my_dbug.h"
#include "my_pthread.h"
#include "mysql.h"
//////////////////////////////////////

typedef struct db_mutex {
    char* name;
    MYSQL *db;
} db_mutex;

typedef struct db_donfig {
    char host[16]; // Host name
    char user[16]; // User name
    char password[16]; // Password
    char dbName[16]; // Database name
    unsigned int SQLport; // SQL port
    char* socket;
} db_config;

class ZMysql
{
public:
    db_config dbc;
    db_mutex dbm;

    void db_die(char *fmt, ...);

    MYSQL *db_connect(MYSQL *db, db_config *dbc);

    void db_disconnect(MYSQL *db);

    long db_query(MYSQL *db, const char *query);

    ~ZMysql();
    ZMysql();
};

#endif
/***************************************/

PHP:
/***************************************/
/* Coded by Night2Dark2             */
/* www.revgamers.net               */
/* File : ZMysql.cpp                      */
 /***************************************/

#include "config_mysql.h"

ZMysql::ZMysql()
{
    // Standerd constructer
}

ZMysql::~ZMysql()
{
    // Standerd deconstructer
}

void ZMysql::db_die(char *fmt, ...)
{
    int i;

    va_list ap;
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
    (void)putc('\n', stderr);
    ZMysql::db_disconnect(ZMysql::dbm.db);
    exit(EXIT_FAILURE);
}

MYSQL *ZMysql::db_connect(MYSQL *db, db_config *dbc)
{
    if(!(db = mysql_init(db)))
    {
        ZMysql::db_die("mysql_init failed: %s", mysql_error(db));
    }
    else
    {
        if(!mysql_real_connect(db, dbc->host, dbc->user, dbc->password, dbc->dbName, dbc->SQLport, dbc->socket, 0))
        {
            ZMysql::db_die("mysql_real_connect failed: %s", mysql_error(db));
        }
        return db;
    }
}

void ZMysql::db_disconnect(MYSQL *db)
{
    if(db)
        mysql_close(db);
}

long ZMysql::db_query(MYSQL *db, const char *query)
{
    long ret;

    ret = mysql_query(db, query);

    if(ret != 0) {
        db_die("mysql_query failed: %s", mysql_error(db));
    }
    else
    {
        MYSQL_RES *res;

        res = mysql_store_result(db);

        if(res)
        {
            MYSQL_ROW row, end_row;
            unsigned int num_fields;

            num_fields = mysql_num_fields(res);

            while((row = mysql_fetch_row(res)))
            {
                for(end_row = row + num_fields; row < end_row; ++row)
                {
                    ++ret;
                }

                mysql_free_result(res);
            }
        } 
        else
        {
            if(mysql_field_count(db) == 0)
            {
                ret = mysql_affected_rows(db);
            }
            else
            {
                ZMysql::db_die("mysql_store_result failed: %s", mysql_error(db));
            }
        }
    }
}
/************************************/

and I am calling it like this.

PHP:
int main()
{
    int retValue = 0;
    SetConsoleTitle("Initializing Gunz Server");
    int initPercent = 0;
    Startup st;

    st.InitializeServer();
    //st.InitializeMySql();

    system("pause");
    return retValue;
}

int Startup::InitializeServer()
{
    // Setup every variable here
    int bErrorCode = 0;
    printf("Initializing Mysql");
    int x = 0;
    Startup::InitializeMySql();
    printf("completed\n");
    return bErrorCode;
}

int Startup::InitializeMySql()
{
    // Start MySQL Connection
    // and do a test :]
    int bErrorCode = 0;

    ZMysql ZDB;
    
    strcpy(ZDB.dbc.host,"localhost");
    
    strcpy(ZDB.dbc.dbName,"GunzDB");
    
    strcpy(ZDB.dbc.password,"GunzDB");
    
    strcpy(ZDB.dbc.socket,"");
    
    ZDB.dbc.SQLport=3306;
    
    strcpy(ZDB.dbc.user,"GunzDB");

    ZDB.db_connect(ZDB.dbm.db, &ZDB.dbc);
    
    printf("...\t");
    
    return bErrorCode;
}

Thats the code however look @ the disassembly and see how this has anything to do with it because the libmysql exists in the folder :3
 
Last edited:
Initiate Mage
Joined
Oct 31, 2008
Messages
2
Reaction score
0
Re: [C++] Server SDK

I don't know much about vb and I'm not sure I understand your question.

Thats the code however look @ the disassembly and see how this has anything to do with it because the libmysql exists in the folder :3

Is libmysql a dll? Not sure this has anything todo with the problem.

You don't say which progaming tool you're using - MSVC?

I'm guessing now - your debugger stops and tells you something has gone bad at the marked disassembly? Since its assembly, chances are the problem does not occur when your code is in context - maybe a library function?

Only thing I see that looks suspious is this

PHP:
int Startup::InitializeMySql()
{
    // Start MySQL Connection
  .
  . 
    strcpy(ZDB.dbc.socket,"");

ZDB.dbc.socket is a char pointer and I can't spot where its initialised. If its not initialised strcpy will copy a null to some random location. This could cause the debugger to stop in strcpy.

I suggest to initialise ZDB.dbc.socket to point at a string. If this is MSVC 2003+ I suggest you use strcpy_s.

HTH
Thad
 
Junior Spellweaver
Joined
Feb 24, 2007
Messages
189
Reaction score
1
Re: [C++] Server SDK

Thad, I am using VS2005 however,

I don't think that is the problem It could be but I don't really think so.

I will test out what you are saying and post back,


Thank you for your help
N2D2
 
Skilled Illusionist
Joined
Nov 12, 2007
Messages
333
Reaction score
3
Re: [C++] Server SDK

Wow, how long did it learn to code C++ like that? Im learning right now but dang.
 
Newbie Spellweaver
Joined
Nov 4, 2008
Messages
35
Reaction score
0
Re: [C++] Server SDK

Wow, how long did it learn to code C++ like that? Im learning right now but dang.
That's OOP (Object Oriented Programming)... not that complicated, but very useful :)
 
Skilled Illusionist
Joined
Nov 12, 2007
Messages
333
Reaction score
3
Re: [C++] Server SDK

That's OOP (Object Oriented Programming)... not that complicated, but very useful :)

oh well i know Php and a little tiny bit of java and that code looked pretty tough.
 
Newbie Spellweaver
Joined
Nov 4, 2008
Messages
35
Reaction score
0
Re: [C++] Server SDK

oh well i know Php and a little tiny bit of java and that code looked pretty tough.
What he wrote isn't a PHP code, it's C++ code put withing PHP tags, by mistake, I assume. In both - PHP and C++ you can program without OOP, but it won't work out well for larger scale programs. OOP provides a good way to seperate the different code parts and keep the code clean.
 
Junior Spellweaver
Joined
Feb 24, 2007
Messages
189
Reaction score
1
Re: [C++] Server SDK

I use oop to keep my code clean and so I don't have to rewrite the same function like 300x. but Init doesnt need to be into its own voids i just like to keep clean and track errors. [on initalization :p].

I put into PHP tags because it preforms some source code highlighting and I like that.
 
Junior Spellweaver
Joined
Feb 24, 2007
Messages
189
Reaction score
1
Re: [C++] Server SDK

Anyways

my error lands me here.

PHP:
MYSQL *ZMysql::db_connect(MYSQL *db, db_config *dbc)
{
	if(!(db = mysql_init(db))) // Lands me right there
	{
		ZMysql::db_die("mysql_init failed: %s", mysql_error(db));
	}
	else
	{
		if(!mysql_real_connect(db, dbc->host, dbc->user, dbc->password, dbc->dbName, dbc->SQLport, dbc->socket, 0))
		{
			ZMysql::db_die("mysql_real_connect failed: %s", mysql_error(db));
		}
		return db;
	}
}

Thats my Connection to mysql function[using libmysql thingy :p]

I had to do extra work to find out that it was landing to db_connect like adding printfs before what its going to do as a test :3[yes its annoying + boring but its debug oh well]
 
Junior Spellweaver
Joined
Feb 24, 2007
Messages
189
Reaction score
1
Anoyne ever used libmysql that can help me because this is really delaying my project.
 
Back
Top