Try/catch gerber file loading in attempt to catch oom

Potentially help with #7444
This commit is contained in:
Marek Roszko 2021-05-01 12:46:18 -04:00
parent eb343d3f8f
commit 850a22c3ae
2 changed files with 51 additions and 36 deletions

View File

@ -37,7 +37,8 @@
// HTML Messages used more than one time:
#define MSG_NO_MORE_LAYER _( "<b>No more available layers</b> in GerbView to load files" )
#define MSG_NOT_LOADED _( "\n<b>Not loaded:</b> <i>%s</i>" )
#define MSG_NOT_LOADED _( "\n<b>Not loaded:</b> <i>%s</i>" )
#define MSG_OOM _( "\n<b>Memory was exhausted reading:</b> <i>%s</i>" )
void GERBVIEW_FRAME::OnGbrFileHistory( wxCommandEvent& event )
@ -253,48 +254,59 @@ bool GERBVIEW_FRAME::LoadListOfGerberAndDrillFiles( const wxString& aPath,
visibility[ layer ] = true;
if( aFileType && (*aFileType)[ii] == 1 )
try
{
LoadExcellonFiles( filename.GetFullPath() );
layer = GetActiveLayer(); // Loading NC drill file changes the active layer
}
else
{
if( filename.GetExt() == GerberJobFileExtension.c_str() )
if( aFileType && ( *aFileType )[ii] == 1 )
{
//We cannot read a gerber job file as a gerber plot file: skip it
wxString txt;
txt.Printf(
_( "<b>A gerber job file cannot be loaded as a plot file</b> <i>%s</i>" ),
filename.GetFullName() );
success = false;
reporter.Report( txt, RPT_SEVERITY_ERROR );
LoadExcellonFiles( filename.GetFullPath() );
layer = GetActiveLayer(); // Loading NC drill file changes the active layer
}
else if( Read_GERBER_File( filename.GetFullPath() ) )
else
{
UpdateFileHistory( m_lastFileName );
layer = getNextAvailableLayer( layer );
if( layer == NO_AVAILABLE_LAYERS && ii < aFilenameList.GetCount()-1 )
if( filename.GetExt() == GerberJobFileExtension.c_str() )
{
//We cannot read a gerber job file as a gerber plot file: skip it
wxString txt;
txt.Printf( _( "<b>A gerber job file cannot be loaded as a plot file</b> "
"<i>%s</i>" ),
filename.GetFullName() );
success = false;
reporter.Report( MSG_NO_MORE_LAYER, RPT_SEVERITY_ERROR );
// Report the name of not loaded files:
ii += 1;
while( ii < aFilenameList.GetCount() )
{
filename = aFilenameList[ii++];
wxString txt = wxString::Format( MSG_NOT_LOADED, filename.GetFullName() );
reporter.Report( txt, RPT_SEVERITY_ERROR );
}
break;
reporter.Report( txt, RPT_SEVERITY_ERROR );
}
else if( Read_GERBER_File( filename.GetFullPath() ) )
{
UpdateFileHistory( m_lastFileName );
SetActiveLayer( layer, false );
layer = getNextAvailableLayer( layer );
if( layer == NO_AVAILABLE_LAYERS && ii < aFilenameList.GetCount() - 1 )
{
success = false;
reporter.Report( MSG_NO_MORE_LAYER, RPT_SEVERITY_ERROR );
// Report the name of not loaded files:
ii += 1;
while( ii < aFilenameList.GetCount() )
{
filename = aFilenameList[ii++];
wxString txt =
wxString::Format( MSG_NOT_LOADED, filename.GetFullName() );
reporter.Report( txt, RPT_SEVERITY_ERROR );
}
break;
}
SetActiveLayer( layer, false );
}
}
}
catch( const std::bad_alloc& )
{
wxString txt = wxString::Format( MSG_OOM, filename.GetFullName() );
reporter.Report( txt, RPT_SEVERITY_ERROR );
success = false;
continue;
}
if( progress )
progress->AdvanceProgress();

View File

@ -50,20 +50,23 @@ bool GERBVIEW_FRAME::Read_GERBER_File( const wxString& GERBER_FullFileName )
Erase_Current_DrawLayer( false );
}
gerber = new GERBER_FILE_IMAGE( layer );
// use an unique ptr while we load to free on exception properly
std::unique_ptr<GERBER_FILE_IMAGE> gerber_uptr = std::make_unique<GERBER_FILE_IMAGE>( layer );
// Read the gerber file. The image will be added only if it can be read
// to avoid broken data.
bool success = gerber->LoadGerberFile( GERBER_FullFileName );
bool success = gerber_uptr->LoadGerberFile( GERBER_FullFileName );
if( !success )
{
delete gerber;
gerber_uptr.reset();
msg.Printf( _( "File \"%s\" not found" ), GERBER_FullFileName );
ShowInfoBarError( msg );
return false;
}
gerber = gerber_uptr.release();
wxASSERT( gerber != nullptr );
images->AddGbrImage( gerber, layer );
// Display errors list