I have a c# code that calls a dll (cpp build) and writes a text file (of 12 MB size) of txt file to a variable by calling a cpp dll
Getting following errors :
{"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."}
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
What I have tried:
c# code :
<pre> private void button3_Click(object sender, EventArgs e)
{
try
{
System.Threading.ThreadStart starter = delegate { CallTheNumerOfTimesEx(1, 98600, "Thread1"); };
System.Threading.Thread thread = new System.Threading.Thread(starter);
thread.Start();
thread.Join();
string outputFile = @"D:\SampleJFile.txt";
if (File.Exists(outputFile))
File.Delete(outputFile);
string loging = someVariable
using (StreamWriter writer = new StreamWriter(outputFile))
writer.WriteLine(toLog);
obj.Information("Asdf", loging , "asdf");
}
catch (Exception ex)
{
tracelog.Error(ex);
}
MessageBox.Show("");
}
cpp function that writes the received data :
void Trace::Information (String^ category, String^ messageText, String ^stackTrace)
{
String ^oldCategory = traceMessage->category;
TM->eventType = KTEventType::Information;
TM->category = category;
TM->errorCode = 0;
TM->messageText = messageText;
this->TM->timeStamp = DateTime::Now.ToString ("yyyy-MM-dd HH:mm:ss.fff");
if (stackTrace == nullptr)
{
System::Diagnostics::StackTrace ^st = gcnew System::Diagnostics::StackTrace ();
TM->stackTrace = st->ToString ();
}
else
TM->stackTrace = stackTrace;
LogMessage (TM);
TM->stackTrace = nullptr;
TM->category = oldCategory;
};
the given variables are declared as follows
private:
TraceMessage ^TM;
struct EXPORT_CLASS TraceMessage
{
public:
KTEventType eventType;
LPCWSTR category;
KTWSTRING timeStamp;
LPCWSTR machineName;
LPCWSTR applicationName;
LPCWSTR userName;
LPCWSTR threadID;
long errorCode;
LPCWSTR messageText;
LPCWSTR stackTrace;
LPCWSTR errorSource;
byte logLevel;
}
void GenericLogger::LogMessage(ILogMessage^ logMessage)
{
bool sharedMemoryLockHeld = false;
try
{
if(logMessage == nullptr)
{
throw gcnew ArgumentException("Custom log message passed to LogMessage method is null.");
}
long messageId = logMessage->MessageId;
if(messageId < 0)
{
throw gcnew ApplicationException("All custom message Ids must be positive integers. Please specify another message id for this custom log message.");
}
if(messageId == 0)
{
throw gcnew ApplicationException("The message id '0' is reserved for TraceMessage message types. Please specify another message id for this custom log message.");
}
Init();
MemoryStream^ pMemStream = gcnew MemoryStream();
BinaryFormatter^ pFormatter = gcnew BinaryFormatter();
pFormatter->Serialize(pMemStream, logMessage);
array<unsigned char>^ arrSerializedBytes =
pMemStream->GetBuffer();
if(arrSerializedBytes != nullptr)
{
int customMessageLength = arrSerializedBytes->Length;
if(customMessageLength > 0)
{
int totalMessageSize = customMessageLength + 12;
m_pShmWriter->BeginWrite(totalMessageSize);
sharedMemoryLockHeld = true;
m_pShmWriter->AddLong((long)LogType::Custom);
m_pShmWriter->AddLong(messageId);
m_pShmWriter->Write((array<system::byte> ^)arrSerializedBytes);
}
}
}
catch(Exception^ pEx)
{
Trace::WriteLine(pEx->Message);
}
finally
{
if(sharedMemoryLockHeld)
{
m_pShmWriter->EndWrite();
sharedMemoryLockHeld = false;
}
}
Could anyone suggest to fix the issue