Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8752237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:11:42+00:00 2026-06-13T13:11:42+00:00

I’ve read solutions to similar problems, but I’m having trouble getting them to work

  • 0

I’ve read solutions to similar problems, but I’m having trouble getting them to work in my case. I’m getting the following error on a C# .NET 4.0 project running in Visual Studio 2010:

CallbackOnCollectedDelegate was detected Message: A callback was made
on a garbage collected delegate of type
‘VLCMTest!VLCMTest.Data+VCECLB_GrabFrame_CallbackEx::Invoke’. This may
cause application crashes, corruption and data loss. When passing
delegates to unmanaged code, they must be kept alive by the managed
application until it is guaranteed that they will never be called.

Here is my situation: I have a background thread that is notified when a frame of data is collected.

protected AutoResetEvent frameGrabbed;
public event EventHandler<DataFrameInfo> FrameGrabbedEvent;

    private void DataCollectionThread()
    {
        while (true)
        {
            frameGrabbed.WaitOne();

            lock (locker)
            {
                FrameGrabbedEvent(this, new DataFrameInfo(lastFrame.BufferIndex, lastFrame.FrameNumber, lastFrame.FrameTimestamp));
            }
        }
    }

The DataFrameInfo class stores some information about the frame (index into the frame buffers, frame number, and timestamp). I create an instance of this class and pass it to the main thread so the data can be displayed. The code in the main thread looks like this:

    // Delegate for the Invoke call, 
    // make it static to prevent a problem with garbage collection
    delegate void GetFrameDelegate(DataFrameInfo frameInfo);
    private static GetFrameDelegate d;

    /// <summary>
    /// Did we just receive a frame?
    /// </summary>
    /// <param name="source"></param>
    /// <param name="args"></param>
    void frameGrabbed(object source, DataFrameInfo args)
    {
        if (this.InvokeRequired)
        {
            // It's on a different thread, so use Invoke.
            d = new GetFrameDelegate(GetFrame);
            this.Invoke(d, new object[] { args });
            return;
        }

        // Get the Frame
        GetFrame(args);
    }

    private void GetFrame(DataFrameInfo frameInfo)
    {
        // Call Display Frame
        Debug.WriteLine("Frame: The bufferIndex is " + frameInfo.BufferIndex);
        Debug.WriteLine("Frame: The number is " + frameInfo.FrameNumber);
        Debug.WriteLine("Frame: The timestamp is " + frameInfo.FrameTimestamp / 1000);
    }

The FrameGrabbedEvent is connected to the frameGrabbed() function. Since frameGrabbed() is being called from another thread, Invoke should be required. Then for now, I’m just trying to dump out the frame details before I work on displaying the data.

Interestingly enough, the program will run fine for a while. As soon as I move the main window of the program around the desktop, the error appears almost instantly. I must be changing the timing just enough that an object is getting garbage collected before it’s used. It seemed like the solution most suggested was to make the delegate static, but that’s not working for me.

Update

Sounds like I missed some relevant code. Below is the code that causes the frameGrabbed event. Essentially it’s an interrupt handler that is called by a DLL that I’m using.

I declare the following:

// Function pointer used by StartGrabEx
public delegate void GrabFrame_CallbackEx(IntPtr userData, ref FrameInfoEx frameInfo);

Then I start the data collection with:

    public void Start()
    {

        // Start grabbing frames
        isGrabRunning = true;
        GrabFrame_CallbackEx callback = new GrabFrame_CallbackEx(GrabCallback);
        StartGrabEx(callback);
    }

The GrabCallback function then looks like this:

    FrameInfo lastFrame;

    private void GrabCallback(IntPtr userData, ref FrameInfoEx frameInfo)
    {
        // Are we grabbing frames?
        if (!isGrabRunning)
        {
            return;
        }

        lock (locker)
        {
            lastFrame = new DataFrameInfo(bufferIndex, frameInfo.number, frameInfo.timestamp);
        }

        // We've captured a frame, notify the DataCollectionThread
        frameGrabbed.Set();
    }

Looking over this update, perhaps the problem is with lastFrame, although I thought I changed the code to update lastFrame in GrabCallback (rather than using new) and that still failed.

Update #2

Perhaps I should also mention that the DataCollectionThread is declared as such:

DataCollectionThread = new Thread(DataCollectionThread );
DataCollectionThread .Name = "DataCollectionThread ";
DataCollectionThread .IsBackground = true;
DataCollectionThread .Start();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:11:43+00:00Added an answer on June 13, 2026 at 1:11 pm

    As Hans Passant said, the problem was with the callback variable in the Start() method. I changed it to the following:

    private GrabFrame_CallbackEx callback;
    
    public void Start()
    {
    
        // Start grabbing frames
        isGrabRunning = true;
        callback = new GrabFrame_CallbackEx(GrabCallback);
        StartGrabEx(callback);
    }
    

    Everything is working now!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
This could be a duplicate question, but I have no idea what search terms
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.