MFC debug assertion failed

Hi all,

i have some issue on my first MFC project.
i built some new windows that delete txt file according to the user input.
for some reason, after first running, assertion failed appear. and just build a new window + dedicated class solve the issue.

this is my windows call -

void CMFCApplication24Dlg::OnBnClickedButton6()
{
delwind item;
item.DoModal();
}

and this is mu delete function -

UpdateData(true);
int i = 0;
string str;
str = to_string(intTodel);
CString cstr(str.c_str());
CString filename = cstr + ".txt";
fstream fs;
std::string temps(CW2A(filename.GetString()));
fs.open(filename);
if (!fs)
{
MessageBox(L"No Car Plate found, please add a new car");
fs.close();
return;
}
fs.close();
temps = (CW2A(filename.GetString()));
int u = temps.size();
char * constchar = new char[u];
constchar = &temps[0];
constchar[u] = '\0';
int y = remove(constchar);
if (y == 0)
{
MessageBox(_T("Car removed"));
}

it is working for the first time so i pretty sure that this function cause the error.

please advice,

many thanks !
what did the assert message say? They usually tell you what the problem is.
Hi,

this is the error message -

Debug Assertion Failed!
…\source\repos\MFCAppplication24\x64\Debug\MFCApplication24.exe

File:
d:\agent\_work\3\s\src\vctools\vc7libs\ship\atlmfs\src\mfc\dlgdata.cppp
Line 40

I found the file related, but I can not do any change in the file.
> it is working for the first time so i pretty sure that this function cause the error.
Never equate apparent early success as being 'bug free'.
Often, a program will do what you want exactly once, but in doing so will leave a trial of devastation behind it that makes any subsequent run futile.

> int u = temps.size();
Lemme guess, if temps is "Hello", then u will be 5 right?

> char * constchar = new char[u];
So immediately, your attempt to copy the string fails to take into account the \0 that will be appended.

> constchar = &temps[0];
Nope, this isn't how you copy a string.
You just introduced a memory leak, and also just brute-forced a pointer to what you were trying to avoid changing in the first place.

> constchar[u] = '\0';
Now trash some memory - who's?

1
2
3
int u = temps.size();
char * constchar = new char[u+1]; // +1 for the \0
strcpy(constchar,temps.c_str()); // will copy a \0 


https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/remove-wremove?view=vs-2019
It's a pointless exercise anyway, since remove accepts a const char * to begin with, so you could simply have done
 
remove( temps.c_str() );
thanks salem, will try.
but do you think that the issue you point to, can cause the error ?
the error appear on the DoModal() command.

elad
Debug asserts show up when something decides to check whether the program is in some consistent state (or not).

They're not immediate (as in the next line) detections of where the root cause problem actually occurred.

There are tools to do that for you, but on Windows, they typically cost $$$ to buy.
hi,
unfortanttly, this is not working for me, please see in line.

> int u = temps.size();
Lemme guess, if temps is "Hello", then u will be 5 right?
yes you right.

> char * constchar = new char[u];
So immediately, your attempt to copy the string fails to take into account the \0 that will be appended.
no, i trying to create dynamic array with u cell size.

> constchar = &temps[0];
Nope, this isn't how you copy a string.
You just introduced a memory leak, and also just brute-forced a pointer to what you were trying to avoid changing in the first place.
just change it to your suggestion..

> constchar[u] = '\0';
Now trash some memory - who's?
just to close the array.

this is mu code right now.

thanks
> no, i trying to create dynamic array with u cell size.
I know that, my point was that you didn't make it long enough.
"Hello" takes SIX bytes of memory, because it's stored as "Hello\0".

> just to close the array.
No, you wrote to (in effect) temps.c_str()[u] = '\0';

> this is mu code right now.
Post your actual code.
Not your description of what you may (or may not) have done.

Also, there is no actual guarantee that this is where the problem REALLY lies, but it's the only code you posted.

You could try putting
1
2
3
4
5
6
7
8
9
10
11
12
#if 0
temps = (CW2A(filename.GetString()));
int u = temps.size();
char * constchar = new char[u];
constchar = &temps[0];
constchar[u] = '\0';
int y = remove(constchar);
if (y == 0)
{
MessageBox(_T("Car removed"));
}
#endif 

around sections of code to see if the overall problem goes away.
Sure, it won't delete the file, but if it still crashes, then you're pretty sure that this code wasn't the problem, because it isn't even run any more.

hello again.

just open a new MFC project and copy all the code.
its working fine, mode them one time :-).
i think the problem was multiple treat with the same variable.

thanks and sorry.

Topic archived. No new replies allowed.