Click here to Skip to main content
15,895,011 members
Home / Discussions / C#
   

C#

 
AnswerRe: Centralized exception handler and logger Pin
Giorgi Dalakishvili8-Oct-08 1:14
mentorGiorgi Dalakishvili8-Oct-08 1:14 
AnswerRe: Centralized exception handler and logger Pin
matixsc8-Oct-08 2:16
professionalmatixsc8-Oct-08 2:16 
QuestionRe:Installing Flash player with windows set up Pin
RameshwerE8-Oct-08 0:19
RameshwerE8-Oct-08 0:19 
QuestionUpdate requires a valid UpdateCommand when passed DataRow collection with modified rows. [modified] Pin
astrovirgin7-Oct-08 23:36
astrovirgin7-Oct-08 23:36 
AnswerRe: Update requires a valid UpdateCommand when passed DataRow collection with modified rows. Pin
Giorgi Dalakishvili8-Oct-08 0:23
mentorGiorgi Dalakishvili8-Oct-08 0:23 
AnswerRe: Update requires a valid UpdateCommand when passed DataRow collection with modified rows. Pin
nelsonpaixao8-Oct-08 12:47
nelsonpaixao8-Oct-08 12:47 
QuestionProblem with free up memory! Pin
Ha ha ha ha ha ha ha ha7-Oct-08 23:24
Ha ha ha ha ha ha ha ha7-Oct-08 23:24 
AnswerRe: Problem with free up memory! Pin
Simon P Stevens8-Oct-08 0:11
Simon P Stevens8-Oct-08 0:11 
private void Receive()
{
    try
    {
        /*BufferDescription*/
        BufferDescription playbackBufferDescription = new BufferDescription();
        playbackBufferDescription.BufferBytes = waveFormat.AverageBytesPerSecond / 5;
        playbackBufferDescription.Format = waveFormat;

        bbStop = false;
        IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);

        byte[] byteData;
        byte[] byteDecodedData;

        /*While the voice stream is being recieved */
        while (!bbStop)
        {
            //Receive data.
            byteData = udpClient.Receive(ref remoteEP);
            byteDecodedData = new byte[byteData.Length * 2];

            //I've used G711 for voice stream compression
            //G711 compresses the data by 50%, so I allocate a buffer of double
            //the size to store the decompressed data.
            //byteDecodedData = new byte[byteData.Length * 2];

            //Decompress data using the proper vocoder.
            ALawDecoder.ALawDecode(byteData, out byteDecodedData);

            //Play the data received to the user.
** HERE ==> playbackBuffer = new SecondaryBuffer(playbackBufferDescription, device);
            playbackBuffer.Write(0, byteDecodedData, LockFlag.None);
            playbackBuffer.Play(0, BufferPlayFlags.Default);

            first = false;
            //byteData = null;
            playbackBuffer = null;
            //byteDecodedData = null;
        }
        playbackBuffer.Dispose(); //Here is the problem! It doesn't work! Confused
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error on Receive", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}


You are creating a new playback buffer every time you loop round the while loop (See annotation above). You are then only disposing it at the end (after you've set it to null, so I'm surprised you don't get a null ref exception. (Also, if an exception occurs, the playback buffer will never get disposed)

You should either be re-using the same playback buffer all the time to prevent having to create a new one each time, or you should be properly disposing of it each time.

You can use the "using" construct to ensure it gets disposed of. This will automatically call dispose on the playback buffer object when it passes the closing brace, and also ensures it will get disposed even if an exception occurs:
//Play the data received to the user.
using (playbackBuffer = new SecondaryBuffer(playbackBufferDescription, device))
{
    playbackBuffer.Write(0, byteDecodedData, LockFlag.None);
    playbackBuffer.Play(0, BufferPlayFlags.Default);
}


Without seeing the rest of your code it's hard to tell, but if there is a way to just re-use the same playback buffer without creating a new one each time round the loop, that would be a better solution.


[By the way, If you use the <pre> "code block" tags instead, for large blocks of code it will be easier to read]

Simon

GeneralRe: Problem with free up memory! Pin
leppie8-Oct-08 1:53
leppie8-Oct-08 1:53 
GeneralRe: Problem with free up memory! Pin
Ha ha ha ha ha ha ha ha8-Oct-08 1:54
Ha ha ha ha ha ha ha ha8-Oct-08 1:54 
GeneralRe: Problem with free up memory! Pin
Simon P Stevens8-Oct-08 3:06
Simon P Stevens8-Oct-08 3:06 
GeneralRe: Problem with free up memory! Pin
Ha ha ha ha ha ha ha ha8-Oct-08 3:51
Ha ha ha ha ha ha ha ha8-Oct-08 3:51 
GeneralRe: Problem with free up memory! Pin
Simon P Stevens8-Oct-08 4:42
Simon P Stevens8-Oct-08 4:42 
GeneralRe: Problem with free up memory! Pin
DaveyM698-Oct-08 5:58
professionalDaveyM698-Oct-08 5:58 
GeneralRe: Problem with free up memory! Pin
Simon P Stevens8-Oct-08 22:58
Simon P Stevens8-Oct-08 22:58 
GeneralRe: Problem with free up memory! [modified] Pin
Mark Salsbery8-Oct-08 8:07
Mark Salsbery8-Oct-08 8:07 
GeneralRe: Problem with free up memory! Pin
Ha ha ha ha ha ha ha ha9-Oct-08 20:50
Ha ha ha ha ha ha ha ha9-Oct-08 20:50 
QuestionGet a whole column in a matrix Pin
xkrja7-Oct-08 21:37
xkrja7-Oct-08 21:37 
AnswerRe: Get a whole column in a matrix Pin
User 66587-Oct-08 21:48
User 66587-Oct-08 21:48 
QuestionHow to get whitespace in xml Pin
manju#1237-Oct-08 19:52
manju#1237-Oct-08 19:52 
AnswerRe: How to get whitespace in xml Pin
Pedram Behroozi7-Oct-08 20:18
Pedram Behroozi7-Oct-08 20:18 
GeneralRe: How to get whitespace in xml Pin
manju#1237-Oct-08 20:23
manju#1237-Oct-08 20:23 
GeneralRe: How to get whitespace in xml Pin
Pedram Behroozi7-Oct-08 20:29
Pedram Behroozi7-Oct-08 20:29 
GeneralRe: How to get whitespace in xml [modified] Pin
manju#1237-Oct-08 20:57
manju#1237-Oct-08 20:57 
GeneralRe: How to get whitespace in xml Pin
Pedram Behroozi7-Oct-08 22:47
Pedram Behroozi7-Oct-08 22:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.