Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am Getting First-chance exception error in Visual Studio 2005, after running the application... Anyone help me to fix this ....Thanks

[Copied code from below comment]
void CEditBox_1Dlg::OnBnClickedButton1()
{ 
    // TODO: Add your control notification handler code here 
    CString csSamplFreq; 

    char carrSamplFreq[50],csTemp[50]; 
    char *pcSamplFreq = NULL; 
    pcSamplFreq =(char*)carrSamplFreq; 

    m_CntrlDisplay.SetFocus(); 
    m_CntrlDisplay.GetWindowText(csSamplFreq); 
    strcpy(carrSamplFreq,(LPCSTR) (CStringA)csSamplFreq); 
    pcSamplFreq += usCurPos; 
    sprintf(csTemp,"%s",pcSamplFreq); 
    for(short sLoop = 0;sLoop<strlen((char*)csTemp);sLoop++) 
    { 
        *pcSamplFreq =csTemp[sLoop]; pcSamplFreq++; 
    } 
    *pcSamplFreq = '\0'; 
    csSamplFreq.Format(L"%s",carrSamplFreq); 
    m_CntrlDisplay.SetWindowText(csSamplFreq); 
    usCurPos++; 
    m_CntrlDisplay.SetSel(usCurPos,usCurPos); 
    //m_CntrlDisplay=atoi(carrSamplFreq); 
    m_TxtSamplingFrequency = carrSamplFreq; 
    UpdateData(false);
}
Posted
Updated 17-Dec-13 21:14pm
v2
Comments
CPallini 18-Dec-13 1:15am    
Without seeing the code how could we help you?
Baakki 18-Dec-13 2:03am    
This is my code...I am getting error , when the control goes to sprintf .....

<pre lang="cs">void CEditBox_1Dlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here

CString csSamplFreq;

char carrSamplFreq[50],csTemp[50];
char *pcSamplFreq = NULL;
pcSamplFreq =(char*)carrSamplFreq;

m_CntrlDisplay.SetFocus();

m_CntrlDisplay.GetWindowText(csSamplFreq);

strcpy(carrSamplFreq,(LPCSTR) (CStringA)csSamplFreq);

pcSamplFreq += usCurPos;


sprintf(csTemp,"%s",pcSamplFreq);

for(short sLoop = 0;sLoop<strlen((char*)csTemp);sLoop++)

{

*pcSamplFreq =csTemp[sLoop]; pcSamplFreq++;

}

*pcSamplFreq = '\0';

csSamplFreq.Format(L"%s",carrSamplFreq);
m_CntrlDisplay.SetWindowText(csSamplFreq); usCurPos++;
m_CntrlDisplay.SetSel(usCurPos,usCurPos);

//m_CntrlDisplay=atoi(carrSamplFreq);
m_TxtSamplingFrequency = carrSamplFreq;

UpdateData(false);</pre>
Jochen Arndt 18-Dec-13 3:19am    
I have copied the code from your comment to the question.

What do you want to achieve? You are copying string contents without any sense. Try to simplfy the code.

There are two points that may result in access violations:
- When the text in the control is longer than 49 chars, you have an buffer overrun on carrSamplFreq
- When usCurPos is greater than the length of the carrSamplFreq string, pcSamplFreq points to a not initialised string (which may be also beyond the valid locations)

There is not enough information to answer this question with details. The most often reason is: you're trying to read unallocated memory.

I would suggest you to read this: Analyze Crashes to Find Security Vulnerabilities in Your Apps[^].
Please, debug you program and check for errors.
 
Share this answer
 
Comments
Baakki 18-Dec-13 6:45am    
I want to append a character to the edit box on button click.....Do u hav any idea ?
Maciej Los 18-Dec-13 6:47am    
Please, read CPallini's comments.
Quote:
strcpy(carrSamplFreq,(LPCSTR) (CStringA)csSamplFreq);
This is not the correct way for assigning a (wide) CString content to character array.

Quote:
pcSamplFreq += usCurPos;
sprintf(csTemp,"%s",pcSamplFreq);
I suppose usCurPos is out-of-control there.


I think in your case it would probably better if you tell us what are trying to achieve instead of insist on fixing the ugly CString-character array mixture.
 
Share this answer
 
Comments
Baakki 18-Dec-13 4:29am    
I want to develop a edit box application, when I press a button, a character(A or B like wise) should get updated on the edit box and the cursor should update ie.., move to next position...
CPallini 18-Dec-13 4:34am    
Do you mean: "I want to append characters to the edit box on button click" ?
You know, you may manipulate the text using the '+' operator of the CString object.
Baakki 18-Dec-13 5:12am    
Yes I want to append a character to the edit box on button click.....Do u hav any idea ?
CPallini 18-Dec-13 5:30am    
Yes:

CString text;
MyEdit.GetWindowText(text);
MyEdit.SetWindowText(text + L'A');
Baakki 18-Dec-13 6:02am    
Yeah that's correct..., but in order to update the cursor, setfocus and setsel needed right ?
Bin sprintf and get rid of any arrays and pointers. Learn how to use std::stringstream and std::string. If you've still got a problem then come back and ask again.
 
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