Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
http://s1.uploads.im/vVqAx.png

I want to know how to solve this strange assertion fail. This is my program..

C++
#include <iostream>

using std::cout;
using std::endl;

void eatSpaces(const char arr[]);

void main()
{
	char x[]{"hello world how are you"};
	eatSpaces(x);
}

void eatSpaces(const char arr[])
{
	size_t arrPosition{}, strPosition{};
	char* str = new char[];
	
	while (arr[arrPosition] != '\0')
	{
		if (arr[arrPosition] != ' ')
			*(str + strPosition++) = arr[arrPosition];

		arrPosition++;
	}
	*(str + strPosition) = '\0';

	cout << str << endl;
}


As you see, my program has nothing to do with the file "f:\dd\vctools\crt\crtw32\misc\dbgheap.c"
Besides it's not where I save my programs.. It's a removable device. Why such errors occur without any clear reason ?

And this message box doesn't show up at once.. It's like a random.. Sometimes the program compiles and run with no errors. But sometimes as the program opens this assertion failing shows up.

Thanks in advance !

PS: No matter if I unplug the device, this message box keep showing up.
Posted
Updated 25-May-14 6:21am
v3
Comments
chandanadhikari 26-May-14 2:56am    
hello,
My solution below might be an overkill in this case (but those are useful skills neverthe less). Maybe a line by line debugging in the IDE itself would reveal something. So, first simply try debugging.

hi,
Welcome to the world of heap corruption, memory leaks and postmortem dump analysis.
You need to use WinDbg for such issues .

Actualy, your application is leaking memory . So,what is a memory leak[^]

If you Google for "dd/vctools/crt/crtw32/misc/dbgheap.c", you will see that it is a general error message for memory leak . It has nothing to do with where you save your programs.

You need to attach your application to WinDbg and then execute it.
You can directly see the call stack. This is called live debugging.
OR
In your case i think a memory dump must be generated automatically. you can analyze this dump
to see which line of code is causing the error . the 'reasons' will become pretty clear ;)

I do not want to give you overdose of info, in case all these things sound new to you. Better go slow and try to learn these useful skills.

Helpful series of articles related to WinDbg[^]
You can google more for WinDbg, dump files, pdb files etc .

hope this was helpful!
 
Share this answer
 
Comments
M­­ar­­­­k 26-May-14 6:49am    
Thanks.. :)
C++
char* str = new char[];

This allocation doesn't make a lot of sense and I'm surprised the compiler eats it without a warning or error. You haven't specified an array length, so your allocated memory may be as little as 1 byte. If you want more than that, you need to pass the required length as argument. Your later access to str accesses unallocated memory, causing the error.
 
Share this answer
 
There's a couple options; but the likelihood of diagnosing this correctly changes dramatically with the underlying structure of this program.
There's a couple of factors that will help us find where the problem arises; these questions need answering.
Have you changed the source at all since you last compiled the binary?
Does your application generate worker threads?
Have you correctly balanced all operator new()/malloc()/operator new[]() calls with the appropriate delete, free() or delete[]?
Have you appropriately set null all invalid dangling pointers (after delete or a RAII style deallocation)?
During your compilation process, did any compilations fail with the same error, only to magically disappear with basically no change?

The heap assertion checks the assumption that pUserData is a valid pointer to the heap memory.
It is not. start from there; break and use the call stack to find the last point the variable changed. Set a new data break-point on the variable.


If your program involves threads: check all your synchronization.
If your program source has been changed, please check your SCC's diff to see what change you made.
But the likely error is that you've just corrupted the pointer.
You should blame everyone who writes dynamic memory to this variable. Check this before you start looking for other criterion.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900