locked
Logging in the onsuspend handler

    Question

  • Before my application is suspended, I would like to commit any unwritten log messages to disk. For that I have a previously opened FileRandomAccessStream whose 'canRead' and 'canWrite' properties both report 'true'. But when calling writeAsync with the log message, an error is thrown: 0x80004003 - JavaScript runtime error: Invalid pointer.

    It seems like something in the suspend mechanism has already closed or otherwise invalidated that stream before I had a chance to write the remaining information. I already got a deferral from the suspend event which has not been completed yet. Is there anything else I can do?

    Wednesday, August 08, 2012 12:35 PM

Answers

  • Hi Jeff,

    actually I found a way to reproduce it - and with it a reason for the problem which I have to admit is quite embarrassing: to write to the stream, the text is encoded using CryptographicBuffer.convertStringToBinary. Now I found that this method returns null if the input string length is 0. And of course writeAsync doesn't work when passing in null as a buffer.

    I'm very sorry for having taken up your time for this stupid mistake. Seems like I couldn't see the forest through the trees with that one...

    All the best
    Marcus

    Wednesday, August 08, 2012 4:14 PM

All replies

  • Hi Marcus,

    From your description it seems like you are doing things correctly.  One thing however, even if you ask for the deferral you only have 5 seconds to complete your action.  Are you confident you are finishing fast enough?  Are writes to that same file elsewhere in your code fine?

    -Jeff


    Jeff Sanders (MSFT)

    Wednesday, August 08, 2012 12:39 PM
    Moderator
  • Hi Jeff,

    thanks for the quick reply! During normal operation, the log events are properly written to file. The onsuspend handler does very little work and completes very quickly, so the 5-second-limit shouldn't be a problem.

    I also tried moving the code into the 'checkpoint' event and used setPromise() but the problem remains the same.

    Strangely enough, when I copy&paste the code into another application, the error does not occur and the FileRandomAccessStream remains valid even after resuming the app. Thus I'm a bit mystified and will need to do some more investigation.

    Wednesday, August 08, 2012 2:18 PM
  • Hi Marcus,

    Your best bet to get an idea of what the file handle state is will be to use process monitor and or process explorer.  You can get them both here:

    http://technet.microsoft.com/en-us/sysinternals/bb795533.aspx

    -Jeff


    Jeff Sanders (MSFT)

    Wednesday, August 08, 2012 2:20 PM
    Moderator
  • Hi Jeff,

    thank you for the tip! Watching wwahost.exe in Process Monitor I found that there is no CloseFile happening on the log file anywhere during the suspend process. After resuming the process, writing to the file works again. It is only from within the checkpoint or suspend handler that FileRandomAccessString throws the 'invalid pointer' COM exception.

    Wednesday, August 08, 2012 2:56 PM
  • So it sounds like there is something specific to your app that is going awry.  Can you contact me and arrange to transfer your repro to me?

    http://blogs.msdn.com/jpsanders/contact.aspx


    Jeff Sanders (MSFT)

    Wednesday, August 08, 2012 3:06 PM
    Moderator
  • Hi Jeff,

    actually I found a way to reproduce it - and with it a reason for the problem which I have to admit is quite embarrassing: to write to the stream, the text is encoded using CryptographicBuffer.convertStringToBinary. Now I found that this method returns null if the input string length is 0. And of course writeAsync doesn't work when passing in null as a buffer.

    I'm very sorry for having taken up your time for this stupid mistake. Seems like I couldn't see the forest through the trees with that one...

    All the best
    Marcus

    Wednesday, August 08, 2012 4:14 PM
  • Hi Marcus,

    No reason whatsoever to apologize, we ALL make mistakes.  I am happy you were able to resolve the issue!

    -Jeff


    Jeff Sanders (MSFT)

    Wednesday, August 08, 2012 5:08 PM
    Moderator