最近try Map, 發現一個問題:
在client中new一個map, 並將此map傳入DLL中, 由DLL負責進行map之insert, 可是會發生
"Access violation"的runtime error.
我trace了程式後發現map傳入DLL之前, 其tree結構的"_Parent"與"_Nil"相同,
但是傳入DLL時, _Nill就變成0x00000000, 導致tree 的traverse無法進行.
如果將DLL的程式寫在client中, 則無此問題, why?
請幫幫忙, 謝謝
下面是source code:
// -----------------------------Client:
#include "MapSvr.h"
void
main(void)
{
MapGen mg;
PowerMap *myMap;
PowerMap::iterator it;
myMap = new PowerMap;
mg.generate(0, 5, myMap);
for (it = myMap->begin(); it != myMap->end(); it++)
{
printf("Power of %ld is %ld", (*it).first, (*it).second);
}
return;
// ----------------------------- MapSvr.h
#ifndef __MapSvr_h__
#define __MapSvr_h__
#pragma warning(disable : 4786)
#include <windows.h>
#include <map>
typedef std::map<int, int> PowerMap;
#define PowerMapPair PowerMap::value_type
class __declspec (dllimport) MapGen {
public:
MapGen(void){};
~MapGen(void){};
long generate(DWORD startNum, DWORD endNum, PowerMap* myMap)
};
#endif // __MapSvr_h__
// -------------------- DLL
#include "MapSvr_h.h" // 與MapSvr.h差不多, 只有dllimport改為dllexport
long
MapGen::generate(DWORD startNum, DWORD endNum, PowerMap* newMap)
{
DWORD i;
newMap->clear();
for (i = startNum; i < endNum; i++)
{
newMap->insert(PowerMapPair(i, i*i));
}
return 0;
}