SessionManager.Dispose() in C#

119 views
Skip to first unread message

Asim

unread,
Dec 17, 2010, 2:01:39 AM12/17/10
to OpenNI
I'm using ManagedNite.dll... Was trying to make an application that
could use the Context with a switch. When switch is on, the context is
created and is in session, and when switch is turned off, the context
and session are ended and disposed.

Somehow, the statement sessionMgr.dispose() throws an AccessViolation
exception. I'm running a background worker to update the context and
session manager. And when the while loop in the worker ends, the
objects need to be closed and disposed.

Here's what the detail looked like : System.AccessViolationException
---> {"Attempted to read or write protected memory. This is often an
indication that other memory is corrupt."}

Why is this happening....???

sharon brizinov

unread,
Dec 17, 2010, 9:02:19 AM12/17/10
to OpenNI
I saw your E-mail but I prefer to discuss it over here so other people
with similar problem will be able to solve it.
I think I know what is the problem:
Before disposing the Context and the SessionManager you need to do the
following:
1) In the running thread - false the while loop so it won't run-
while(false).

2) Abort the thread -
thread.Abort();

3) Dispose the Context and the SessionManager.

I believe the error occurs because your are trying to dispose those
object while the thread is still running and uses them.

Update me if it works.

blurg

unread,
Feb 18, 2011, 7:00:48 AM2/18/11
to openn...@googlegroups.com
Hi,

I'm facing the same problem with the new C# NITE wrapper (XnVNITE.net.dll).
I've attached a WPF application that helps to reproduce the problem.
Button start instanciate Context, SessionManager and a Task (System.Threading.Tasks) to perform the update :

            // Context
            string openNIXmlFilePath = Path.Combine("Data", "openni.xml");
            _openNIContext = new xn.Context(openNIXmlFilePath);

            //Instanciate MiddleWare
            _niteSessionManager = new xnv.SessionManager(_openNIContext, "Wave", "RaiseHand");

            // Make sure all generators are generating data
            _openNIContext.StartGeneratingAll();

            //init ok => start update task
            _updateTask = new Task(() => Update());
            _updateTask.Start();
            _isUpdating = true;

Button stop is supposed to dispose all the resources :

   _isUpdating = false;

            if (_updateTask != null)
            {
                try
                {
                    _updateTask.Wait();
                }
                finally
                {
                    _updateTask.Dispose();
                    _updateTask = null;
                }
            }

            _openNIContext.StopGeneratingAll();

            if (_niteSessionManager != null)
            {
                //How to dispose properly ??
                _niteSessionManager = null;
            }

            if (_openNIContext != null)
            {
                _openNIContext.Shutdown();
                _openNIContext.Dispose();

                _openNIContext = null;
            }
After playing and stopping a few times (between 2 and 8 switches) an exception System.AccessViolationException {"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."} is thrown, at xnv.NITEImporter.xnvSessionManager_Destroy(UInt32 id) at xnv.SessionManager.Finalize()

Is there something I did in the wrong order ? What is the proper way to clean NITE SessionManager to be able to restart a new one ?

Thanks
Stop_Start_NiteSession.zip
Reply all
Reply to author
Forward
0 new messages