jellyfin / TMDbLib

C#.Net library for TheMovieDB
MIT License
344 stars 128 forks source link

NullReference from the library from Rest.RestResponse.Dispose() #405

Closed SirSparkles closed 2 years ago

SirSparkles commented 2 years ago

Recently there has been a flurry of NREs coming out of the interface. All from the Dispose() method:

TMDB:929282 [Halo: The Fall of Reach]

System.NullReferenceException: Object reference not set to an instance of an object. at TMDbLib.Rest.RestResponse.Dispose() at TMDbLib.Client.TMDbClient.d__140.MoveNext()

--- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at TMDbLib.Client.TMDbClient.d__139.MoveNext()

SirSparkles commented 2 years ago

System.AggregateException: One or more errors occurred. ---> System.NullReferenceException: Object reference not set to an instance of an object. at TMDbLib.Rest.RestResponse.Dispose() at TMDbLib.Rest.RestRequestExtensions.d__1`1.MoveNext()

--- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at TMDbLib.Client.TMDbClient.d__218`1.MoveNext()

--- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at TMDbLib.Client.TMDbClient.d__238.MoveNext()

 --- End of inner exception stack trace ---  ---> 

(Inner Exception #0) System.NullReferenceException: Object reference not set to an instance of an object. at TMDbLib.Rest.RestResponse.Dispose() at TMDbLib.Rest.RestRequestExtensions.d__1`1.MoveNext()

--- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at TMDbLib.Client.TMDbClient.d__218`1.MoveNext()

--- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at TMDbLib.Client.TMDbClient.d__238.MoveNext()

<--- ---> (Inner Exception #1) System.NullReferenceException: Object reference not set to an instance of an object. at TMDbLib.Rest.RestResponse.Dispose() at TMDbLib.Rest.RestRequestExtensions.d__1`1.MoveNext()

--- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at TMDbLib.Client.TMDbClient.d__218`1.MoveNext()

--- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at TMDbLib.Client.TMDbClient.d__236.MoveNext()

<---

carlosmanri commented 2 years ago

I have the same issue.

System.NullReferenceException HResult=0x80004003 Mensaje = Object reference not set to an instance of an object. Origen = TMDbLib Stack trace: at TMDbLib.Rest.RestResponse.Dispose() at TMDbLib.Client.TMDbClient.d__213.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() ...

carlosmanri commented 2 years ago

I just realized that checking if its null before disposing the response in RestResponse.Dispose() solves the problem. I dont know why is getting disposed before that.


        [SuppressMessage("IDisposableAnalyzers.Correctness", "IDISP007:Don't dispose injected.", Justification = "RestResponse owns the response")]
        public virtual void Dispose()
        {
            Response?.Dispose(); // <--there
        }
LordMike commented 2 years ago

Well, if it's null, it's always been null - so it must be null when it's in the ctor.

LordMike commented 2 years ago

Ok, so I can see that the rest client will return null in some cases, like 404 - which then yields an !IsValid rest response, which then cannot be disposed. That's a woops.

LordMike commented 2 years ago

I have pushed 1.9.2 which includes the null check proposed by @carlosmanri - hope this works out for all of you :)