Skip to content

Track bar callback throws exception #14

@SSteve

Description

@SSteve

I'm trying to write a sample to show how to use a track bar. I can create the track bar and display it in a window, but when I click on it I get this exception:

Managed Debugging Assistant 'InvalidVariant' has detected a problem in 'SamplesCS.exe'.
Additional Information: An invalid VARIANT was detected during a conversion from an unmanaged VARIANT to a managed object. Passing invalid VARIANTs to the CLR can cause unexpected exceptions, corruption or data loss.

I don't have experience with going back and forth between unmanaged and managed data. What should I do to avoid this exception?

Here's the code so far:

public class TrackBarSample : ISample
{
    public void Run()
    {
        using var src = Cv2.ImRead(FilePath.Image.Balloon);
        const string windowName = "TrackBar Sample";
        using var _ = new Window(windowName, WindowMode.Normal);
        var trackBarValue = 256;
        var trackBarRed = Cv2.CreateTrackbar("red", windowName, ref trackBarValue, 511, OnTrackBarChange);
        Cv2.ImShow(windowName, src);
        Cv2.WaitKey(0); 
    }

    private void OnTrackBarChange(int pos, object userData)
    {
        Debug.WriteLine($"track bar: {pos} object: {userData}");
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions