Hello Everyone,

As you can see, I am new to DaniWeb, and might I say this is a VERY good community. Either ways, my problem is a pretty complicated one, which I have been working on for a while now, 2-3 weeks. I am assigned by my team to create a dashboard for our robot, basically a window on a computer that will have speedometers, RPM gauges, voltage gauges, odometers, things of that nature. The entire network code is done, the server is in C++ and the client is in C#,nthe client is multi threaded and connects to the server, receive data, and it can then reset and do everything over. My problem is that everytime I run the server, which is just in testing, it sends the packets, the client updates, the server terminates as it should, (It closes all the sockets and open connection), and right when the program should exit, it gives an error. An inhandled win32 exception, and when I click debug, it takes me to free.h, to a line that is:

if(retval == 0)

I have no clue what to do, as the packets are sent properly, updated, the server terminates, and when there is ABSOLUTELY no code left to process, it gives me an error. I dont htink the client is necessary for display, but here is my server code

DashboardConnecter.h

/* Team 263 Dashboard - Server
 * Created by Pranav Sathyanarayanan
 * ------------------------------------------
 * DashboardConnecter.h
 * January, 2010
*/
#include <iostream>
#include <sstream>
#include <string>

#include "WINSOCK2.H"

using namespace std;

#pragma comment (lib, "WS2_32.LIB")

#define INTERVAL 1000

#define SPEED_X "speedx"
#define SPEED_Y "speedy"
#define VOLTAGE "voltage"
#define RPM "rpm"
#define EXIT "exit"

#pragma once
class DashboardConnecter
{
	private:
		WSADATA data;
      	SOCKET sock, tmp_sock;
      	SOCKADDR_IN connInfo, remote_info;
      	int cLen;
		char* buff;

		string convertDouble(double value) 
		{
 			stringstream o;
  			if (!(o << value))
    			return "";
			return o.str();
		}

	public:

		DashboardConnecter(void)
		{
		}

		void Initialize(char* connIP, int connPort)
		{
			if(WSAStartup(MAKEWORD(2,2), &data))
      		{
            		return;
      		}
      
      		sock = socket(AF_INET, SOCK_STREAM, 0);

      		if(sock == SOCKET_ERROR)
     		{
            	return;
      		}

      		connInfo.sin_family = AF_INET;
      		connInfo.sin_port = htons(connPort);
	  		connInfo.sin_addr.S_un.S_addr = inet_addr(connIP);
      
      		if(bind(sock, (SOCKADDR*) &connInfo, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
      		{
            	return;
      		}
		}

		void Listen()
		{
			int bytesRecieved = 0;
			if(listen(sock, 1)  == SOCKET_ERROR)
      		{
            	return;
      		}
		
	  		tmp_sock == SOCKET_ERROR;
      		do
      		{
            	tmp_sock = accept(sock, (SOCKADDR*) &remote_info, &cLen);
      		}
      		while(tmp_sock == SOCKET_ERROR);
	  		sock = tmp_sock;

			cout << "Connected!" << endl;
	  	}

		void SendSpeedX(double speed)
		{
			string conc;
			char* tosend;
			stringstream o;

  			if (!(o << speed))
    			return;

			conc = o.str();

			tosend = new char[conc.length()];

			sprintf(tosend, "%s:%s", SPEED_X,conc.c_str());
			cout << "tosend: " << tosend << endl;
	
			send(sock, tosend, strlen(tosend), 0);

			conc.clear();
			free(tosend);
		}

		void SendSpeedY(double speed)
		{
			string conc;
			char* tosend;
			stringstream o;

  			if (!(o << speed))
    			return;

			conc = o.str();
			tosend = new char[conc.length() + 1];
			sprintf(tosend, "%s:%s", SPEED_Y,conc.c_str());
			cout << "tosend: " << tosend << "\n";

			send(sock, tosend, strlen(tosend), 0);

			conc.clear();
			o.clear();
			delete[] tosend;
		}

		void SendVoltage(double volts)
		{
			string conc;
			char* tosend;
			stringstream o;

  			if (!(o << volts))
    			return;

			conc = o.str();
			tosend = new char[conc.length() + 1];
			sprintf(tosend, "%s:%s", VOLTAGE,conc.c_str());
			cout << "tosend: " << tosend << "\n";

			send(sock, tosend, strlen(tosend), 0);

			conc.clear();
			o.clear();
			delete[] tosend;
		}

		void SendRPM(double rpm)
		{
			string conc;
			char* tosend;
			stringstream o;

  			if (!(o << rpm))
    			return;

			conc = o.str();
			tosend = new char[conc.length() + 1];
			sprintf(tosend, "%s:%s", RPM,conc.c_str());
			cout << "tosend: " << tosend << "\n";

			send(sock, tosend, strlen(tosend), 0);

			conc.clear();
			free(o);
			free(tosend);
		}
		
		void Terminate()
		{
			send(sock, EXIT, strlen(EXIT), 0);
			try
			{
				closesocket(sock);
      			closesocket(tmp_sock);
      			WSACleanup();
			}
			catch(char* str)
			{

			}
		}

		~DashboardConnecter(void)
		{
		}
};

Server.cpp

/* Team 263 Dashboard - Server
 * Created by Pranav Sathyanarayanan
 * ------------------------------------------
 * Server.cpp
 * January, 2010
*/
#include "DashboardConnecter.h"
#include <iostream>
#include <string>

void main(int argc)
{
      DashboardConnecter connection;

	  connection.Initialize("192.168.2.3", 1165);
	  connection.Listen();

	  Sleep(INTERVAL);
	  connection.SendSpeedX(1.63);
	  Sleep(INTERVAL);
	  connection.SendSpeedY(7.89);
	  Sleep(INTERVAL);
	  connection.SendVoltage(4.22);
	  Sleep(INTERVAL);
	  connection.SendRPM(100.93);
	  Sleep(INTERVAL);
	  connection.Terminate();
	  exit(0);
}

The DashboardConnecter.h is where the socket is made, binded and listenes, then it is also where the sending and stuff happens. Server.cpp is just used to test my custom class(DashboardConnecter.h , which is a class btw). Can anyone please tell me how to fix this problem. Thank You!

- Pranav Sathyanarayanan

SOLVED! - I managed to fix this myself, it seems that the string.c_str() method, particularly in the line,

sprintf(tosend, "%s:%s", RPM, conc.c_str());

, where RPM could have been any other value, was messing up char* tosend. I removed the char*, and I amde one string final = to the gauge to change, and then appended the final string with the value to set the gauge to.

The problem is that you mix delete and free (in function SendRPM() for example). If you allocate memory with new you need to delete it. If you allocate with malloc you need to free it. Don't mix'em :)

[edit]
and you're free'ing 'o' which is a stringstream. You don't have to free a stringstream.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.