locked
Possible causes of COMException ?

    Question

  • Hi,

    When I try to send messages to datagramsocket using datawriter class, I am getting a COMException, the message property of exception gives the message as "The Object has been closed". I am able to send message one time, second time it is giving the exception. Moreover I am not getting the MessageReceived event after sending the first message. I included the code below

    DataWriter^writer=refnewDataWriter(((DatagramSocket^)(sock))->OutputStream);

    Platform::Array<unsignedchar,1>^refarray=refnewPlatform::Array<unsignedchar,1>(((unsignedchar*)data),size);

    writer->WriteBytes(refarray);

    (writer->StoreAsync())->Completed=refnewAsyncOperationCompletedHandler<unsignedint>(AsyncOperationCompletedHandlerSock);

    Please give some solution

    Thanks

    AKEV




    • Edited by AKEV Monday, December 3, 2012 11:52 AM
    Monday, December 3, 2012 11:49 AM

All replies

  • I am using the below code to send data using DatagramSocket and I am able to receive MessageReceived event without any issues. Can u please check this?

    	DatagramSocket^ m_Socket;
    	
    	m_Socket= ref new DatagramSocket();
    
    	task<void>(m_Socket->BindEndpointAsync( Localhost,"" )).then([this] (task<void> previousTask)
    	{	
    	m_Socket->MessageReceived += ref new TypedEventHandler<DatagramSocket^, DatagramSocketMessageReceivedEventArgs^>(this, &MainPage::OnMessageReceived);
    
    			task<IOutputStream^>(m_Socket->GetOutputStreamAsync( Host, port)).then([this] ( task<IOutputStream^> stream )
    			{
    				try  
    				{
    						IOutputStream^ MyStrem = stream.get();
    						Platform::Array<unsigned char>^ Packet= { //data//};
    						
    						DataWriter^ Writer = ref new DataWriter( MyStrem );	
    						Writer->WriteBytes( Packet );
    
    						task<unsigned int>(Writer->StoreAsync()).then([this]( task<unsigned int> WriteTask )						
    						{
    							try
    							{
    								WriteTask.get();
    								task<bool>(m_Socket->OutputStream->FlushAsync()).
    								then([this](task<bool> bVal)
    								{
    									try
    									{
    										bVal.get();
    									}
    									catch(Exception^ e)
    									{
    										String^ ErrorMsg = e->Message;	
    									}
    								});
    							}
    							catch( Exception^ Exp )
    							{
    								String^ ErrorMsg = Exp->Message;
    							}
    					});
    				}
    				catch( Exception^ Exp )
    				{
    					String^ ErrorMsg = Exp->Message;
    				}
    			});
    	});
    

    Wednesday, December 5, 2012 4:49 AM
  • I had a similar issue while using StreamSocket and after debugging the assembly I found that the ref-counting was deleting the OutputStream reference.

    I have declared the OutputStream as a class member to hold at least one reference to it and workaround this issue.

    Wednesday, December 5, 2012 6:03 AM