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 » TcpSocket in Array
TcpSocket in Array [message #37216] Thu, 06 September 2012 15:41 Go to next message
Zbych is currently offline  Zbych
Messages: 325
Registered: July 2009
Senior Member
Hi,

In my application I would like to keep all open sockets in an Array. The problem is that when I add (or maybe I should say 'move') TcpSocket to Array and TcpSocket gets out of scope, program crashes. Destructor tries to call ssl->Close() and ssl pointer has value 1.

CONSOLE_APP_MAIN
{
	Array<TcpSocket> a;
	
	{	
		TcpSocket s;
		a.Add(s);
	}//<---- crash


	//rest of code that uses TcpSockets collected in Array
}


The question is how should I add TcpSockets to Array to avoid this error?
Re: TcpSocket in Array [message #37217 is a reply to message #37216] Thu, 06 September 2012 16:09 Go to previous messageGo to next message
omari is currently offline  omari
Messages: 264
Registered: March 2010
Experienced Member
CONSOLE_APP_MAIN
{
	Array<TcpSocket> a;
	
	{	
		TcpSocket& s = a.Add();
		
	}

}


regards
omari.
Re: TcpSocket in Array [message #37218 is a reply to message #37217] Thu, 06 September 2012 17:14 Go to previous messageGo to next message
Zbych is currently offline  Zbych
Messages: 325
Registered: July 2009
Senior Member
I know this way, but want to keep in array only those sockets that are already accepted/open (another thread uses them for communication).

[Updated on: Thu, 06 September 2012 17:15]

Report message to a moderator

Re: TcpSocket in Array [message #37220 is a reply to message #37218] Thu, 06 September 2012 19:11 Go to previous messageGo to next message
omari is currently offline  omari
Messages: 264
Registered: March 2010
Experienced Member

CONSOLE_APP_MAIN
{
	Array<TcpSocket> a;
	
	{	
		TcpSocket s;

                if(..)
                   a.Add() = s;
		
	}

}



Note: the destructor TCPSocket can cause problems.

if this is the case, I recomande the first solution:

CONSOLE_APP_MAIN
{
	Array<TcpSocket> a;
	
	{	
		TcpSocket& s = a.Add();

		if (! ..)
		   a.Remove(a.GetCount()-1);
		
	}

}


regards
omari.

[Updated on: Thu, 06 September 2012 19:35]

Report message to a moderator

Re: TcpSocket in Array [message #37226 is a reply to message #37220] Fri, 07 September 2012 10:23 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
This would not work, as TcpSocket does not have any form of "copy".

Actually, this is sort of bug I have to fix (make copy constructor private).

I you really insist, you can use something like:

Array<TcpSocket> a;
TcpSocket *s = new TcpSocket;
s->...
if(...)
    a.Add(s);
else
    delete s;


anyway, that includes "new" which is not really "U++ish".

Other options:

One<TcpSocket> s;
s.Create();
s->...
if(...)
   a.Add(s.Detach());
// avoids need to delete (which you can forget to do ;)


TcpSocket& s = a.Add();
...
if(!...)
   a.Drop();


Mirek
Re: TcpSocket in Array [message #37238 is a reply to message #37226] Mon, 10 September 2012 13:42 Go to previous message
Zbych is currently offline  Zbych
Messages: 325
Registered: July 2009
Senior Member
Thank you both for suggestions. I guess that One<...> is the simplest solution in my case.
Previous Topic: Skylark logs failure in accept()
Next Topic: TcpSocket Tutorials exist?
Goto Forum:
  


Current Time: Thu Mar 28 17:06:24 CET 2024

Total time taken to generate the page: 0.01100 seconds