get error pure virtual method called terminate called without an active exception when use I thread

I want to use thread in my Client program to run functions together but I get this error pure virtual method called terminate called without an active exception and the program stops,why?

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
  bool setup::conn() {

	struct sockaddr_in serv_addr;
	struct hostent *server;

	sock = socket(AF_INET, SOCK_STREAM, 0);
	if (sock == -1){
		printf("ERROR opening socket");
		fflush(stdout);
	}

	server = gethostbyname("192.168.1.113");
	if (server <= 0) {
		fprintf(stderr, "ERROR, no such host\n");
		fflush(stdout);
	}
	bzero((char *) &serv_addr, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	bcopy((char *) server->h_addr,
	(char *)&serv_addr.sin_addr.s_addr,
	server->h_length);
	serv_addr.sin_port = htons(MYPORT);

	while (true) {
		fflush(stdout);
		csock = (int*) malloc(sizeof(int));
		*csock = connect(sock, (struct sockaddr *) &serv_addr,
				sizeof(serv_addr));
		if (*csock == -1) {
			perror("error");
			if (errno == EISCONN  )
				break;
			

		}

		thread thread1;
		thread1 = thread(&setup::SocketHandler, this);
		 thread1.join();
     }
}
void setup::SocketHandler() {

	while (1) {

		if ((bytecount = recv(*csock, buffer, buffer_len, 0)) == -1)
			fprintf(stderr, "Error receiving data %d\n", errno);

		if (bytecount > 0) {
			printf("\n Received bytes: %d ", bytecount);
			
		}
                fflush(stdout);
		usleep(100);

	}
	pthread_exit(0);

}
Last edited on
You've not shown the declarations of the variables used. Without the types ...

Why are you allocating memory for the client socket? Why not just use an int?
Is setup::conn() being called from setup's constructor? If setup::SocketHandler() is a virtual function then that's going to fail; you can't make virtual calls into this from the constructor.
this is my setup.h

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
#include <time.h>
#include <fcntl.h>
#include <errno.h>
#include <resolv.h>
#include <thread>
....

#define MAXTIMINGS 85
#define MYPORT 3333
using namespace std;
class setup {
public:
	setup();
	void SocketHandler();
	void closeSocket();
	void set();
	bool conn();
	virtual ~setup();

	char buffer[1024];
	int buffer_len = 1024;
	int bytecount, bytecnt;
	int* csock;
        ....

};

#endif  



I do this but I get previous error again

1
2
3
setup s1;
std::thread t1 (&setup::SocketHandler, &s1);
 t1.join();


Last edited on
std::thread will takes a function and its arguments. But you're passing is a class method that takes no parameters.

When SocketHandler() runs, it's really doing this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void setup::SocketHandler() {
	while (1) {
		if ((this->bytecount = recv(*this->csock, this->buffer, this->buffer_len, 0)) == -1)
			fprintf(stderr, "Error receiving data %d\n", errno);

		if (this->bytecount > 0) {
			printf("\n Received bytes: %d ", bytecount);
			
		}
                fflush(stdout);
		usleep(100);
	}
	pthread_exit(0);
}

And this is uninitialised.

Conceptually, you wouldn't run one member method in a different thread. You've have to specifically choose such a method and then design it to be run that way. You haven't done any of that. The whole thing's a mess
std::thread will takes a function and its arguments. But you're passing is a class method that takes no parameters.
Doesn't the std::thread constructor understand member function pointers? I believe std::bind() will also accept binding this with the same syntax.

isan:
Is setup::conn() being called from setup's constructor? If setup::SocketHandler() is a virtual function then that's going to fail; you can't make virtual calls into this from the constructor.
Topic archived. No new replies allowed.