Click to See Complete Forum and Search --> : What's happen? SetDlgItemText Crash


Sandrine
October 8th, 1999, 03:49 AM
Hi!

What's happen?
Yesterday, my app work without problem.
Today, it crashes! The crash occurs at the initialization of a dialog box , at setting data in control with the "SetDlgItemText "instruction.
I don't understand why today it crashes. Between yesterday and today i don't change the file concerning with dialog. I just add some functions to print, to tile the windows...
The error messages are :
"L'instruction 0x758fe" emploie l'adresse memoire "0x8652f" qui ne peut pas etre written".
"Unhandled exception in powercap.exe (NTDLL.DLL); access violation"

Somebody has an idea!

Sandrine

eric33
October 8th, 1999, 06:57 AM
Dont you change anything in your resources or recource.h files (for example control's ID)?

Do you have some code sample ?

Sandrine
October 8th, 1999, 07:24 AM
I add some items to my menu...
But i have some controls the ID is a number: Because i have a lot of controls in a dialog box a number is easier to manipulate. They have the number 100 to 196.
Is it perhaps not a good thing? In the resource.h each ID of a control has a number (#Define IDC_EDIT_NAME 3652). And the first ID control has the number 1000. the ID of dialog box, bitmaps and icons have the numbers from 100 to 300...

Please help me...

this is a part od code when it crashes:

for ( i=0; i<8; i++)
{

nID = 100 + ( i*6) + 1; // int nID=>int or UINT
str.Format("%d", pdu.NbLine[m_PDUIndex + i]);
SetDlgItemText(nID, str);
.....
}



Thanks a lot

Sandrine

eric33
October 8th, 1999, 07:42 AM
You must be sure of the ids you are using.

The resource editor use IDs string. These strings are mapped to numbers in the resource.h.
So the ids you directly use in your code must be the SAME as the ids in the resource.h that corresponds of the ids string in the dialog.

If you try to access an non existing id (in the window) then a GPF occurs.

I think it is poor method of working.

For example you could try to make an array of ids


UINT myids={IDC_EDIT1,IDC_EDIT2,IDC_EDIT3};
for ( i=0; i<siseof(myids)/sizeof(UINT); i++) {
str.Format("%d", pdu.NbLine[m_PDUIndex + i]);
SetDlgItemText(myids(i), str);
.....
}




hope this helps

Sandrine
October 8th, 1999, 07:53 AM
You're right: I try to work with an array...

But my app crashes when i open a dialog box whiwh the ID are correctly defined. You think that my problem is due to these uncertain ID.

And another question, i have these messages in my debug window:
"Loaded 'C:\WINNT\system32\KERNEL32.DLL', no matching symbolic information found."
"Loaded 'C:\WINNT\system32\NTDLL.DLL', no matching symbolic information found."
"First-chance exception in Powercap.exe (KERNEL32.DLL): 0xC0000005: Access Violation".
"First-chance exception in Powercap.exe (NTDLL.DLL): 0xC0000005: Access Violation".

What do they mean?

Thanks

Sandrine

eric33
October 8th, 1999, 08:25 AM
Does your error occur if you remove the SetDlgItem code ?
If yes you must certainly access either an unknown id in the window or a control that don't support SetDlgItem ?
In this case replace your id computing by real ids of your window. Still crashing ?
I dont know the exact differences between SetDlgItemText and SetWindowText. Try the second ...

About your messages : Microsoft Secret World

Sandrine
October 11th, 1999, 01:51 AM
I change my ID but the app crashes always.It crashes after i try to open some dialog box .
This is the message in the debug window when the crash occurs:

"HEAP[Powercap.exe]: Invalid Address specified to RtlGetUserInfoHeap( 140000, 77e771e8 )"

I think i will rebuilt my apply from a backup...

Just a question:
I have global pointers. In the OnInitInstance() function, i initialise them with NULL value.
After, I call a function which return a pointer (address) and affect it to my pointer. Is it correct?
Example
* the pointer: int *p;
* p = NULL;
* p = thefunction(...); // with int* the function (...)

Thanks for your replies
Je suis desesperee!

Sandrine

Jason Teagle
October 11th, 1999, 02:12 AM
1) Try a full recompile on your project (Build - Rebuild All). Sometimes too many incremental recompiles and relinks can screw the whole project up.

2) Before using the ID with SetDlgItemText(), call GetDlgItem(). Test the CWnd pointer returned from that to see if it is NULL. If it is, then you are trying to access a non-existent ID.

3) Failing that, trace into the SetDlgItemText() call. Follow it through the MFC code and see if you can spot any problems as you go.

eric33
October 11th, 1999, 02:30 AM
About your pointer this seems !VERY! incorrect.

If i am correct you do this :


int *fct(...) {
...}

int *i=NULL;
// HERE IS A ERROR
*i=fct();
// i DONT contain now the returned pointer
// but you try to put the value of the returned pointer in NULL address (->GPF)
// YOU MUST DO THIS :
i=fct();
// NOW i IS EQUAL TO THE RETURNED POINTER




I dont what you want to do.
If you want to keep a pointer to integer in i then do as described above.

Another thing.
Dont forget that a function MUST NOT return a pointer to a local variable

Here is an example that fct MUST NOT do.

int *fct() {
int i;
// VERY VERY BAD STUFF
return &i;
}




hop this helps

October 11th, 1999, 02:58 AM
Dear Sandrine,

Going through your discussions, I think that a little more time should be taken
and the whole code (related to the dialog under question) should be rewritten.
It is sounding a magnum opus, but if you just fix the Dialog ID and the control
IDs carefully, your code will stabilize. You may not have to rewrite the code
fully. But this extra time you spend now will buy you a lot of peace of mind
later.

In general, it is a good practice to use control IDs through the resource. For a
large system, the chances of a Dialog ID duplication or control ID duplication
(in the same dialog) is very high, specially if you are attaching control ids from
your source program.

Jason's suggestion of a full compile (Rebuild All) is good; it may help. But if
there is a ID duplication, a code walk-through is imminent and the sooner it
is done, the better.

In answer to Eric's question:
SetDlgItemText() sets text for the control in a dialog; e.g. edit control,
radio button, push button etc. It assumes a valid Dialog ID/handle and
a valid control ID/handle in that dialog.
SetWindowText() applied to a dialog will set the text in its title bar. It only
assumes a valid Dialog ID/handle.

If you really want to use dynamic control IDs, then create the controls
dynamically, too. For instance, you may have 20 edit boxes in a sin

Sandrine
October 11th, 1999, 04:16 AM
Sorry, in my previous mail, i use "*" to mark the different steps...

This is what i do:
- First i declare a global pointer and assign a NULL value: MyPointer = NULL;


- After i call my function and assign the return adress to my pointer: MyPointer = MyFunction();

My pointer is derived of the CMDIFrameWnd class and my function is called when i want to create a new Wnd and the returned value is the returned value of the CMDIChildWnd::Create() function.

I think that it's correct, no?

I manage to find the mean of some "access violation messages": cause i manipulate empty strings.

But my initial problem is not resolved...even if i rebuilt my application...

Thanks

Sandrine

eric33
October 11th, 1999, 04:35 AM
CMDIChildWnd::Create returns a BOOl not a pointer.
Where do you alloate your mdi child window, in function local stack or in global heap ?

Finally the suggestions of the two others to make some debug and rebuild sound find to me.

Sandrine
October 11th, 1999, 04:49 AM
Yes, you're right for CMDIFrameWnd... Stupid girl!

I alloate my MDI window in the OnInitInstance() function... But, i just assign the NULL value, not use new because i don't know where free it. In the destructor,it doesn't work...

eric33
October 11th, 1999, 05:23 AM
Its good you found the problem source.
But something sounds bad to me.
You allocate a general CMDIFrameWnd and you have a function that call create on this object.
Does this mean you call your function several times and so you call several times Create method of the same CMDIFrameWnd object ?

good luck

Sandrine
October 11th, 1999, 06:22 AM
Re:
You allocate a general CMDIFrameWnd and you have a function that call create on this object.
Does this mean you call your function several times and so you call several times Create method of the same CMDIFrameWnd object ?

Yes...

---
You know, now my appli crashes when i try to open some dialog boxes. With the debug , i follow the instructions and it crashes when it calls the CreateDlgIndirect() function.
And i have the following message :
"HEAP[Powercap.exe]: Invalid Address specified to RtlGetUserInfoHeap( 140000, 77e771e8 )
First-chance exception in Powercap.exe (NTDLL.DLL): 0xC0000005: Access Violation."
What's this function? What does it do?...

Sandrine

eric33
October 11th, 1999, 06:33 AM
This function creates a dialog from a memory template.
This function sends a WM_INITDIALOG message to your dialog window class.
The function send to a WM_FONT message to the dialog. So look at your OnSetFond function of your dialog (if you have one).

Perhaps you could make a breakpoint in your
OnInitDialog or OnSetFont functions of your dialog class (the problem could be here).

To be complete the function shows the dialog if the WS_VISIBLE is set in the style's dialog box.

Do you have try to make step into the function ?
I am not sure of what does the SDK ::CreateDialogIndirect function. Perhaps it could assert the validity of the parent wnd.

Sandrine
October 11th, 1999, 07:05 AM
The appli. displays my dialog box and i can see that it displays the content of some controls but just after it crashes.
I put a breakpoint in the OnInitDialog() function -i don't have a OnSetFont() function- , just before the CDialog::OnInitDialog() line. But it crashes before.I can't eter in the OnInitDialog funtion.

Step by step, i localize the crash: it occurs in the CDialog::DoModal() function, at this line
VERIFY(RunModalLoop(dwFlags) == m_nModalResult);



and in the RunModalLoop() function at this line:
// pump message, but quit on WM_QUIT
if (!AfxGetThread()->PumpMessage())



???

Sandrine
October 11th, 1999, 07:11 AM
The appli. displays my dialog box but just after it crashes.
I put a breakpoint in the OnInitDialog() function -i don't have a OnSetFont() function- , just before the CDialog::OnInitDialog() line. But it crashes before.I can't eter in the OnInitDialog funtion.

Step by step, i localize the crash: it occurs in the CDialog::DoModal() function, at this line
VERIFY(RunModalLoop(dwFlags) == m_nModalResult);



and in the RunModalLoop() function at this line:
// pump message, but quit on WM_QUIT
if (!AfxGetThread()->PumpMessage()) // HERE
{
AfxPostQuitMessage(0);
return -1;
}




???

eric33
October 11th, 1999, 08:20 AM
Dont know what exactly is your problem ?
I think the better way is to go back to a previous version that works.
Then add the portions little by little and try to found the source of your crash.

I look at the PumpMessage function.
It returns FALSE (and quit) if GetMessage returns FALSE that means if a WM_QUIT message is received.

Dont know where your problem comes from.

Sandrine
October 12th, 1999, 05:14 AM
The last one...

I want to thank all, and in particular Eric33, for your attention and your advises. You spent a long time to help me to resolve my problem. It was very nice...

I think my problem comes really from the function SetDlgItemText(). When i set in comment all the calls to this function, the appli works almost: Step by step with the debugger, the dialog box is displayed but when you click on OK or Cancel it crashes (??). I have always the "access violation (NTDDLL.DLL) " message when i try to open some dialog boxes (those which crash).

But, at the end of month of June ( 99/09/26), i sent a mail to the forum to know how to use SetDlgItemText(). And, somabody explain me that use this function as many people advise me , that is to say format a CString variable with the numeric value and use SetDlgItemText(), is ill-advise. Cause the CString constructor allocates small amounts of memory on the heap and very often calls will make the heap fragmented. Is it perhaps the source of my problem?

I don't know... I will work with a backup and implement step by step the missing functions

Thank all very much

Sandrine

eric33
October 12th, 1999, 05:59 AM
Using CString is not ill advise as the person said to you.
If you just declare a local CString or heap CString the problem is to correctly free up the cstring when you dont need it anymore. Dont forget that after setting the text with SetDlgItemText you can free your CString because the control has it own memory for text allocation.

I think that the method to go back to a previous good backup is good.

No matter how long it is to help you
Forums like codeguru are usefull only if it helps you founding a solution to your problems.
So dont bother and send questions if any