Hi all,

I got this assertion problem that i cant seem to solve, could you guys help me on this. So far, i think the problem might be due to the while loop for the iterator line by line check and i have made changes to it but still cant solve it.

Debug assertion failed

file:f\dd\vctools\crt_bld\self_x86\crt\src\dbgheap.c
Line: 1317
Expression: _CrtIsValidHeappointer(pUserData)

file:f\dd\vctools\crt_bld\self_x86\crt\src\dbgheap.c
Line: 1419
Expression: _pFirstBlock == pHead

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
		 
{
	ifstream ckin("sample.txt");
	int initclock = 0;
	
	if (ckin==NULL)
	{
		MessageBox::Show ("not found");
		ckin.close();
		initclock++;		
	}
	
	if (ckin!=NULL)
	{		
		char checkEmpty;
		ckin.get(checkEmpty);
		if (!ckin.eof()) 
		{textBox1->Text = "file empty";
		ckin.close();
		}
		else 
		{textBox1->Text = "not empty";
		ckin.close();
		initclock++;
		}
	}
	ifstream in("sample.txt");
	string line;
	string match("sample\:");
	int clock = 0 ;
	while (getline(in, line)&&(initclock==0))
		{			
		stringstream sstrm(line);
		string::iterator iter =
		search (line.begin(),line.end(),match.begin(),match.end());
										if (iter != line.end() )
		{
	 iter = find_if( iter +match.length(), line.end(),not1(ptr_fun <int,int> (isspace)));
		if (iter == line.end())
		{
		MessageBox::Show ("No numbers or words after Serial no. :");
		break;
		}
		string::iterator j = find_if (iter, line.end(), ptr_fun <int,int> (isspace));
		string found;
		copy(iter, j, back_insert_iterator <string> (found));
		
		//create a new file based on the word after said word
		//and saved it into the said folder
		string filename = found + ".txt";
		string dirname = "log//";
		string finaldir = dirname + filename;
		ofstream(finaldir.c_str());
		//using ifstream and ofstream to copy content of file to another
		char * fbuffer;
		long fsize;
		ifstream cpy ("sample.txt",ios::binary);
		ofstream target (finaldir.c_str(),ios::binary);
		cpy.seekg(0,ifstream::end);
		fsize=cpy.tellg();
		cpy.seekg(0);
		fbuffer = new char [fsize];
		cpy.read (fbuffer,fsize);
		target.write (fbuffer,fsize);
		delete[] fbuffer;
		cpy.close();
		target.close();
						  
		//write the found word/serial no. into temp.txt
		const char * buffer=found.c_str();
		string str (found.c_str());
		int sizeNum = str.length();
		long size = sizeNum;
		ofstream outfile ("store.txt");
		outfile.write (buffer,size);
		//delete[] buffer;//release dynamically allocated memory
		outfile.close ();
								//textbox update and check if previous saved name is the same as 
	//current one
	textBox2->Text = System::IO::File::ReadAllText("store.txt");
	if (textBox2->Text == label3->Text)						{
		MessageBox::Show ("no. repeated");
		}
		else
		{
		label3->Text = textBox2->Text;
		}
	//try to use an empty file to write and empty a file 
	//which is being used by another program
	char * ebuffer;
	long esize;
	ifstream ecpy ("void.txt",ios::binary);
	ofstream etarget ("sample.txt",ios::binary);
	ecpy.seekg(0,ifstream::end);
	esize=ecpy.tellg();
	ecpy.seekg(0);
	ebuffer = new char [esize];
	ecpy.read (ebuffer,esize);
	etarget.write (ebuffer,esize);
	delete[] ebuffer;
	ecpy.close();
	etarget.close();
	textBox1->Text = "done";
	clock++;
	in.close();
	if (clock!=0)
	break;
}//if iter
	else
	{
	textBox1->Text = "not found";
	}
}//while
}

You've trashed your allocated memory pool.
Unfortunately, the code you've posted is probably OK (if you've been staring at it for that long without solving the problem).

The thing of it is, where the fault occurred (the real code you need to fix) is seldom the same as the code which notices there's a problem (the code you posted). It's this remoteness of "cause" and "effect" which really takes the time in solving these kinds of problems.

The fact that your code hasn't crashed up to now just makes you lucky, not good.

What you do about it now however is harder to say.
You could look at the MALLOC_DBG options, and cause it to call the pool-checking functions on every alloc/free call. This will slow it down, but it will notice the problem a lot sooner.

The weird part is that it had no problem compiling and executing in any com with vc++ 2008 express edition.

Initially, it could not even be executed in com without vc++ express installed, come with error saying->"application failed to start because application configuration is incorrect. reinstalling the application may fixed the problem".

I did followed msdn example of copying the msvcr, msvcm, msvcp.dll and a manifest file into the same folder as the .exe file for those com which dont have vc++ express installed.

Then it was able to execute but disappointingly come with the above mentioned error which i cant seem to solve.

Current changes are those ifstream i had changed to fstream but still cant solve it.

Can someone kind enough to spare some time on this and give me some advices, pointers or any link that can assist me.

Thanks in advance.

On the folder "C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\BootStrapper\Packages\vcredist_x86" on the PC with installed Visual Studio 8 placed the file "vcredist_x86.exe" (for VS 2008 installation there are similar redistributable package). Sometimes running this installer on the machine without installed development environment can bring better results than copying the required MFC and CRT dlls to the system PATH of that computer.

On the folder "C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\BootStrapper\Packages\vcredist_x86" on the PC with installed Visual Studio 8 placed the file "vcredist_x86.exe" (for VS 2008 installation there are similar redistributable package). Sometimes running this installer on the machine without installed development environment can bring better results than copying the required MFC and CRT dlls to the system PATH of that computer.

Thanks for your reply.

I have tried installing the vcredist_x86 on target com with no Microsoft Visual studio but it cant even be executed. after pasting the dlls then it can be executed.

during debugging, after i exited my program i saw red wording in the stack frame: xxx.exe!main(array<system::string^>^args={Length=0})Line 17.

Anyone know what causes this? or is this the cause to my assertion failed?

int main(array<System::String ^> ^args)
{
	// Enabling Windows XP visual effects before any controls are created
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false); 

15.	// Create the main window and run it
16. 	Application::Run(gcnew Form1());
17. 	return 0;
18. }

Brian MuthMVP

You should be aware that you cannot legally redistribute the msXXXd.dll's in this manner. They are not redistributable.

To deploy a c++ application properly you should read the following material:

How to Deploy C++ Properly
Deployment (C++)
How to redistribute the Visual C++ Libraries with your application
Windows Installer
Deploying .NET Framework Applications

Above reply is from the msdn forum for VC++.

Im glad that the problem is not due to my code but because vc++ express is free so it has its limitation. Sad and angry that i spent quite a considerable amount of time on it which seem is fruitless.

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.