2010-09-28 9 views
0

J'ai un problème avec mon code de test. Il compile bien, mais quand j'essaie d'appeler un délégué, le programme se bloque.Erreur de segmentation avec 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); 
} 

Callstack:

#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) 

Répondre

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

Comme il n'y a aucun élément dans la carte avec la "foo" clé, ExternalFuncs["foo"] défaut construit un nouveau Function, insère cet objet construit par défaut dans la carte, et retourne une référence à celui-ci; vous appelez ensuite operator() sur cet objet, qui tente de déréférencer la variable membre Ptr non initialisée. De mauvaises choses arrivent.

+0

Gîtes Jest: D Merci beaucoup. Quand quelqu'un écrit si peu de code, il ne regarde pas les détails: P – Xirdus

Questions connexes