19 May '13 - 18:32 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register  
Pages: [1]
  Reply  |  Print  
Author Topic: Garbage Collector  (Read 978 times)
dregalia
Posts: 10


« on: 19 Jul '12 - 08:31 »
Reply with quoteQuote


I got this error today once I implemented a timer on stream:

A callback was made on a garbage collected delegate of type 'Bass.Net!Un4seen.Bass.AddOn.Enc.ENCODENOTIFYPROC::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.

So, My initial search on this board for this error lead me to this post:  http://www.un4seen.com/forum/?topic=5826.0 and I changed my code to use a global variable...

Quote
Public Class Form1
Private posBytes As Integer
--lots of code --
 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        posBytes = Bass.BASS_ChannelGetPosition(Stream, BASSMode.BASS_POS_BYTES)
        ToolStripStatusLabel1.Text = posBytes
 End Sub
End Class

This really is only the relevant code, everything works fine until I implement the timer.  Did I miss the point of the referenced post?
Logged
radio42
Posts: 4012


« Reply #1 on: 19 Jul '12 - 14:37 »
Reply with quoteQuote

As the error references a garbage collected "ENCODENOTIFYPROC", I guess this is the real basis of the error.
Can you please show your code where you used that?

I guess the additional Timer tick stuff might simply make the error happen earlier.
Logged
dregalia
Posts: 10


« Reply #2 on: 20 Jul '12 - 06:41 »
Reply with quoteQuote

This is the code i'm using to play the songs

Quote
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero)
        'Dim stream As Integer = Bass.BASS_StreamCreateFile("P:\\MP3\\Buckcherry - Crazy Bitch.mp3", 0, 0, BASSFlag.BASS_DEFAULT)
        Stream = Bass.BASS_StreamCreateFile("P:\\MP3\\Buckcherry - Crazy Bitch.mp3", 0, 0, BASSFlag.BASS_DEFAULT)

        Dim lame As EncoderLAME = New EncoderLAME(stream)
        lame.InputFile = Nothing
        lame.OutputFile = Nothing
        lame.LAME_Bitrate = CInt(BaseEncoder.BITRATE.kbps_96)
        lame.LAME_Mode = EncoderLAME.LAMEMode.Default
        lame.LAME_TargetSampleRate = CInt(BaseEncoder.SAMPLERATE.Hz_44100)
        lame.LAME_Quality = EncoderLAME.LAMEQuality.Quality

        Dim _IceCast As ICEcast = New ICEcast(lame)

        _IceCast.ServerAddress = xxx
        _IceCast.ServerPort = xxx
        _IceCast.Username = xxx
        _IceCast.Password = "xxx"
        _IceCast.MountPoint = "/xxx"
        _IceCast.StreamName = "xxx"
        _IceCast.StreamGenre = "Test Genre"
        _IceCast.PublicFlag = False
        _IceCast.SongTitle = "Test Song"

        Dim cBroadcast As BroadCast = New BroadCast(_IceCast)
        cBroadcast.AutoReconnect = True
        cBroadcast.ReconnectTimeout = 5

        ' mute it (if you don't want to 'hear' it)
        Bass.BASS_ChannelSetAttribute(stream, BASSAttribute.BASS_ATTRIB_VOL, 0.0F)
        ' play it (which feeds the encoder/broadcaster)
        Bass.BASS_ChannelPlay(stream, False)
        cBroadcast.AutoConnect()
        Timer1.Enabled = True

    End Sub
Logged
radio42
Posts: 4012


« Reply #3 on: 20 Jul '12 - 14:41 »
Reply with quoteQuote

All your broadcast members are defined locally just within your Button1_Click event handler (lame , _IceCast, cBroadcast)!
So they will be garbage collected at some point in time once the method is left - as they do not 'live' beyond that scope!

So declare (lame , _IceCast, cBroadcast) as real class members and e.g. make them 'private' members.
Logged
dregalia
Posts: 10


« Reply #4 on: 20 Jul '12 - 22:20 »
Reply with quoteQuote

Yep, that was it... Thanks man!
Logged
wido
Guest
« Reply #5 on: 24 Oct '12 - 21:12 »
Reply with quoteQuote

Same problem here.

Used the solution. But error still occurs.

 Public Sub startstream()
 Dim i = DataGridView1.CurrentRow.Index
 Dim file_name = DataGridView1.Item("FilelocationDataGridViewTextBoxColumn", i).Value     

        Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero)
        stream = Bass.BASS_StreamCreateFile(file_name, 0, 0, BASSFlag.BASS_DEFAULT)
     
        lame = New EncoderLAME(stream)
     

        lame.InputFile = Nothing 'STDIN
        lame.OutputFile = Nothing 'STDOUT
        lame.LAME_Bitrate = CInt(EncoderLAME.BITRATE.kbps_128)
        lame.LAME_Mode = EncoderLAME.LAMEMode.Stereo
        lame.LAME_TargetSampleRate = CInt(EncoderLAME.SAMPLERATE.Hz_44100)
        lame.LAME_Quality = EncoderLAME.LAMEQuality.Quality



        ' create a StreamingServer instance (e.g. SHOUTcast) using the encoder:
        shoutcast = New SHOUTcast(lame)
        shoutcast.ServerAddress = "http://stream.net"
        shoutcast.ServerPort = 888
        shoutcast.Password = "dance"
        shoutcast.PublicFlag = True
        shoutcast.StationName = "BestClubdio"
        shoutcast.Genre = "NEusic"
        shoutcast.Url = "songs.com"



        ' use the BroadCast class to control streaming:
        _broadCast = New BroadCast(shoutcast)
        _broadCast.AutoReconnect = True
        AddHandler _broadCast.Notification, AddressOf OnBroadCast_Notification
        _broadCast.UpdateTitle("yo", "yo")

        ' _broadCast.Connect()

        Bass.BASS_ChannelSetAttribute(stream, BASSAttribute.BASS_ATTRIB_VOL, 0.0F)
        ' play it (which feeds the encoder/broadcaster)
        Bass.BASS_ChannelPlay(stream, False)
        _broadCast.AutoConnect()
End Sub

in the class i added the private variables
ublic Class Form1

    Private _recHandle As Integer
    Private _broadCast As BroadCast
    Private lame As EncoderLAME
    Private shoutcast As SHOUTcast
    Private stream

then in a button.click i call startstream()
when i click the button again error occurs.

Hope you got an idea.



Logged
radio42
Posts: 4012


« Reply #6 on: 25 Oct '12 - 08:32 »
Reply with quoteQuote

First what exact errors do you get  - and with which line of code?

Second, don't init bass again! BASS_Init only needs to be called once - e.g. initially in your app when you initialize your sound devices.
Calling BASS_Init again on an already initialized device will fail.

Finally I don't see any 'code' where you 'stop' your broadcaster, encoder and free your stream?!
So you should (when you don't need your braoadcast instance anymore):
- stop/close/dispose the broadcaster
- stop/close/dispose the encoder
- free your stream
Logged
wido
Guest
« Reply #7 on: 11 Jan '13 - 14:36 »
Reply with quoteQuote

Hi Radio42.

That was of a big help. Streaming goes well. The problem was caused by  bass_initing too many times.
I also stop the broadcaster / encoder / and free my stream.
I am using.
BASS_StreamFree(stream)
    _broadCast.StopEncoder()
            _broadCast.Disconnect()

My mp3 player runs automated from a playlist. So everytime it loads a new songs i do
BASS_StreamFree(stream)
            _broadCast.StopEncoder()
            _broadCast.Disconnect()

And then build the stream again from scratch see below. Is there also a better way to do this?

        stream = Bass.BASS_StreamCreateFile(file_name, 0L, 0L, BASSFlag.BASS_SAMPLE_FLOAT)
        Bass.BASS_ChannelPlay(stream, False)
        lame = New EncoderLAME(stream)
        lame.InputFile = Nothing 'STDIN
        lame.OutputFile = Nothing 'STDOUT
        lame.LAME_Bitrate = CInt(EncoderLAME.BITRATE.kbps_128)
        lame.LAME_Mode = EncoderLAME.LAMEMode.Stereo
        lame.LAME_TargetSampleRate = CInt(EncoderLAME.SAMPLERATE.Hz_44100)
        lame.LAME_Quality = EncoderLAME.LAMEQuality.Quality

        ' create a StreamingServer instance (e.g. SHOUTcast) using the encoder:
        shoutcast = New SHOUTcast(lame)
        shoutcast.ServerAddress = "http://stream..net"
        shoutcast.ServerPort = 8888
        shoutcast.Password = "dd"
        shoutcast.PublicFlag = True
        shoutcast.StationName = " Preview Radio"
        shoutcast.Genre = "NEW House Music"
        shoutcast.Url = "http://www..com"

       _broadCast = New BroadCast(shoutcast)
        _broadCast.AutoReconnect = True
        AddHandler _broadCast.Notification, AddressOf OnBroadCast_Notification
        _broadCast.UpdateTitle("yo", "yo")
        Bass.BASS_ChannelSetAttribute(stream, BASSAttribute.BASS_ATTRIB_VOL, 0.0F)
        _broadCast.AutoConnect()

Thanks Wido
Logged
radio42
Posts: 4012


« Reply #8 on: 11 Jan '13 - 19:06 »
Reply with quoteQuote

Sure, you might use BASSmix add-on and create a non-stop mixer stream (see the Bassmix addon for details).
You can then add you playback stream to that mixer as a source stream.
Note, that mixer sources must be decoding streams!
Then simply use the mixer stream for broadcasting - which you can create once at startup and then keep running forever.
The rest is just a matter of adding/removing source streams to the mixer.
Logged
Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.18 | SMF © 2013, Simple Machines