🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #7813

Re: Close Form in DLL

#7813
From: KL Chin
Date: Tue, 21 Dec 2004 05:26
239 lines
9335 bytes
Hi Remy,

Sorry, I will not able to show the complete code, it was too big.

BTW, not I wanted to access the DLL after it is freed.
It was some how called during the unload/closing process and
it was always had problem when Server issue a close command.


I hv a callback routine call from EXE to DLL to recheck the
DLL's form was truely free before it unload the DLL, the problem
always after this call.  FYI, on CloseForm caFree was used.

This problem occur with this module only,  I had other modules same
coding style working without any problem.

Here some portion of the actual code

void __fastcall  TfrMxCast::avPreExit( void )
{
    if ( !iPreInited )
      return;
    tbtStop->Click( );   // stop playback
    if ( pWav    )  pWav->wvoStop( );
    if ( pAudTTx )  pAudTTx->fStop = 1;   // flag thread to stop
    if ( pVdoTTx )  pVdoTTx->fStop = 1;  // flag thread to stop
    if ( pAudTRx )  pAudTRx->fStop = 1;  // flag thread to stop
    if ( pVdoTRx )  pVdoTRx->fStop = 1;  // flag thread to stop
    if ( vdoTime )
      {
        timeKillEvent( vdoTime );  vdoTime = 0;
        while ( iInVdoDsp && (99 != iInVdoDsp) )
          xSleep( 20 );
      }
    if ( pAudTTx )  pAudTTx->fqcStp( );  // trigger thtread stop event
    if ( pAudTRx )  pAudTRx->fqcStp( );  // trigger thtread stop event
    if ( pVdoTTx )  pVdoTTx->fqcStp( );  // trigger thtread stop event
    if ( pVdoTRx )  pVdoTRx->fqcStp( );  // trigger thtread stop event
    if ( pWav )
      {
        pWav->wvoClose( );
        delete pWav;
        pWav = NULL;
      }
    if ( pAudTTx )  {  delete pAudTTx;  pAudTTx = NULL;  }
    if ( pVdoTTx )  {  delete pVdoTTx;  pVdoTTx = NULL;  }
    if ( pAudTRx )  {  delete pAudTRx;  pAudTRx = NULL;  }
    if ( pVdoTRx )  {  delete pVdoTRx;  pVdoTRx = NULL;  }
    if ( pAudTxQ )  {  pAudTxQ->queClear( );  delete pAudTxQ;  pAudTxQ = 
NULL;  }
    if ( pVdoTxQ )  {  pVdoTxQ->queClear( );  delete pVdoTxQ;  pVdoTxQ = 
NULL;  }
    if ( pAudRxQ )  {  pAudRxQ->queClear( );  delete pAudRxQ;  pAudRxQ = 
NULL;  }
    if ( pVdoRxQ )  {  pVdoRxQ->queClear( );  delete pVdoRxQ;  pVdoRxQ = 
NULL;  }
    if ( NULL != pATxCpr )  {  OSFreeMem( pATxCpr );  pATxCpr = NULL;  }
    if ( NULL != pVTxCpr )  {  OSFreeMem( pVTxCpr );  pVTxCpr = NULL;  }
    if ( NULL != pATxRaw )  {  OSFreeMem( pATxRaw );  pATxRaw = NULL;  }
    if ( NULL != pVTxRaw )  {  OSFreeMem( pVTxRaw );  pVTxRaw = NULL;  }
    if ( NULL != pARxDcp )  {  OSFreeMem( pARxDcp );  pARxDcp = NULL;  }
    if ( NULL != pARxRaw )  {  OSFreeMem( pARxRaw );  pARxRaw = NULL;  }
    if ( NULL != pVRxRaw )  {  OSFreeMem( pVRxRaw );  pVRxRaw = NULL;  }
    dxdExit( );
    if ( hWndVdo )  { delete hWndVdo;  hWndVdo = NULL;  }
    iPreInited = 0;
}

void __fastcall TfrMxCast::FormClose( TObject *Sender,  TCloseAction 
&Action )
{
  DWORD dwtm;
  int   ncnt;

    DspMsg( 0,0,80,0,"Closing %08ld",GetTickCount( ) );
    Enabled          = false;
    tmrNQue->Enabled = false;
    tmrSeek->Enabled = false;
    tmrLoop->Enabled = false;
    tmrTime->Enabled = false;
    if ( iPlgInTInfo )
      iPlgInTInfo( PGII_PLUGINFREE,(PVOID)20000,InTAID_MOVIE );    // inform 
host, it will take 20000msec to free
    avPreExit( );
    if ( wTermType != TERM_ST )  // is program act as server
      {
        iEndSend = 0xFEFE;
        // To ensure packet were sending out with InTMSG_NETACK and must be 
valid dll InTMSG_VALIDDLL
        if ( iSendInT_VarPrm )
          iSendInT_VarPrm( InTMSG_USENETPRM | InTMSG_VALIDDLL | 
InTMSG_USETCP,  // InTMSG_SENDTOAPP,
                           dwLocalTID,dwLocalTID | 
0x000000FF,InTAID_MOVIE,InTAID_MOVIE,MDC_EXIT,0xFEFE,0,NULL );
        xSleep( 200 );  // delay 200msec
      }
    if ( pGrpLst )
      for ( ncnt = 0;  ncnt < pGrpLst->Count;  ++ncnt )
        iPlgInTInfo( 
PGII_RMVTMPCID,(PVOID)((PTIDGrpInf)pGrpLst->Items[ncnt])->agrphdr.dwCID,InTAID_MOVIE 
 );
    while ( pNetQue && pNetQue->Count )
      {
        OSFreeMem( pNetQue->Items[0] );
        pNetQue->Items[0] = NULL;
        pNetQue->Delete( 0 );
      }
    while ( pGrpLst && pGrpLst->Count )
      {
        OSFreeMem( pGrpLst->Items[0] );
        pGrpLst->Items[0] = NULL;
        pGrpLst->Delete( 0 );
      }
    if ( !iForceStop )
      {
        frMxCast = NULL;
        Action   = caFree;
      }
    if ( iPlgInTInfo )
      iPlgInTInfo( PGII_PLUGINCLOSED,NULL,InTAID_MOVIE );  // inform Host 
that DLL was ready to be unload.
}  // until this point, everything still good

int __fastcall TfrMxCast::ProcessQue( LPTNetHeader lpPrm )   // process 
remote command from server
{
  DWORD dwCID;

    DspMsg( 0,0,0,0,"MDC : %04lX",lpPrm->InTHdr.wMDC );
    switch ( lpPrm->InTHdr.wMDC )
      {
        case MDC_EXIT      :  iOnClosing = 2;
                              Enabled          = false;
                              tmrNQue->Enabled = false;
                              tmrSeek->Enabled = false;
                              tmrLoop->Enabled = false;
                              tmrTime->Enabled = false;
                              tbtStop->Click( );  // stop playback
                              xSleep( 100 );
                              PostMessage( Handle,WM_CLOSE,0,0 );
                              PostMessage( Handle,WM_DESTROY,0,0 );  // with 
this closing issue look like dissapear, but
                                                                             
                          //  new problem occur when re-start, still 
checking
                         break;
     }
}

void __fastcall TfrMxCast::tmrNQueTimer( TObject *Sender )
{
  LPTNetHeader pptr;

    tmrNQue->Enabled = false;
    while ( pNetQue && pNetQue->Count )
      {
        pptr = (LPTNetHeader)pNetQue->Items[0];
        pNetQue->Delete( 0 );
        ProcessQue( pptr );
        OSFreeMem( pptr );
      }
    tmrNQue->Enabled = !iOnClosing;
}

// call from Host/EXE to make sure created form was freed before it unload 
it
__declspec(dllexport) int WINAPI PluginExit( void )
{
    if ( frMxCast )
      {
        iForceStop = 1;
        frMxCast->Close( );
        delete frMxCast;
      }
    frMxCast = NULL;
    if ( clsPrms.WinClassName[0] ) // not used
      UnregisterClass( clsPrms.WinClassName,clsPrms.WindowClass.hInstance );
    CoUninitialize( );
    return ( 0 );
}   // until this point, everything still good

// call from EXE to create a Form
__declspec(dllexport) int WINAPI PluginInit( HWND hWnd,  viPluginInTInfo 
lpInTInfo )
{
  static PSTR psec = "Movie";

  FARPROC      afunc[10];
  String       stmp,sini;
  WAVEFORMATEX awf;
  char         amac[256];
  BYTE         bmac[64];
  PSTR         pnxt;
  char         aaut[256];

    CoInitialize( NULL );  // must call otherwise TSampleGrabber not 
availble
    dxdInit( GetDesktopWindow( ),353,288 );
    ZeroMemory( &clsPrms,sizeof(Controls::TCreateParams) );
    if ( lpInTInfo )
      {
        iPlgInTInfo = lpInTInfo;
        iPlgInTInfo( PGII_NETSEND,afunc,sizeof(afunc) );  // get EXE's 
function for DLL to  callback
        (FARPROC)iSendInT_VarPrm = afunc[0];
        (FARPROC)iSendInT_NetPrm = afunc[1];
        (FARPROC)iSendInT_NetPkt = afunc[2];
      }
    if ( !frMxCast )
      frMxCast = new TfrMxCast( (HWND)NULL );
    return ( frMxCast == NULL ? 0 : 1 );
}


Regards
KL Chin

"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message 
news:41c7a7df$1@newsgroups.borland.com...
>
> "KL Chin" <klchin@1asia.com.my> wrote in message
> news:41c7a03a@newsgroups.borland.com...
>
>> The problem almost every time (99%) had problem when close
>> the application remotely, by issue the PostMessage with WM_CLOSE
>> or Close.  Where the application always access/call/jump to the
>> free-ed DLL memory/code area (filled with 0 or ???? view with

>> debugger), where I'm not sure the code belonging to which lib, bcoz
>> the debugging show the wrong source code or little info.
>
> Please show your actual code.  Why are you trying to access the DLL after 
> it
> is freed?  Where are you freeing the DLL to begin with?
>
>
> Gambit
>
> 
Technical Details
Message-ID: <41c7b41a@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!fr.ip.ndsoftware.net!border2.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <41c78d36@newsgroups.borland.com> <41c79929$3@newsgroups.borland.com> <41c7a03a@newsgroups.borland.com> <41c7a7df$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,bd28311fbf37010b
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "KL Chin" <klchin@1asia.com.my>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <41c78d36@newsgroups.borland.com> <41c79929$3@newsgroups.borland.com> <41c7a03a@newsgroups.borland.com> <41c7a7df$1@newsgroups.borland.com>
Subject: Re: Close Form in DLL
Date: Tue, 21 Dec 2004 13:26:46 +0800
Lines: 241
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Original
NNTP-Posting-Host: 219.93.184.30
Message-ID: <41c7b41a@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1103606810 219.93.184.30 (20 Dec 2004 21:26:50 -0800)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!fr.ip.ndsoftware.net!border2.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail