The data specified for the media type is invalid inconsistent or not supported by this object

Ali 0 Reputation points
2024-07-01T14:58:38.8666667+00:00

Hi,

I have an windows form application which is recording video by using Media Capture. This app is taking hundreds of record in a day but in a few days getting(once in 500-550 record) an error which below;

"The data specified for the media type is invalid inconsistent or not supported by this object"

Also, StartAsync throws this error.

I shared my code below;

mediacapture

Thanks.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,890 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,905 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 54,311 Reputation points
    2024-07-03T15:07:24.63+00:00

    As Jiale Xue mentioned, it is most likely a resource leak. The key indicators are:

    • works for several days and then starts erroring
    • using IDisposable objects and not cleaning them up

    Every instance you create of an IDisposable object must be cleaned up. If the object can be used for a long time then you can create it and ensure that it is cleaned up when it goes out of scope by the owning type. However in most cases you need to create the instance, use it and then clean it up yourself.

    using var someInstance = new SomeDisposableObject();
    someInstance...
    
    //Cleaned up when goes out of scope
    

    A clear indication you're doing something wrong is:

    • Storing an IDisposable object in a field of a class and the class doesn't implement IDisposable itself
    • Assigning a value to an IDisposable variable without first ensuring it is not set or cleaning up the previous object

    MediaCapture is one such type. It appears you're creating an instance and storing it in a field. Unless this is happening in the constructor of the class (in which case use a field initializer or lazy loading) then each time this block of code is called you are leaking the previous object. Hence a resource leak. You can quickly identify this situation by calling that method in a loop 1000s of times. It will eventually leak enough to fail. The correct approach here is to store the value in a local variable and wrap it in a using.

    using var mediaCapture = new MediaCapture();
    ...
    

    If you really, really the instance to persist for the life of the class instance it is stored in then create it in the field initializer expression or use lazy loading. Then the runtime will create the instance only once. However you also need to clean it up so the owning type needs to implement IDisposable as well.

    It doesn't appear that you are using any more IDisposable objects in this code but you should check the rest of your code to be sure. Note that there are analyzers that you should be running against your code. One of them should identify all the places you're not cleaning up these resources.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.