Author Topic: [Solved][C#]A callback was made on a garbage collected delegate of type 'Bass.  (Read 256 times)

reagcz

  • Posts: 2
Hi everyone, sorry to bother you, but I need your help. I'm writing a program for visualising music on an LED strip, but I'm running into a problem.
It runs for a few seconds, but crashes soon. I think I made a mistake somewhere that causes GC to trigger, which then causes a NullReferenceException. This also happened while I was trying to make a GUI. Mind you that I'm an absolute C# beginner. Anyone got any ideas?
Thanks

Code: [Select]
using System;
using System.Collections.Generic;
using System.Threading;
using Un4seen.Bass;
using System.Net.Sockets;

namespace MusicLed
{
    public class Program
    {
        public static bool RecordProc(int handle, IntPtr buffer, int length, IntPtr user)
        {
            Bass.BASS_StreamPutData(0, buffer, length);
            return true;
        }
        private static void Main(string[] args)
        {
            try
            {
                GC.TryStartNoGCRegion(4000); // This doesn't help
                UdpClient _udpClient = new UdpClient("X.X.X.X", 666);
                RECORDPROC myRecProc = new RECORDPROC(RecordProc);
                Bass.BASS_RecordInit(-1);
                Bass.BASS_RecordSetDevice(-1);
                Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero);
                int handle = Bass.BASS_RecordStart(44100, 1, BASSFlag.BASS_SAMPLE_FLOAT, myRecProc, IntPtr.Zero);
                int x, y;
                while (1 == 1)
                {
                    float[] buffer = new float[2048];
                    Bass.BASS_ChannelGetData(handle, buffer, (int)BASSData.BASS_DATA_FFT2048);
                    List<byte> spectrum = new List<byte>();
                    int _lines = 101;
                    int b0 = 0;
                    for (x = 0; x < _lines; x++)
                    {
                        float peak = 0;
                        int b1 = (int)Math.Pow(2, x * 10.0 / (_lines - 1));
                        if (b1 > 256) b1 = 256;
                        if (b1 <= b0) b1 = b0 + 1;
                        for (; b0 < b1; b0++)
                        {
                            if (peak < buffer[1 + b0]) peak = buffer[1 + b0];
                        }
                        y = (int)(Math.Sqrt(peak) * 3 * 255 - 4);
                        if (y > 255) y = 255;
                        if (y < 0) y = 0;
                        spectrum.Add((byte)y);
                    }
                    spectrum.RemoveRange(100, _lines - 101);
                    var data = new byte[303];
                    int RedThreshold = 170;
                    int YellowThreshold = 80;
                    for (int i = 0; i < 101; i++)
                    {
                        var pos = i * 3;
                        if (spectrum[i] < 80)
                        {
                            data[pos] = 0;
                            data[pos + 1] = 0;
                            data[pos + 2] = (byte)(spectrum[i] / 2);
                        }
                        if (spectrum[i] >= YellowThreshold)
                        {
                            data[pos] = 0;
                            data[pos + 1] = (byte)(spectrum[i] / 2);
                            data[pos + 2] = 0;
                        }
                        if (spectrum[i] > RedThreshold)
                        {
                            data[pos] = spectrum[i];
                            data[pos + 1] = 0;
                            data[pos + 2] = 0;
                        }
                    }
                    _udpClient.Send(data, data.Length);
                    Thread.Sleep((int)(1000 / 60));
                }
            }
            catch (NullReferenceException ex)
            {
                Console.WriteLine(ex);
            }
        }
    }
}
« Last Edit: 6 Jun '19 - 10:34 by reagcz »

radio42

  • Posts: 4655
Please see here:
http://www.bass.radio42.com/help/html/9b9af3f1-f0dd-42e8-898a-ed607b9d0f60.htm#Callbacks

The problem is, that you declare your RECORDPROC locally, and as such it is GCed once you leave the scope of its declaration.


reagcz

  • Posts: 2
Please see here:
http://www.bass.radio42.com/help/html/9b9af3f1-f0dd-42e8-898a-ed607b9d0f60.htm#Callbacks

The problem is, that you declare your RECORDPROC locally, and as such it is GCed once you leave the scope of its declaration.

Thanks, that's exactly what I was looking for.