Hi,

I have a problem with a random number generating function. This function works great on windows XP but when I run the function in Windows VISTA and Windows 7, I receive an error message that displays:

"Attempted to read or write protected memory. This is often an indication that other memory has been corrupted"

(I dont know if this is because I mix mananged and unmanaged code. I did find the srand a good random function from the unmanaged code so this is why I have mixed it here)

int Start = 0 int Start = 0;
 int End = 50;
 int NumberResults = 100;
 std::vector<int> FillData;
 int Number5 = 0;


 std::string Dummy;
 System::String^ text;




 srand((unsigned)time(nullptr)); 
 for( int i = 0; i < NumberResults + 1; i++ )
 {


	 Number5 = rand() % (End - Start + 1) + Start;


		stringstream v1;
		std::string v2;					

		v1 << Number5;
		v2 = v1.str();

		String^ Number = gcnew String(v2.c_str());

		this->textBox1->Text = text;
		this->textBox1->Multiline = true;
		this->textBox1->WordWrap = false;


		text += Number + System::Environment::NewLine;
 }

Dani AI

Generated

This error text is the classic AccessViolation (native memory corruption) surface — on Vista/7 DEP/ASLR and stricter memory protections will make a latent native bug fail where XP tolerated it. Common causes are heap/stack corruption in unmanaged code, invalid pointers passed across the managed/native boundary, bad P/Invoke signatures or calling conventions, double-frees, or buffer overruns. Mixing managed and unmanaged code increases the risk unless every native call and pointer is marshalled/pinned correctly.

Practical, low-effort checks that often fix this class of crash:

  • Ensure all managed handles are initialized before use (matches ’s observation).
  • Stop updating UI properties inside the tight loop (set Multiline/WordWrap once, and set Text only once after building the content). Repeated cross-boundary work can expose timing/GC issues.
  • Avoid repeated std-to-String conversions; instead build the output in a single managed buffer (StringBuilder) and assign it in one go.
  • Prefer managed Random instead of global srand/rand when working in a .NET UI app.
  • Verify loop bounds and arithmetic used in the modulus to rule out division-by-zero or negative modulus values. ’s managed approach (initializing strings, using managed RNG) is a sensible way to eliminate several suspects quickly. Running “as Administrator” (as suggested) rarely changes the underlying memory-corruption cause — it only alters privileges, not heap integrity.

If problems remain, isolate and diagnose: create a minimal managed-only repro; enable mixed-mode/native debugging and “break on all exceptions” in Visual Studio to capture the native call stack; use Application Verifier / PageHeap or ProcDump to produce a crash dump and analyze it in WinDbg. Those tools will point to the corrupting module (native DLL, bad P/Invoke, or heap misuse) so the precise fix can be applied.

Recommended Answers

All 3 Replies

Line 35, you add to text which is declared as a handle at line 9 but never gets an object assigned to it.

I think, with my very limited knowledge of .NET, you need to gcnew String an object to text.

Keep in mind that windows Vista and 7 have much more memory read/ write protection than XP did. Try right clicking on your executable file in Vista/ 7 and choosing 'Run as Administrator'.

Hello Jenifer88

Your code is full of mixed Managed and Unmanaged code:-/
If you are going to write code in the .NET environment it is best to stick to managed code where possible.
I've commented out the unmanaged code in you program and given you something to get going with.
Hope it is some help to you.
Cheers
Milton

private:
		void Randomize(void)
		{
			int Start = 0;
			int End = 50;
			int NumberResults = 100;
			//std::vector<int> FillData;
			int Number5 = 0;
			Random^ rand = gcnew Random(); // Class for generating random numbers
			//std::string Dummy;
			System::String^ text = ""; // Best to initialise the variable
			//srand((unsigned)time(nullptr)); 
			 for( int i = 0; i < NumberResults + 1; i++ )
			 {
				Number5 = rand->Next() % (End - Start + 1) + Start;

				//stringstream v1;
				//std::string v2;					
				//v1 << Number5;
				//v2 = v1.str();
				
				text += Number5.ToString() + System::Environment::NewLine;
				this->textBox1->Text = text;
				/*
				Better to set these properties in the 
				design mode properties for the TB control.
				You do not want to set them in very "for loop"
				it just slows things down.
				**************************************************
				this->textBox1->Multiline = true;
				this->textBox1->WordWrap = false;
				// A verticle scoll bar is handy here
				this->textBox1->ScrollBars = ScrollBars::Vertical
				**************************************************/
			 }
		}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.