{"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."}  
Author Message
ambe





PostPosted: Visual C# General, {"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."} Top

So i have method which is executing in loop, there is this method:

public void Play()

{

MemoryStream tempStream = new MemoryStream(memStream.GetBuffer()); //everytime i create new memorystream

try

{

applicationBuffer = new SecondaryBuffer(tempStream, applicationDevice); //create directsound secondary buffer to play data in stream

}

catch (Exception ex)

{

MessageBox.Show(ex.ToString());

}

if (applicationBuffer != null)

applicationBuffer.Play(0, BufferPlayFlags.Default); //play data

//tempStream.Flush(); //tried this

//tempStream.Close(); //and this

tempStream.Dispose();

tempStream = null;

//GC.Collect(); //this didn'd help

//GC.WaitForPendingFinalizers();

}

so the first time in loop everything works fine, but when method is trying to execute second time i have error wich is in subject. I think that it's somehow related to tempStream creating and disposing but how I have tried to debug, and first time tempStream created succesful, second time stream created too, and is filled with needed data, but i have this error anyway.... i need to create tempStream becouse everytime arrived memStream is closed and i cant directly create secondaryBuffer from closed stream, so i just get buffer from it...

maby somebody has ideas




Visual C#8  
 
 
RizwanSharp





PostPosted: Visual C# General, {"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."} Top

So i have method which is executing in loop, there is this method:

public void Play()

{

MemoryStream tempStream = new MemoryStream(memStream.GetBuffer()); //everytime i create new memorystream

try

{

applicationBuffer = new SecondaryBuffer(tempStream, applicationDevice); //create directsound secondary buffer to play data in stream

}

catch (Exception ex)

{

MessageBox.Show(ex.ToString());

}

if (applicationBuffer != null)

applicationBuffer.Play(0, BufferPlayFlags.Default); //play data

//tempStream.Flush(); //tried this

//tempStream.Close(); //and this

tempStream.Dispose();

tempStream = null;

//GC.Collect(); //this didn'd help

//GC.WaitForPendingFinalizers();

}

so the first time in loop everything works fine, but when method is trying to execute second time i have error wich is in subject. I think that it's somehow related to tempStream creating and disposing but how I have tried to debug, and first time tempStream created succesful, second time stream created too, and is filled with needed data, but i have this error anyway.... i need to create tempStream becouse everytime arrived memStream is closed and i cant directly create secondaryBuffer from closed stream, so i just get buffer from it...

maby somebody has ideas

I think you are overwriting data by calling Play with no delay, i.e One Play call is in progress and you make another call to the Play() function so underlying stream is OverWritten.

And also release resources from applicationBuffer after use because next time you'll call Play function, it'll re create a new instance of it in Try block!

Observe you code and see if you are making Thread safe calls to it.

May be this solves your problem!

Best Regards,



 
 
SvenC





PostPosted: Visual C# General, {"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."} Top

Hi,

you might need to reset the tempStream stream pointer by calling tempStream.Postion = 0

--
SvenC


 
 
ambe





PostPosted: Visual C# General, {"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."} Top

i found another intresting thing which one confused me at all... my method Play() where secondaryBuffer in wich secondaryBuffer is created and try to play.. when i try to execute this methot invoked by code (from another method) i have no sound... but if i run this method from buttonClick it sounds great!

private void PlaySound(byte [] soundBytes, bool isLast)

{

byte[] recievedBuffer = null;

recievedBuffer = soundBytes;

if (!isLast)

{

Writer.Write(recievedBuffer, 0, recievedBuffer.Length);

}

else //in this place we get anyway, becouse we have last chunk of bytes anyway

{

Writer.Flush();

Play(); //in this place i invoke may method but it doesn't play, as you see after this i do nothing just play

}

}

so what's the diffrent when i have this i have no sound, but if i comment method invoking here ant invoke by button click it works.....