STL 与 DLL 高手请进 !!!

coolstar 2002-11-27 08:06:26

DLL文件MapDll.Dll代码

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}

class __declspec(dllexport) CMapTest
{
typedef map <string, int> ContextList;
public:
void DelData(int Data);
void AddData(string Index,int Data);
CMapTest(){}
virtual ~CMapTest(){}

ContextList m_ContextList;
};

void CMapTest::AddData(string Index,int Data)
{
m_ContextList[Index] = Data;
}

void CMapTest::DelData(int Data)
{
ContextList::iterator mapIter = m_ContextList.begin();
while(mapIter != m_ContextList.end())
{
if((*mapIter).second == Data)
{
m_ContextList.erase(mapIter);
break;
}
mapIter++;
}
}



exe 文件Test.exe代码

int main(int argc, char* argv[])
{
CMapTest m_MapTest;
m_MapTest.AddData("111111",18);
m_MapTest.DelData(18);
return 0;
}


问题:运行Test.exe 出错


Debug Assertion Failed!
Program: E:\eeee\Debug\eeee.exe
File:dbgheap.c
Line:1044

Expresssion:_CrtIsValidHeapPointer(pUserData)

For information on how you programe can cause an assertion
failure, see the Visual C++ documentation on asserts
谁能解释这个问题?
...全文
98 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
hzxswfj 2002-11-29
  • 打赏
  • 举报
回复
我想有可能是没有调用析构函数吧。

你把它定义为友员试试看看???
Bowdom 2002-11-29
  • 打赏
  • 举报
回复
应该是这个地方:
while(mapIter != m_ContextList.end())
{
if((*mapIter).second == Data)
{
m_ContextList.erase(mapIter);
break;
}
mapIter++;
}
在 erase 之后 mapIter 的状态是不确定的 此时再调 mapIter++ 就不对了
Behard 2002-11-28
  • 打赏
  • 举报
回复
C 能使用 class 吗?
coolstar 2002-11-28
  • 打赏
  • 举报
回复
我用的vc装了sp5,好像是内存释放的时候堆地址异常
ToUpdate 2002-11-28
  • 打赏
  • 举报
回复
up
mmosquito 2002-11-28
  • 打赏
  • 举报
回复
楼住可以试一下我说的方法
class __declspec(dllexport) CMapTest
{
typedef map <string, int> ContextList;
public:
void DelData(int Data);
void AddData(string Index,int Data);
CMapTest(); //no {}
virtual ~CMapTest(); //no {}

ContextList m_ContextList;
};
coolstar 2002-11-28
  • 打赏
  • 举报
回复
谢谢大家!

我再加一句,程序是运行到m_ContextList.erase(mapIter);时出现问题的
堆地址异常

mmosquito 2002-11-28
  • 打赏
  • 举报
回复
大概讲一下我所理解的这个问题所在,不一定正确哦,楼主。

因为 map <string, int> 在dll和exe都可见,所以他有两份代码,一份在dll中,一份在exe中,我们看见构造函数是定义在class内的,所以
CMapTest m_MapTest;//调用了exe的代码
m_MapTest.AddData("111111",18);//调用了dll的代码
又因为使用了静态变量,所以就错了
那么修改的方法最简单的就是构造和析构不在class内部定义,这样使用的全部是dll的代码,就没有错了。

请指正.
once168 2002-11-27
  • 打赏
  • 举报
回复

将m_ContextList.erase(mapIter);改成下面试一下

mapIter=m_ContextList.erase(mapIter);
continue;
mmosquito 2002-11-27
  • 打赏
  • 举报
回复
呵呵,看起来像使用的vc,否则dll不好export class

别的不清楚,如果是vc6.0 (sp5) 的话,它的map的实现使用到了静态变量,所以出错是一定的了。
ccaommao 2002-11-27
  • 打赏
  • 举报
回复
文件名的问题吧。

24,860

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧