From c200ec9338b1e2edbb5bc0165420f9229d49ffb9 Mon Sep 17 00:00:00 2001 From: charras Date: Tue, 22 Dec 2009 14:08:24 +0000 Subject: [PATCH] Fixed: eeschemas sometimes crashes after a component with aliases is modified in libedit --- eeschema/class_libentry.cpp | 6 +-- eeschema/class_libentry.h | 4 +- eeschema/class_library.cpp | 75 ++++++++++++++----------------------- 3 files changed, 35 insertions(+), 50 deletions(-) diff --git a/eeschema/class_libentry.cpp b/eeschema/class_libentry.cpp index f93d26f26e..c2a0e01f8a 100644 --- a/eeschema/class_libentry.cpp +++ b/eeschema/class_libentry.cpp @@ -126,13 +126,13 @@ int LibraryEntryCompare( const CMP_LIB_ENTRY* aItem1, const CMP_LIB_ENTRY* aItem * (like 74LS00, 74HC00 ... and many op amps ) */ -LIB_ALIAS::LIB_ALIAS( const wxString& aName, LIB_COMPONENT* aComponent, +LIB_ALIAS::LIB_ALIAS( const wxString& aName, LIB_COMPONENT* aRootComponent, CMP_LIBRARY* aLibrary ) : CMP_LIB_ENTRY( ALIAS, aName, aLibrary ) { - wxASSERT( aComponent != NULL && aComponent->isComponent() ); + wxASSERT( aRootComponent != NULL && aRootComponent->isComponent() ); - root = aComponent; + root = aRootComponent; } diff --git a/eeschema/class_libentry.h b/eeschema/class_libentry.h index 7c1b9f1cd8..77fac08cec 100644 --- a/eeschema/class_libentry.h +++ b/eeschema/class_libentry.h @@ -515,11 +515,13 @@ protected: * @note - Do not delete the root component. The root component is owned * by library the component is part of. Deleting the root component * will likely cause EESchema to crash. + * Or, if the root component is deleted, aliases must be deleted or their .root member + * must be changed to point a new root component */ LIB_COMPONENT* root; public: - LIB_ALIAS( const wxString& aName, LIB_COMPONENT* aComponent, + LIB_ALIAS( const wxString& aName, LIB_COMPONENT* aRootComponent, CMP_LIBRARY* aLibrary = NULL ); LIB_ALIAS( LIB_ALIAS& aAlias, CMP_LIBRARY* aLibrary = NULL ); ~LIB_ALIAS(); diff --git a/eeschema/class_library.cpp b/eeschema/class_library.cpp index 332d6eea4a..c3b5b010aa 100644 --- a/eeschema/class_library.cpp +++ b/eeschema/class_library.cpp @@ -356,60 +356,43 @@ library <%s>" ), LIB_COMPONENT* CMP_LIBRARY::ReplaceComponent( LIB_COMPONENT* aOldComponent, LIB_COMPONENT* aNewComponent ) { - wxASSERT( aOldComponent != NULL && aNewComponent != NULL - && aOldComponent->GetName().CmpNoCase( aNewComponent->GetName() )== 0 ); + wxASSERT( aOldComponent != NULL ); + wxASSERT( aNewComponent != NULL ); + wxASSERT( aOldComponent->GetName().CmpNoCase( aNewComponent->GetName() )== 0 ); size_t i; - int index; - LIB_ALIAS* alias; - - if( aOldComponent->m_AliasList != aNewComponent->m_AliasList ) - { - /* Remove extra aliases. */ - for( i = 0; i < aOldComponent->m_AliasList.GetCount(); i++ ) - { - index = - aNewComponent->m_AliasList.Index( aOldComponent->m_AliasList[ i ] ); - - if( index != wxNOT_FOUND ) - continue; - - wxLogDebug( wxT( "Removing extra alias <%s> from component <%s> in library <%s>." ), - GetChars( aOldComponent->m_AliasList[ i ] ), - GetChars( aOldComponent->GetName() ), - GetChars( fileName.GetName() ) ); - - RemoveEntry( aOldComponent->m_AliasList[ i ] ); - } - - /* Add new aliases. */ - for( i = 0; i < aNewComponent->m_AliasList.GetCount(); i++ ) - { - index = aOldComponent->m_AliasList.Index( aNewComponent->m_AliasList[ i ] ); - - if( index != wxNOT_FOUND - || FindEntry( aNewComponent->m_AliasList[ i ] ) != NULL ) - continue; - - wxLogDebug( wxT( "Adding extra alias <%s> from component <%s> in library <%s>." ), - GetChars( aNewComponent->m_AliasList[ i ] ), - GetChars( aNewComponent->GetName() ), - GetChars( fileName.GetName() ) ); - - alias = new LIB_ALIAS( aNewComponent->m_AliasList[ i ], aNewComponent ); - entries.push_back( alias ); - } - } - - RemoveEntry( aOldComponent->GetName() ); LIB_COMPONENT* newCmp = new LIB_COMPONENT( *aNewComponent, this ); - if( newCmp == NULL ) - return NULL; + /* We want to remove the old root component, so we must remove old aliases. + * even if they are not modified, because their root component will be removed + */ + for( i = 0; i < aOldComponent->m_AliasList.GetCount(); i++ ) + { +/* wxLogDebug( wxT( "Removing alias <%s> from component <%s> in library <%s>." ), + GetChars( aOldComponent->m_AliasList[ i ] ), + GetChars( aOldComponent->GetName() ), + GetChars( fileName.GetName() ) ); +*/ + RemoveEntry( aOldComponent->m_AliasList[ i ] ); + } + /* Now, add current aliases. */ + for( i = 0; i < aNewComponent->m_AliasList.GetCount(); i++ ) + { +/* wxLogDebug( wxT( "Adding alias <%s> from component <%s> in library <%s>." ), + GetChars( aNewComponent->m_AliasList[ i ] ), + GetChars( aNewComponent->GetName() ), + GetChars( fileName.GetName() ) ); +*/ + LIB_ALIAS* alias = new LIB_ALIAS( aNewComponent->m_AliasList[ i ], newCmp ); + entries.push_back( alias ); + } + + RemoveEntry( aOldComponent->GetName() ); entries.push_back( (CMP_LIB_ENTRY*) newCmp ); entries.sort(); + isModified = true; return newCmp; }