Changeset 13848

Show
Ignore:
Timestamp:
02/23/08 06:50:48 (4 months ago)
Author:
rouault
Message:

Add a ref counter to LTIVSIStream so that duplicate() method works correctly (fix crash on mrsid_4)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/gdal/frmts/mrsid/mrsidstream.cpp

    r13827 r13848  
    4141/************************************************************************/ 
    4242 
    43 LTIVSIStream::LTIVSIStream() : poFileHandle(NULL), nError(0) 
     43LTIVSIStream::LTIVSIStream() : poFileHandle(NULL), nError(0), pnRefCount(NULL) 
    4444{ 
    4545} 
     
    5151LTIVSIStream::~LTIVSIStream() 
    5252{ 
    53     if ( poFileHandle ) 
    54     { 
    55         VSIFCloseL( (FILE *)poFileHandle ); 
    56         nError = errno; 
     53    if ( poFileHandle) 
     54    { 
     55        (*pnRefCount)--; 
     56        if (*pnRefCount == 0) 
     57        { 
     58            VSIFCloseL( (FILE *)poFileHandle ); 
     59            nError = errno; 
     60            delete pnRefCount; 
     61        } 
    5762    } 
    5863} 
     
    6570                                    const char *pszAccess ) 
    6671{ 
     72    CPLAssert(poFileHandle == NULL); 
     73 
    6774    poFileHandle = (VSIVirtualHandle *)VSIFOpenL( pszFilename, pszAccess ); 
     75    if (poFileHandle) 
     76    { 
     77        pnRefCount = new int; 
     78        *pnRefCount = 1; 
     79    } 
    6880    nError = errno; 
    6981 
     
    7587/************************************************************************/ 
    7688 
    77 LT_STATUS LTIVSIStream::initialize( VSIVirtualHandle *poFileHandle ) 
    78 
    79     this->poFileHandle = poFileHandle; 
     89LT_STATUS LTIVSIStream::initialize( LTIVSIStream* ltiVSIStream ) 
     90
     91    CPLAssert(poFileHandle == NULL); 
     92 
     93    poFileHandle = ltiVSIStream->poFileHandle; 
     94    if (poFileHandle) 
     95    { 
     96        pnRefCount = ltiVSIStream->pnRefCount; 
     97        (*pnRefCount) ++; 
     98    } 
    8099 
    81100    return poFileHandle ? LT_STS_Success : LT_STS_Failure; 
     
    88107bool LTIVSIStream::isEOF() 
    89108{ 
     109    CPLAssert(poFileHandle); 
     110 
    90111    bool    bIsEOF = (0 != poFileHandle->Eof()); 
    91112    nError = errno; 
     
    118139LT_STATUS LTIVSIStream::close() 
    119140{ 
     141    CPLAssert(poFileHandle); 
     142 
    120143    if ( poFileHandle->Seek( 0, SEEK_SET ) == 0 ) 
    121144        return LT_STS_Success; 
     
    133156lt_uint32 LTIVSIStream::read( lt_uint8 *pDest, lt_uint32 nBytes ) 
    134157{ 
     158    CPLAssert(poFileHandle); 
     159 
    135160    lt_uint32   nBytesRead = 
    136161        (lt_uint32)poFileHandle->Read( pDest, 1, nBytes ); 
     
    146171lt_uint32 LTIVSIStream::write( const lt_uint8 *pSrc, lt_uint32 nBytes ) 
    147172{ 
     173    CPLAssert(poFileHandle); 
     174 
    148175    lt_uint32   nBytesWritten = 
    149176        (lt_uint32)poFileHandle->Write( pSrc, 1, nBytes ); 
     
    159186LT_STATUS LTIVSIStream::seek( lt_int64 nOffset, LTIOSeekDir nOrigin ) 
    160187{ 
     188    CPLAssert(poFileHandle); 
     189 
    161190    int nWhence; 
    162191    switch (nOrigin) 
     
    193222lt_int64 LTIVSIStream::tell() 
    194223{ 
     224    CPLAssert(poFileHandle); 
     225 
    195226    lt_int64    nPos = (lt_int64)poFileHandle->Tell(); 
    196227    nError = errno; 
     
    206237{ 
    207238    LTIVSIStream *poNew = new LTIVSIStream; 
    208     poNew->initialize( poFileHandle ); 
     239    poNew->initialize( this ); 
    209240 
    210241    return poNew; 
  • trunk/gdal/frmts/mrsid/mrsidstream.h

    r13827 r13848  
    4343    LTIVSIStream(); 
    4444    LT_STATUS initialize( const char *, const char * ); 
    45     LT_STATUS initialize( VSIVirtualHandle * ); 
     45    LT_STATUS initialize( LTIVSIStream* ltiVSIStream ); 
    4646    ~LTIVSIStream(); 
    4747 
     
    6767    VSIVirtualHandle    *poFileHandle; 
    6868    int                 nError; 
     69    int                 *pnRefCount; 
    6970}; 
    7071