AV when print

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

AV when print

Postby Lena » Tue Nov 07, 2017 1:58 am

Hi.
I send a list of strings from the client to print in my program PrintServer.exe:
Code: Select all
class TUpdateRichEdit : public TIdNotify
{
   private:
      std::unique_ptr<TStringList> sText;
   protected:
      virtual void __fastcall DoNotify();
   public:
      __fastcall TUpdateRichEdit(std::unique_ptr<TStringList> &sMessage);
};

//for log
class TUpdateMemo2 : public TIdNotify
{
   protected:
      String sText;
      void __fastcall DoNotify();
   public:
     __fastcall TUpdateMemo2( const String &sMessage );
};


Code: Select all
TFormPrint *FormPrint;

//---------------------------------------------------------------------------
__fastcall TFormPrint::TFormPrint(TComponent* Owner)
   : TForm(Owner)
{
 Top = 0;
 Left =  Screen->Width - Width;
 IdTCPServer1->Active = true;
}
//---------------------------------------------------------------------------

__fastcall TUpdateRichEdit::TUpdateRichEdit(std::unique_ptr<TStringList> &sMessage)
   : TIdNotify(), sText(std::move(sMessage))
{
}
//---------------------------------------------------------------------------

__fastcall TUpdateMemo2::TUpdateMemo2( const String &sMessage )
   : TIdNotify(), sText(sMessage)
{
}
//---------------------------------------------------------------------------

void __fastcall TUpdateMemo2::DoNotify()
{
 if (sText.IsEmpty()) return;

 FormPrint->Memo2->Lines->Add(sText);
 FormPrint->Memo2->Lines->Add(L"**********");

}
//---------------------------------------------------------------------------

void ZapisVRichEditWindowMemo2(String zapis)
{

   std::unique_ptr<TUpdateMemo2> updateLB(new TUpdateMemo2(zapis));
   updateLB->Notify();
   updateLB.release();

}
//---------------------------------------------------------------------------


void __fastcall TUpdateRichEdit::DoNotify()
{

  FormPrint->Memo1->Lines->Clear();
  FormPrint->Memo1->Lines->Assign(sText.get());

   int count = sText->Count;
   if (count == 0)
      return;

   std::unique_ptr<TPrinter> Prntr(new TPrinter);
   Prntr->BeginDoc();
   try
   {
      Prntr->Canvas->Font->Size = 11;
     for (int i = 0; i < FormPrint->Memo1->Lines->Count; i++)
     Prntr->Canvas->TextOut(100,
       100 + (i * Prntr->Canvas->TextHeight(FormPrint->Memo1->Lines->Strings[i])),
       FormPrint->Memo1->Lines->Strings[i]);

     /*
     for (int i = 0; i < count; ++i)
     {
       Prntr->Canvas->TextOut(
         100,
         100 + (i * Prntr->Canvas->TextHeight(sText->Strings[i])),
         sText->Strings[i]
       );
     }
     */

      Prntr->Canvas->Brush->Color = clBlack;
   }
   __finally {
      Prntr->EndDoc();
   }
}
//---------------------------------------------------------------------------

void ZapisVRichEditWindow(std::unique_ptr<TStringList> &zapis)
{
   std::unique_ptr<TUpdateRichEdit> updateLB(new TUpdateRichEdit(zapis));
   updateLB->Notify();
   updateLB.release();
}
//---------------------------------------------------------------------------


void __fastcall TFormPrint::IdTCPServer1Execute(TIdContext *AContext)
{

 try
   {
     std::unique_ptr<TStringList> Sdata(new TStringList);
     AContext->Connection->IOHandler->ReadStrings(Sdata.get());
     ZapisVRichEditWindow(Sdata);
   }
   catch (const EIdConnClosedGracefully &)
   {
   }
   catch (const Exception &E)
   {
     String mis = _D("Class: ") + E.ClassName() + _D(" Message: ") + E.Message;
     ZapisVRichEditWindowMemo2(mis);
   }
}
//---------------------------------------------------------------------------

void __fastcall TFormPrint::IdTCPServer1Connect(TIdContext *AContext)
{
 AContext->Connection->IOHandler->DefStringEncoding = IndyTextEncoding_UTF8();
}
//---------------------------------------------------------------------------

void __fastcall TFormPrint::FormClose(TObject *Sender, TCloseAction &Action)
{
  if(Memo2->Lines->Count > 0)
     Memo2->Lines->SaveToFile("log.txt");
}



At my home, everything works. PRINT Canon printing without problems.

I took the program to another location on another computer and there is an AV.
Please tell me how you can identify the problem and fix it?
Attachments
print.jpg
print.jpg (81.08 KiB) Viewed 418 times
Lena
BCBJ Master
BCBJ Master
 
Posts: 525
Joined: Sun Feb 06, 2011 1:28 pm

Re: AV when print

Postby rlebeau » Tue Nov 07, 2017 4:12 pm

Lena wrote:
Code: Select all
   catch (const EIdConnClosedGracefully &)
   {
   }


You need to re-throw that exception, or not catch it at all. Let TIdTCPServer handle it so it can stop the calling thread and close the socket.

Lena wrote:
Code: Select all
   catch (const Exception &E)
   {
     String mis = _D("Class: ") + E.ClassName() + _D(" Message: ") + E.Message;
     ZapisVRichEditWindowMemo2(mis);
   }


Likewise, you should re-throw this exception too, especially if it derives from EIdException.

Personally, I would have used the TIdTCPServer::OnException event for error reporting, and not used a try block in the OnExecute event handler at all.

Code: Select all
void __fastcall TFormPrint::IdTCPServer1Execute(TIdContext *AContext)
{
    std::unique_ptr<TStringList> Sdata(new TStringList);
    AContext->Connection->IOHandler->ReadStrings(Sdata.get());
    ZapisVRichEditWindow(Sdata);
}

void __fastcall TFormPrint::IdTCPServer1Exception(TIdContext *AContext, Exception *AException)
{
    if (!dynamic_cast<EIdConnClosedGracefully*>(AException))
    {
        String mis = _D("Class: ") + AException->ClassName() + _D(" Message: ") + AException->Message;
        ZapisVRichEditWindowMemo2(mis);
    }
}


Lena wrote:At my home, everything works. PRINT Canon printing without problems.

I took the program to another location on another computer and there is an AV.
Please tell me how you can identify the problem and fix it?


There is not enough information for anyone here to diagnose that. You will have to debug your own code.

Is it an actual EAccessViolation, or are you just using AV as a general term for any unknown exception?

What is the actual class name and error message of the exception that is being thrown?

You might consider using an exception logging library like madExpect or EurekaLog to log uncaught exceptions, stack traces, etc.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1457
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA


Return to Technical

Who is online

Users browsing this forum: Bing [Bot] and 11 guests

cron