2010-09-28 54 views
0

我的測試代碼出現問題。它編譯得很好,但是當我嘗試調用委託時,程序崩潰。使用FastDelegate的分段錯誤

#include "..\libs\FastDelegate\FastDelegate.h" 
#include <string> 
#include <map> 
#include <iostream> 

typedef fastdelegate::FastDelegate1 <int, int> FuncPtr; 

struct Function 
{ 
FuncPtr Ptr; 
int Param; 
Function() {}; 
Function (FuncPtr Ptr_, int Param_): Ptr (Ptr_), Param (Param_) {}; 
int operator() (int xxx) {return Ptr(xxx);}; 
}; 

std::map <std::string, Function> ExternalFuncs; 

bool RegisterFunction (const std::string& a, const Function b) 
{ 
ExternalFuncs[a] = b; 
return true; 
} 

int foo (int bar) 
{ 
std::cout << "Jest gites"; 
return 0; 
} 

int main() 
{ 
RegisterFunction ("Foo", Function(&foo, 1)); 
ExternalFuncs ["foo"] (5); 
} 

調用堆棧:

#0 00000000 0x00000000 in ??() (??:??) 
#1 0041F209 fastdelegate::FastDelegate1<int, int>::operator() (this=0x3e256c, p1=5) (F:/Projekty/aaa/../libs/FastDelegate/FastDelegate.h:991) 
#2 0041D774 Function::operator() (this=0x3e256c, xxx=5) (F:\Projekty\aaa\main.cpp:14) 
#3 00401526 main() (F:\Projekty\aaa\main.cpp:34) 

回答

7
RegisterFunction ("Foo", Function(&foo, 1)); 
       ^capital F 
ExternalFuncs ["foo"] (5); 
       ^lowercase f 

由於在與該鍵"foo"地圖中沒有元素,ExternalFuncs["foo"]默認構造新Function,插入該默認構造對象到地圖,並返回提及它;然後您在該對象上調用operator(),該對象嘗試取消未初始化的Ptr成員變量的引用。壞事發生。

+0

Jest gites:D非常感謝。當有人寫這麼一小段代碼時,他沒有看細節:P – Xirdus 2010-09-28 16:34:46