Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ MT-multithreading and servers » error / memory leak in HttpServer example
error / memory leak in HttpServer example [message #59952] Mon, 12 June 2023 17:29 Go to next message
BetoValle is currently offline  BetoValle
Messages: 203
Registered: September 2020
Location: Brasil Valinhos SP
Experienced Member
Hi,

After i run the HttpServer example (reference Assembly) and when I close the window the application show 20 lines of memory leak!

If i put Server.Close() in line 30 (HttpServer) it displays error Assertion failed in (...\Core\Socket.cpp, line 382)
ls.IsOpen()

I don't know if there was carelessness in the example when shutting down the server or if this should be corrected!

Thanks!
Re: error / memory leak in HttpServer example [message #59954 is a reply to message #59952] Tue, 13 June 2023 16:11 Go to previous message
BetoValle is currently offline  BetoValle
Messages: 203
Registered: September 2020
Location: Brasil Valinhos SP
Experienced Member
Hi,
I made some changes and it worked by terminating from Ctrl+c (or via uri)
(windows 10!)
Tks!

#include <Core/Core.h>
#include <signal.h>
using namespace Upp;

TcpSocket server;
StaticMutex ServerMutex;

void signal_callback_handler(int signum)
{
	Cout() << "Caught signal " << signum << EOL;
	// Terminate program
	exit(signum);
}

void Server()
{
	bool stop = false; // Of course it is not recommended to manipulate the server through
	                   // the client! This condition is for experimental testing purposes!
	for(;;) {

		TcpSocket socket;
		LOG("Waiting...");
		ServerMutex.Enter();

		bool b = socket.Accept(server);
		ServerMutex.Leave();

		if((b) && stop == false) {
			LOG("Connection accepted");
			HttpHeader http;
			http.Read(socket);
			String html;
			html << "<html>"
				 << "<b>Method:</b> " << http.GetMethod() << "<br>"
				 << "<b>URI:</b> " << http.GetURI() << "<br>";
			stop = (http.GetURI().Find("stop=ok") > -1);
			if(stop)
				html << "<p>atenção! vai encerrar o servidor!</p>"
					 << "<br>";
			for(int i = 0; i < http.fields.GetCount(); i++)
				html << "<b>" << http.fields.GetKey(i) << ":</b> " << http.fields[i] << "<br>";
			int len = (int)http.GetContentLength();
			if(len > 0)
				socket.GetAll(len);
			html << "<b><i>Current time:</i></b> " << GetSysTime() << "</html>";
			HttpResponse(socket, http.scgi, 200, "OK", "text/html", html);
			if(stop) {
				socket.Close();
				server.Close();
				exit(0);
			}
			signal(SIGINT, signal_callback_handler);
		}
	}
}

CONSOLE_APP_MAIN
{

	StdLogSetup(LOG_COUT | LOG_FILE);

	if(!server.Listen(4000, 10)) {
		LOG("Cannot open server port for listening\r\n");
		return;
	}
#ifdef _MULTITHREADED
	const int NTHREADS = 10;
	for(int i = 0; i < NTHREADS; i++)
		Thread::Start(callback(Server));
#endif
	Server();
}

Previous Topic: Problem breaking loop (with close button) in main thread
Next Topic: fileupload and post data
Goto Forum:
  


Current Time: Sun Jun 23 09:33:22 CEST 2024

Total time taken to generate the page: 0.01968 seconds