Segmentation Fault

I can't work out why I get a Segmentation Fault with my C++ program. Could please guide me to an answer.

Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <string>
#include <stdexcept>

class CallbackWrapper
{
	public:
		virtual ~CallbackWrapper() { };
		virtual void Call() = 0;
};

template <typename MT> class CallbackFunction : public CallbackWrapper
{
	typedef void (*CALLBACK)(MT);
	CALLBACK func;
	MT data;
	public:
		CallbackFunction() { };
		void SetData(void (*f)(MT), MT d)
		{
			func = f;
			data = d;
		}
		void Call()
		{
			func(data);
		}
};

class Callback
{
	CallbackWrapper *CallbackClass;
	public:
		Callback()
		{
			CallbackClass = NULL;
		}
		~Callback()
		{
			delete CallbackClass;
		}
		template <typename T> void SetCallback(CallbackFunction <T> *func)
		{
			CallbackClass = func;
		}
		void Call()
		{
			CallbackClass->Call();
		}
};

template <typename CT> Callback NewCallback(void (*func)(CT), CT data)
{
	CallbackFunction <CT> *cf = NULL;
	cf->SetData(func, data);
	Callback cb;
	cb.SetCallback(cf);
	return cb;
};

void Call(Callback CallbackFunc)
{
	CallbackFunc.Call();
}

void foo(std::string str)
{
	std::cout << str << "\n";
}

int main()
{
	std::string str;
	str.append("Hello, World!");
	Call( NewCallback(&foo, str) );
	return 0;
}


Thanks in advance!
1
2
CallbackFunction <CT> *cf = NULL;
cf->SetData(func, data);

You are trying to access a null pointer. Allocate memory for the object with new and it should work

1
2
CallbackFunction <CT> *cf = new CallbackFunction <CT> ;
cf->SetData(func, data);
Wow, I did not think of that, thank you.
OK, So I've finished my little callback system.

Thought I'd share it for any future thread viewers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <string>

class CallbackWrapper
{
    public:
        virtual ~CallbackWrapper() { };
        virtual void Call() = 0;
};

template <typename MT> class CallbackFunction : public CallbackWrapper
{
    typedef void (*CALLBACK)(MT);
    CALLBACK func;
    MT data;
    public:
        CallbackFunction() { };
        void SetData(void (*f)(MT), MT d)
        {
            func = f;
            data = d;
        }
        void Call()
        {
            func(data);
        }
};

class Callback
{
    CallbackWrapper *CallbackClass;
    public:
        Callback() { };
        ~Callback()
        {
            delete CallbackClass;
        }
        template <typename T> void SetCallback(CallbackFunction <T> *func)
        {
            CallbackClass = func;
        }
        void Call()
        {
            CallbackClass->Call();
        }
};

template <typename CT> Callback NewCallback(void (*func)(CT), CT data)
{
    CallbackFunction <CT> *cf = new CallbackFunction <CT>;
    cf->SetData(func, data);
    Callback cb;
    cb.SetCallback(cf);
    return cb;
};

void Call(Callback CallbackFunc)
{
    CallbackFunc.Call();
}

void foo(std::string str)
{
    std::cout << str << "\n";
}

int main()
{
    std::string str;
    str.append("Hello, World!");
    Call( NewCallback(&foo, str) );
    return 0;
}
OK, So I've finished my little callback system.


Where is the copy constructor and copy assignment operator for Callback? You are currently double-deleting the same pointer which results in undefined behavior (which manifests as no output and a segmentation fault for me.)
Huh, I works perfectly for me, I get no errors/seg faults at all on Linux.

Could you please show me how you do this.
Topic archived. No new replies allowed.