I'm getting an error where it says I'm trying to use a null pointer. Then when I click ignore another error saying I tried to read or write protected memory. Any idea how to fix?

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

 using namespace System::Runtime::InteropServices;
using namespace System;
using namespace System::Text;

System::String ^ zs = textBox1->Text;
char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(zs);


char er[50];

string string1(er, 50);

System::String ^swq;
MarshalString(swq, string1);
label2->Text = "Hi";


Ever checked str2 after the call to Marshal:: StringToHGlobalAnsi(zs)???

More code:

Recive Data:

bool Socket::RecvData( char *buffer, int size )
	int i = recv( mySocket, buffer, size, 0 );
	buffer[i] = '\0';
	return true;

Marshall Function:

void MarshalString ( String ^ s, string& os ) {
   using namespace Runtime::InteropServices;
   const char* chars = 
	  (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
   os = chars;

You still are'nt checking chars for NULL!!!!