Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C++ Builder > EAccessViolationException help


Автор: StrikeTheCore 20.6.2006, 00:14
I have code running that constantly throws an Access Violation on Windows XP. This exception only occurs however after around 10 to 20 thousand times running this function. I can catch the exception but want to get rid of it completely, however cannot figure out where it is coming from. Code is below (str is a simple string parser). Any help is greatly appreciated.

//--------------------------------------------------------

        unsigned char *inbuffer;
        AnsiString tmp;
        struct timeval current;
        inbuffer = new unsigned char[Count+2];
        myComPort->Read(inbuffer, Count);
        str.reset();
        try {
        if(Form2->mode == TForm2::HEX_MODE && !Form2->BufferDisabled->Checked){
                for(int i=0; i<Count; i++){
                        tmp.sprintf("%0x ", inbuffer[i]);
                        if(tmp.Length() < 3)
                                tmp = AnsiString("0") + tmp;
                        str << tmp.c_str() << " ";
                }
                if(Form2->ReceiveBuffer.Length() + str.length() < Form2->MaxBufLen){
                        Form2->ReceiveBuffer += AnsiString(str.c_str()).UpperCase();
                }
                else{
                        unsigned int n=0;
                        for(int count=0;n<=str.length();count++)
                                n += (*(Form2->Memo->Lines))[count].Length();
                        Form2->ReceiveBuffer.Delete(0, n);
                        Form2->ReceiveBuffer += AnsiString(str.c_str()).UpperCase();
                }
        }
        else if(Form2->mode == TForm2::ASCII_MODE && !Form2->BufferDisabled->Checked){
                inbuffer[Count] = '\0';
                if(Form2->ReceiveBuffer.Length() + strlen((char*)inbuffer) > Form2->MaxBufLen){
                        Form2->ReceiveBuffer.Delete(0, strlen((char*)inbuffer));
                }
                Form2->ReceiveBuffer += AnsiString((char*)inbuffer);
                if(Form2->logdata){
                        std::ofstream outfile;
                        outfile.open(Form2->LogFileName.c_str(), std::ofstream::out | std::ofstream::app);
                        outfile << (char*)inbuffer;
                        outfile.close();
                }
        }
        }
        catch (const EAccessViolation & vae)
        {
                ShowMessage("Misread message");
        } 

Автор: Vyacheslav 20.6.2006, 09:18
Is  Form2->ReceiveBuffer  AnsiString?  If this is AnsiString,  the first  argument  in Delete has to be  larger than 0 and less than Length of the AnsiString. See help
Цитата

Removes a specified number of characters from the string.

AnsiString& __fastcall Delete(int index, int count);

Description

Delete modifies the AnsiString to remove count characters from the string beginning with the character at index, where 1 is the index of the first character. It returns the resulting modified string (*this).

If index is larger than the length of the AnsiString or less than 1, no characters are deleted.

If count specifies more characters than remain starting at the index, Delete removes the rest of the string. If count is less than 0, no characters are deleted.
 

Автор: StrikeTheCore 20.6.2006, 16:18
Thanks a lot, that solved the problem. I guess I'm so used to Java and string indexes starting at 0 that I didn't even consider the possibility that this could be the source of the problem. 

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)