DeusAduro
DeusAduro

Reputation: 6076

Issue with Unmanaged callback in Windows Forms. Access Violation

I am having an issue getting a third party .dll which uses unmanaged callbacks to work in a c# forms project. Initially the program would crash after the first call to the callback. However, the response here helped me solve that issue (Cdecl was the right calling convention). The callback definition in C# looks like:

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate void FrameDelegate(IntPtr lpContext, IntPtr bpData, int nDataSize, int nChannel, int isKeyFrame, int nFrameCnt);

The actual callback looks like:

    public void FrameDelegate(IntPtr lpContext, IntPtr bpData, int nDataSize,
        int nChannel, int isKeyFrame, int nFrameCnt)
    {
        lock (ChannelStats)
        {
            ChannelStats.TotalSize += nDataSize;
            ChannelStats.CurrentFileSize += nDataSize;
            ChannelStats.FrameCount++;
        }
        UpdateStatistics();
    }

And the update statistics function:

    private void UpdateStatistics()
    {
        if (this.InvokeRequired)
        {
            this.Invoke(new UpdateDelegate(UpdateStatistics));
        }
        else
        {
            lock (ChannelStats)
            {
                FrameCapturedLabel.Text = string.Format("Frames Captured: {0}", ChannelStats.FrameCount);
                TotalCapturedLabel.Text = string.Format("Total Captured: {0} [MB]", (double)ChannelStats.TotalSize / 1000000.0);
                CurrentFileLabel.Text = string.Format("Current File Size: {0} [MB]", (double)ChannelStats.CurrentFileSize / 1000000.0);
                FileCountLabel.Text = string.Format("File Count: {0}", ChannelStats.FileCount); 
            }                      
        }
    }

The code runs successfully for a random number of frames (usually 5-15) before it crashes with an access violation exception. Unfortunately this is being run on a remote, embedded system and as such I don't have my debugger handy to help me. Also of note:

So my question: Any hints on what might be causing an access violation? Is the locking in the code reasonable, or is it totally off?

Any input is much appreciated!

Edit

The ChannelStats variable is defined as follows:

    private class RecordStatistics
    {
        public Int64 TotalSize { get; set; }
        public Int64 CurrentFileSize { get; set; }
        public Int64 FileCount { get; set; }
        public Int64 FrameCount { get; set; }

        public RecordStatistics()
        {
            TotalSize = 0;
            CurrentFileSize = 0;
            FileCount = 0;
            FrameCount = 0;            
        }
    }

    RecordStatistics ChannelStats;

Upvotes: 0

Views: 363

Answers (1)

Vlad Bezden
Vlad Bezden

Reputation: 89677

What is ChannelStats? Is that your class? It is better if you do locking on separate object.

private static object _thisLock = new object();

lock (_thisLock)
{
    // Critical code section
}

Upvotes: 1

Related Questions