`
897371388
  • 浏览: 529697 次
文章分类
社区版块
存档分类
最新评论

武侠世界2-健壮性

 
阅读更多

前几周就获得的武侠世界2的源代码,一直没有时间表去看。从网上搞来的武侠世界2的源代码,能编译通过,大的问题没有,小问题还是挺多。其它的细节,大家其实可以在网上搜索一下。下面的游戏运行的截图:

我还把角色升到2级呢,废话少说,直奔主题。

1、在windows下代码的健壮性

打开World.sln,工程的main函数在World.cpp里面。开始部分我们能看到

#if defined(__WINDOWS__)

SetUnhandledExceptionFilter(MyUnhandledExceptionFilter);

_CrtSetDbgFlag(_CrtSetDbgFlag(0) |_CRTDBG_LEAK_CHECK_DF);

#endif

这段代码。

SetUnhandledExceptionFilter就是设置异常捕获函数,这里原捕捉函数就是MyUnhandledExceptionFilter了,也就是说,系统原来打印异常有默认的处理函数,现在的异常处理将会跳到你指定的函数,这对于游戏服务端是非常重要的,因为设置异常捕获函数,在系统存在异常的时候,我们还可以使游戏继续运行下去,争取了一些时间去修改系统的bug。详细的应用例子可以查看下面的例子

http://blog.sina.com.cn/s/blog_5d8945610100pnzg.html

_CrtSetDbgFlag能检测内存泄露情况。内存泄露危害,特别是游戏服务端不重启运行一段较长时间,其危害性更加严重。而_CrtSetDbgFlag能在程序调试时发现内存泄露的情况,这样将大大减少程序内存泄露情况。

#include <iostream>
#include <crtdbg.h>

using namespace std;

void main()
{

    int * c = new int[5];

    c[0]=1;

    c[1]=2;

    _CrtSetDbgFlag(_CrtSetDbgFlag(0) | _CRTDBG_LEAK_CHECK_DF);

    int * p = new int[3];

    cout << "Test Memory Leak" << endl;

}


按下F5,运行程序。

你可以对比注释掉_CrtSetDbgFlag(_CrtSetDbgFlag(0) |_CRTDBG_LEAK_CHECK_DF);和没有注释_CrtSetDbgFlag(_CrtSetDbgFlag(0) |_CRTDBG_LEAK_CHECK_DF);调试窗口输出情况

很明显调试窗口告诉你有一块12字节长和一块20字节长的内存没有释放,其中20字节长的内存数据是01 00 00 00 02 00 00 00 CD CD CD CD CD CD CD CD,正好是我们程序赋值了的数据。

2、在linux下代码的健壮性

看到上面的代码,这只是在windows下运行才会这样,那样在linux下武侠世界又是怎样做的呢?根据我过往的经验,我搜索一下程序有关信号的代码,果然,给我搜索到了。

GameUtil.h
class ExceptionHandler
{
public:
	ExceptionHandler();
	static VOID Handler(INT);
};
extern ExceptionHandler		g_ExceptionHandler;

GameUtil.cpp
ExceptionHandler::ExceptionHandler()
{
#ifdef __LINUX__
	signal(SIGSEGV, Handler);
	signal(SIGFPE, Handler);
#endif
}

VOID ExceptionHandler::Handler(INT)
{
	DumpStack("ExceptionHandler::Handler");
	exit(0);
}

ExceptionHandler		g_ExceptionHandler;

但是在武侠世界中,当程序捕捉到signal后,在Handler中打印了信息后就退出了程序,这应该不太适合游戏服务端的要求,因为游戏在实际运行存在太多的不确定性了。就像我之前的说的,我们希望的是,出现了异常还能继续运行一段时间。因此我写一段代码来实现这个想法。写这段代码的灵感恰恰来自

extern ExceptionHandler g_ExceptionHandler;

和在构造函数里面初始化signal。

这样写法的优势是:只要声明一次,在全局都有效。

#ifndef SHOWCRASH_H
#define SHOWCRASH_H

#include <setjmp.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <execinfo.h>


/*
 *  ??????????????SETJMP??
 *  ???SETJMP????SETJMP???CRASH???????SETJMP?
 *  ???SETJMP???CRASH???????SETJMP
 */
class ShowCrash
{
public:
    ShowCrash();

    static void CrashFunction(int);

    static const int MAX_JMP_BUF = 16;

    static const int MAX_CALLSTACK_DEPTH = 32;

    enum buffername
    {
        BUF_MAIN,
        BUF_COUNT
    };

    static void SetIndex(int index){buf_index = index;}

    static int  GetIndex(){return buf_index;}

     static int buf_index;
private:


};



#define SETJMP(index)\
    setjmp(buff[index]);\
   ShowCrash::SetIndex(index);\

extern ShowCrash g_showcrash;
extern jmp_buf buff[ShowCrash::MAX_JMP_BUF];

#endif

#include "showcrash.h"



ShowCrash g_showcrash;
jmp_buf buff[ShowCrash::MAX_JMP_BUF];
int ShowCrash::buf_index = 0;

ShowCrash::ShowCrash()
{
    struct sigaction act, oact;
    act.sa_handler = CrashFunction;
    sigemptyset(&act.sa_mask); //娓呯┖姝や俊鍙烽泦
    act.sa_flags = SA_NODEFER;
    sigaction(SIGINT, &act, &oact);
    sigaction(SIGABRT, &act, &oact);
    sigaction(SIGSEGV, &act, &oact);
    sigaction(SIGFPE, &act, &oact);
    sigaction(SIGBUS, &act, &oact);
    sigaction(SIGTRAP,&act,&oact);
   // buf_index = 0;
}

void ShowCrash::CrashFunction(int)
{
    void *traceback[MAX_CALLSTACK_DEPTH];
    char cmd[512] = "addr2line -f -e GameEngine";
    FILE *fp = popen(cmd, "w");
    int depth = backtrace(traceback, MAX_CALLSTACK_DEPTH);
    for (int i = 0; i < depth && i < MAX_CALLSTACK_DEPTH; i++)
    {
        fprintf(fp, "%p\n", traceback[i]);
    }
    fclose(fp);
    longjmp(buff[ShowCrash::GetIndex()],1);
}

这样就能够在出现系统异常的时候,通过setjmp和longjmp来让程序继续运行。

交流群:315249378

如有不正确,欢迎交流讨论!


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics