Xir*_*dus 0 c++ crash delegates stl segmentation-fault
我的测试代码有问题.它编译得很好,但是当我尝试调用delegate时,程序崩溃了.
#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);
}
Run Code Online (Sandbox Code Playgroud)
调用堆栈:
#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)
Run Code Online (Sandbox Code Playgroud)
RegisterFunction ("Foo", Function(&foo, 1));
^ capital F
ExternalFuncs ["foo"] (5);
^ lowercase f
Run Code Online (Sandbox Code Playgroud)
由于在与该键的地图中没有元素"foo",ExternalFuncs["foo"]默认构造新的Function,插入该默认构造对象到地图,并返回对它的引用; 然后调用operator()该对象,该对象尝试取消引用未初始化的Ptr成员变量.坏事发生了.
| 归档时间: |
|
| 查看次数: |
425 次 |
| 最近记录: |