游戏开发论坛

 找回密码
 ????
搜索
查看: 9306|回复: 5

请问多线程(MT)和多线程调试(MTd)的区别

[复制链接]

51

主题

134

帖子

140

积分

注册会员

Rank: 2

积分
140
发表于 2008-5-21 20:20:00 | 显示全部楼层 |阅读模式
我将一些代码导出成DLL,然后用EXE连接,发现在DLL中使用到的std::vector 异常,经过研究,我发现是代码生成中运行时库设置有误造成。
我有几个问题:
1:运行时阶段2者有什么区别(当然还有MD/MDd。。。懒得测试了)
2:如果我自己写个vector模版类,还会异常吗
3:生成代码中“基本运行时检查”的作用。
4:和lib一起生成的exp文件有什么用?可以不生成吗
(。。。后面2个问题与本案无关)

以下是归纳的测试代码:

搞了半天终于搞定了,查了点资料,MT 和MTD 会定义不同的宏,导入不同的库
MTD会自动定义_DEBUG
在DLL中如果要使用STD 运行时库务必要一致

51

主题

134

帖子

140

积分

注册会员

Rank: 2

积分
140
 楼主| 发表于 2008-5-21 20:20:00 | 显示全部楼层

Re: 请问多线程(MT)和多线程调试(MTd)的区别

//testdll.h:

//结论
//1.当DLL、EXE都使用MT或MTd时,打印正常: 1  abcd  1  1  1  1  4
//2.当DLL使用MT,EXE使用MTd时,打印为:0        0  0  0  0  4 :只有不使用vector时正常,vector中的push_back(。。)出错。
//3.当DLL使用MTd,EXE使用MT时,打印为:xxxxxxx(一堆无用数字)    然后出错。。。,注释掉出错部分后,当只有不使用vector时正常,vector完全出错。

//结论为,无论什么时候都应使用1.。。。
//当使用MT时就无法定义宏_DEBUG,库连接错误,故无法检测异常。(但是我还是决定使用MT。。。)

//testdll.h:

#pragma once
#include <stdio.h>
#include <tchar.h>
#include <vector>

#ifdef DLL
#define        MYDLL          __declspec(dllexport)
#endif

#ifdef DLL
class MYDLL testclass
#else
class testclass
#endif
{
public:
        testclass();
        testclass(char* t__);
        ~testclass();
public:
        char* t;
};


#ifdef DLL
class MYDLL testdll
#else
class testdll
#endif
{
public:

        testdll(void);
        testdll(testclass* cpi);
        ~testdll(void);
public:
        std::vector<testclass*>* tpvpi;
};


#ifdef DLL
class MYDLL testdll2
#else
class testdll2
#endif
{
public:

        testdll2(void);
        testdll2(int* cpi);
        ~testdll2(void);
public:
        std::vector<int*>* tpvpi;
};

#ifdef DLL
class MYDLL testdll3
#else
class testdll3
#endif
{
public:

        testdll3(void);
        testdll3(int* cpi);
        ~testdll3(void);
public:
        std::vector<int*> tpvpi;
};

#ifdef DLL
class MYDLL testdll4
#else
class testdll4
#endif
{
public:

        testdll4(void);
        testdll4(int* cpi);
        ~testdll4(void);
public:
        std::vector<int> tpvpi;
};

#ifdef DLL
class MYDLL testdll5
#else
class testdll5
#endif
{
public:

        testdll5(void);
        testdll5(int cpi);
        ~testdll5(void);
public:
        std::vector<int> tpvpi;
};

#ifdef DLL
class MYDLL testdll6
#else
class testdll6
#endif
{
public:

        testdll6(void);
        testdll6(int* cpi);
        ~testdll6(void);
public:
        int* ttpi;
};

51

主题

134

帖子

140

积分

注册会员

Rank: 2

积分
140
 楼主| 发表于 2008-5-21 20:21:00 | 显示全部楼层

Re: 请问多线程(MT)和多线程调试(MTd)的区别

//testdll.cpp:

#include "testdll.h"
#ifdef DLL
testclass::testclass()
{
        t=NULL;
}
testclass::testclass(char* t__)
{
        if(!t__)
        {
                t=NULL;return;
        }
        t=new char[strlen(t__)+1];
        strcpy(t,t__);
        return;
}
testclass::~testclass()
{
        if(!t)return;
        delete t;
        t=NULL;
}

testdll::testdll(void)
{
        tpvpi=new std::vector<testclass*>();
        tpvpi->clear();
}
testdll::testdll(testclass* cpi)
{
        tpvpi=new std::vector<testclass*>();
        testclass* tcpi=cpi;
        tpvpi->clear();
        tpvpi->push_back(tcpi);
}
testdll::~testdll(void)
{
        if(!tpvpi)return;
        for(unsigned long i=0;i<tpvpi->size();i++)
                delete (*tpvpi);
        tpvpi->clear();
        delete tpvpi;
}


testdll2::testdll2(void)
{
        tpvpi=new std::vector<int*>();
        tpvpi->clear();
}
testdll2::testdll2(int* cpi)
{
        tpvpi=new std::vector<int*>();
        tpvpi->clear();
        tpvpi->push_back(cpi);
}
testdll2::~testdll2(void)
{
        if(!tpvpi)return;
        for(unsigned long i=0;i<tpvpi->size();i++)
                delete (*tpvpi);
        tpvpi->clear();
        delete tpvpi;
}


testdll3::testdll3(void)
{
        tpvpi.clear();
}
testdll3::testdll3(int* cpi)
{
        tpvpi.clear();
        tpvpi.push_back(cpi);
}
testdll3::~testdll3(void)
{
        for(unsigned long i=0;i<tpvpi.size();i++)
                delete (tpvpi);
        tpvpi.clear();
}

testdll4::testdll4(void)
{
        tpvpi.clear();
}
testdll4::testdll4(int* cpi)
{
        tpvpi.clear();
        tpvpi.push_back(*cpi);
}
testdll4::~testdll4(void)
{
        tpvpi.clear();
}

testdll5::testdll5(void)
{
        tpvpi.clear();
}
testdll5::testdll5(int cpi)
{
        tpvpi.clear();
        tpvpi.push_back(cpi);
}
testdll5::~testdll5(void)
{
        tpvpi.clear();
}

testdll6::testdll6(void)
{
        ttpi=NULL;
}
testdll6::testdll6(int* cpi)
{
        ttpi=new int;
        *ttpi=*cpi;
}
testdll6::~testdll6(void)
{
        delete ttpi;
}

#else
#pragma comment(lib,"testdll.lib")

int _tmain(int argc, char* argv[])
{
//*
        testclass* tpi=new testclass("abcd");
        testdll* pt=new testdll(tpi);
        printf("%d\n",pt->tpvpi->size());
       
        if(pt->tpvpi->size()>0)
        {
                printf((*(pt->tpvpi))[0]->t);
                printf("\n");
        }
//*/

        int* tpi2=new int;
        *tpi2=4;

//*
        testdll2* pt2=new testdll2(tpi2);
        printf("%d\n",pt2->tpvpi->size());

        testdll3* pt3=new testdll3(tpi2);
        printf("%d\n",(pt3->tpvpi).size());

        testdll4* pt4=new testdll4(tpi2);
        printf("%d\n",(pt4->tpvpi).size());

        testdll5* pt5=new testdll5(*tpi2);
        printf("%d\n",(pt5->tpvpi).size());
//*/
        testdll6* pt6=new testdll6(tpi2);
        printf("%d\n",*(pt6->ttpi));

        return 1;
}
#endif

//虽然有了结论,但我还不知道在运行时阶段2者有什么区别。。。请教高手

0

主题

769

帖子

1052

积分

金牌会员

Rank: 6Rank: 6

积分
1052
发表于 2008-5-21 21:15:00 | 显示全部楼层

Re:请问多线程(MT)和多线程调试(MTd)的区别

2005与2008制造的恶心东西
搞出了个manifast的文件来
没那个在其他没装VS的电脑上无法运行
就知道这效果- -#
至于性能方面有什么优势我没发现

51

主题

134

帖子

140

积分

注册会员

Rank: 2

积分
140
 楼主| 发表于 2008-5-21 22:33:00 | 显示全部楼层

Re:请问多线程(MT)和多线程调试(MTd)的区别

LS不知所言。。。没一个问题和manifast有关

22

主题

309

帖子

353

积分

中级会员

Rank: 3Rank: 3

积分
353
QQ
发表于 2008-5-21 23:56:00 | 显示全部楼层

Re:请问多线程(MT)和多线程调试(MTd)的区别

MTD是MT的debug版...
vs2005导出dll和manifast有关,貌似是防止不同的dll版本混用,记不得了,反正是一系列非常恶心的设定
现在只用静态库
您需要登录后才可以回帖 登录 | ????

本版积分规则

????|????|????|????|?????? ( ?ICP?17032699?-3 )

GMT+8, 2025-5-24 13:46

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表