Assume that there’s no problem with my header file and some included library. I’m using Winsock2.
//main.cpp and this code is okie, I omit some codes for readable reason
SOCKET ClientSocket;
ClientSocket = INVALID_SOCKET;
ClientSocket = accept(ListenSocket, NULL, NULL);
cout <<"a client is connected" << endl;
ClientConnection connection(ClientSocket);
connection.run();
//ClientConnection.h I tested any function and it's okie
class ClientConnection{
private:
SOCKET connectedSocket;
bool connected;
public:
ClientConnection(const ClientConnection &con);
ClientConnection(SOCKET s);
int processMessage(string message);
void sendMessage(string message);
string recvMessage();
int run();
void printSomething(); //function just print something such as :"Here"
};
In run() function, I call the recvMessage() to get the message from client then I call processMessage(message). This is my processMessage(message) and sendMessage:
void ClientConnection::sendMessage(string mess){
const char* sendBuff = mess.c_str();
int error = send(this->connectedSocket,sendBuff, (int)strlen(sendBuff), 0);
delete sendBuff;
return ;
}
int ClientConnection::processMessage(string message){
cout << "This is message from client:\n" << message << endl;
ImageProcessingPlugin *plugin = new ImageProcessingPlugin();
plugin-> processMessage( *this, message);
delete plugin;
return 0;
}
This is ImageProcessingPlugin:
// ImageProcessingPlugin.h
class ImageProcessingPlugin : public Plugin{
public:
ImageProcessingPlugin();
void processMessage(const ClientConnection &connection, string message);
};
// ImageProcessingPlugin.cpp
void ImageProcessingPlugin::processMessage(const ClientConnection& _connection, string message){
ClientConnection connection(_connection);
connection.printSomething() // It's okie
connection.sendMessage("Server received"); // Problem goes here!!
return;
}
After debugging, when I call printSomething() in plugin->processMessage, it’s still ok, but when I go to sendMessage, the client still got the message “Server received” from server, but there was an error: “_Block_Type_IS_VALID(pHead->nBlockUse)”! Anyone can solve my problem?
Your deleting
delete sendBuff;here, but you never allocated the memory yourself. Try removing that line.