The following forum(s) have migrated to Microsoft Q&A (Preview): Developing Universal Windows apps!
Visit Microsoft Q&A (Preview) to post new questions.

Learn More

 locked
invalid pointer address when calling socket from Background Task RRS feed

  • Question

  • After establishing a socket connection via Bluetooth I transfer the socket to the socket broker service. My receiving Background task is run when the SocketActivityTrigger detects activity on the socket. Now here is the problem: When I try to get the socket from 

    var details = taskInstance.TriggerDetails as SocketActivityTriggerDetails;
    StreamSocket socket = details.SocketInformation.StreamSocket;

    this results in an exception:

    The system detected an invalid pointer address in attempting to use a pointer argument in a call. (Exception from HRESULT: 0x8007271E)
    

    How can the socket not be valid if it responds to activity on the socket as expected?

    Monday, September 2, 2019 7:59 AM

Answers

  • Hi,

    I looked at your code, SocketActivityTrigger for network requests (http), and for Rfcomm, there is a better RfcommConnectionTrigger to choose from, this is a trigger for Rfcomm communication services.

    Here is the sample.

    Best regards.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Chris Morschel Tuesday, September 3, 2019 6:03 PM
    Tuesday, September 3, 2019 10:02 AM
  • Hi Chris,

    It should be a matter of type mismatch.

    There are still differences between connecting via Bluetooth and connecting via the network. SocketActivityTrigger and RfcommConnectionTrigger will cause disagreement when it comes to content parsing, which is why RfcommConnectionTrigger is recommended.

    Best regards.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Chris Morschel Tuesday, September 3, 2019 6:04 PM
    Tuesday, September 3, 2019 3:03 PM

All replies

  • Hi Chris,

    From the error message, you quoted an invalid address. Have you checked the Bluetooth connection information to make sure it is connected and the address is correct?

    If you still have problems, please provide a minimal runnable code that reproduces the problem, which will help us debug the problem.

    Best regards.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 2, 2019 9:23 AM
  • Hello,

    You didn't say which kind of thread you use for "Background task".

    For BackgroundWorker you need to create socket within BackgroundWorker - it running in a thread which are not accessible and this thread are different from one where instance of BackgroundWorker created.

    For a simple Thread - you may get access to socket, but need to remember that it still stay in different (taskInstance) thread.


    Sincerely, Highly skilled coding monkey.

    Monday, September 2, 2019 10:07 AM
  • Hi Richasy,

    Thank you for your response.

    I have a sample git project which produces this error:

    https://github.com/cmorschel/CommsHub

    Monday, September 2, 2019 11:50 AM
  • Hi Andrey,

    thank you for your response.

    The background task is a IBackgroundTask class in a WindowsRuntime component. I was following the Microsoft's example on "creating an out-of-process background task" and on "Network communication in the background"

    Here is a sample: https://github.com/cmorschel/CommsHub

    I hope this clarifies things a bit.

    Monday, September 2, 2019 12:00 PM
  • Hi,

    I looked at your code, SocketActivityTrigger for network requests (http), and for Rfcomm, there is a better RfcommConnectionTrigger to choose from, this is a trigger for Rfcomm communication services.

    Here is the sample.

    Best regards.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Chris Morschel Tuesday, September 3, 2019 6:03 PM
    Tuesday, September 3, 2019 10:02 AM
  • Hi Richasy,

    Thank you for your answer. I will probably implement this solution, but I still wonder why the SocketActivityTrigger fires but doesn't contain the socket information.

    Tuesday, September 3, 2019 12:04 PM
  • Hi Chris,

    It should be a matter of type mismatch.

    There are still differences between connecting via Bluetooth and connecting via the network. SocketActivityTrigger and RfcommConnectionTrigger will cause disagreement when it comes to content parsing, which is why RfcommConnectionTrigger is recommended.

    Best regards.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Chris Morschel Tuesday, September 3, 2019 6:04 PM
    Tuesday, September 3, 2019 3:03 PM