32位应用程序中的System.OutOfMemoryException

时间:2016-11-23 12:14:52

标签: .net visual-studio-2015 c++-cli

作为我的硕士论文,我正在为X射线物理学中的不同任务创建一个软件。该软件使用Visual Studio 2015以Visual C ++ .Net编写,仅适用于32位系统。

今天我注意到一个名为“System.Drawing.dll中的System.OutOfMemoryException”的错误。 当我将“大”文件导入我的应用程序时,会发生错误。

Visual Studio 2015的调试工具告诉我,错误发生在下面的函数中。根据调试工具,我已经在代码行创建了一个注释,其中应该出现错误。取消注释注释下面的其余代码将解决OutOfMemoryException,但不应该是我的目标。这会使软件对用户不那么友好。

问题:任何人都可以在下面的函数中找到错误(并发布解决方案),或者有一个很好的提示以解决我的问题?

如果您需要其他信息,请告诉我。如果没有必要,我不想详细介绍(> 10.000行代码,大量物理)。

原始代码:

void ManageComposition::CheckFontSetting(TreeNode^ treenode, bool checklines)
{
    int result;
    if (treenode->Nodes->Count!=0)
    {
        if ((treenode->Tag->GetType()->Name=="Element" && checklines) || treenode->Tag->GetType()->Name=="Composition")
            {
            IEnumerator^ treenodeenum = treenode->Nodes->GetEnumerator();
            while (treenodeenum->MoveNext())
            {
                this->CheckFontSetting(safe_cast<TreeNode^>(treenodeenum->Current),checklines);
            }
        }
    }
    if (treenode->Tag->GetType()->Name=="Composition" && this->WithLines())
    {
        result=safe_cast<Composition^>(treenode->Tag)->CheckCalculationParamters(this->normalizeto100);
        switch (result)
        {
        case 0: 
            treenode->BackColor=Color::LightGreen;
            break;
        case 1: 
            if (this->mode=="calculate intensities")
                treenode->BackColor=Color::Red;
            else if (this->mode=="calculate concentrations")
                treenode->BackColor=Color::LightGreen;
            else if (this->mode=="compare relative intensities")
                treenode->BackColor=Color::Red;
            break;
        case 2: 
            if (this->mode=="calculate concentrations")
                treenode->BackColor=Color::Red;
            else if (this->mode=="calculate intensities")
                treenode->BackColor=Color::LightGreen;
            else if (this->mode=="compare relative intensities")
                treenode->BackColor=Color::Red;
            break;
        case 3:
            treenode->BackColor=Color::Red;
            break;
        }
    }
    // This is the point where the errors occur while handling large lists
// uncommenting the rest of this code will solve the error, but functionality will be lost

    else if (treenode->Tag->GetType()->Name=="Element")
    {
        if (safe_cast<Element^>(treenode->Tag)->GetSymbol()!=treenode->Text)
        {
            if (safe_cast<Element^>(treenode->Tag)->GetIntensity(treenode->Text)!=0.0)
            {
                treenode->NodeFont=gcnew System::Drawing::Font(treenode->TreeView->Font,FontStyle::Bold);
                treenode->Text=treenode->Text;
            }
            else if (safe_cast<Element^>(treenode->Tag)->GetGroupIntensity(treenode->Text)!=0.0)
            {
                treenode->NodeFont=gcnew System::Drawing::Font(treenode->TreeView->Font,FontStyle::Bold);
                treenode->Text=treenode->Text;
            }
            else
            {
                treenode->NodeFont=gcnew System::Drawing::Font(treenode->TreeView->Font,FontStyle::Regular);
                treenode->Text=treenode->Text;
            } // Visual Studio debugging tool points on the condition below
            if (safe_cast<Element^>(treenode->Tag)->IsGroupSelected(treenode->Text) || safe_cast<Element^>(treenode->Tag)->GetTransition(treenode->Text)->IsSelected())
            {
                treenode->BackColor=Color::Aquamarine;
            }
            else
            {
                treenode->BackColor=this->treeview->BackColor;
            } 

        }
        else
        {
            bool red=false;
            if (this->mode=="calculate concentrations" || this->mode=="compare relative intensities")
            {
                if (safe_cast<Composition^>(safe_cast<Element^>(treenode->Tag)->owner)->IsRootComposition())
                {
                    if (safe_cast<Element^>(treenode->Tag)->GetIntensitySum()==0.0)
                    {
                        if (safe_cast<Element^>(treenode->Tag)->GetConcentration()==0.0)
                        {
                            red=true;
                        }
                    }
                }
                else
                {
                    if (safe_cast<Element^>(treenode->Tag)->GetIntensitySum()==0.0)
                    {
                        if (safe_cast<Element^>(treenode->Tag)->GetConcentration()==0.0)
                        {
                            red=true;
                        }
                    }
                }
                array<String^>^ dummy=safe_cast<Element^>(treenode->Tag)->GetRelevantLines();
                if (dummy->Length>1)
                {
                    int x;
                    String^ groupname=safe_cast<Element^>(treenode->Tag)->GetTransition(dummy[0])->GetGroup();
                    for(x=1;x<dummy->Length;x++)
                    {
                        if (groupname!=safe_cast<Element^>(treenode->Tag)->GetTransition(dummy[x])->GetGroup())
                            red=true; 
                    }
                }
            }
            if ((this->mode=="calculate intensities" || this->mode=="compare relative intensities")&& safe_cast<Element^>(treenode->Tag)->GetConcentration()==0.0)
                red=true;
            if (red)
                treenode->BackColor=Color::Red;
            else
                treenode->BackColor=Color::LightGreen;
        } 
    } 
}

使用@Hans Olsson的想法更新了功能:

void ManageComposition::CheckFontSetting(TreeNode^ treenode, bool checklines)
{

Font^ BoldFont = gcnew System::Drawing::Font(treenode->TreeView->Font, FontStyle::Bold);
Font^ RegularFont = gcnew System::Drawing::Font(treenode->TreeView->Font, FontStyle::Regular);

    int result;
    if (treenode->Nodes->Count!=0)
    {
        if ((treenode->Tag->GetType()->Name=="Element" && checklines) || treenode->Tag->GetType()->Name=="Composition")
            {
            IEnumerator^ treenodeenum = treenode->Nodes->GetEnumerator();
            while (treenodeenum->MoveNext())
            {
                this->CheckFontSetting(safe_cast<TreeNode^>(treenodeenum->Current),checklines);
            }
        }
    }
    if (treenode->Tag->GetType()->Name=="Composition" && this->WithLines())
    {
        result=safe_cast<Composition^>(treenode->Tag)->CheckCalculationParamters(this->normalizeto100);
        switch (result)
        {
        case 0: 
            treenode->BackColor=Color::LightGreen;
            break;
        case 1: 
            if (this->mode=="calculate intensities")
                treenode->BackColor=Color::Red;
            else if (this->mode=="calculate concentrations")
                treenode->BackColor=Color::LightGreen;
            else if (this->mode=="compare relative intensities")
                treenode->BackColor=Color::Red;
            break;
        case 2: 
            if (this->mode=="calculate concentrations")
                treenode->BackColor=Color::Red;
            else if (this->mode=="calculate intensities")
                treenode->BackColor=Color::LightGreen;
            else if (this->mode=="compare relative intensities")
                treenode->BackColor=Color::Red;
            break;
        case 3:
            treenode->BackColor=Color::Red;
            break;
        }
    }
    // This is the point where the errors occur while handling large lists
    // uncommenting the rest of this code will solve the error, but functionality will be lost
    else if (treenode->Tag->GetType()->Name=="Element")
    {
        if (safe_cast<Element^>(treenode->Tag)->GetSymbol()!=treenode->Text)
        {
            if (safe_cast<Element^>(treenode->Tag)->GetIntensity(treenode->Text)!=0.0)
            {
                if (BoldFont) treenode->NodeFont = BoldFont;
                treenode->Text=treenode->Text;
            }
            else if (safe_cast<Element^>(treenode->Tag)->GetGroupIntensity(treenode->Text)!=0.0)
            {
                if (BoldFont) treenode->NodeFont = BoldFont;
                treenode->Text=treenode->Text;
            }
            else
            {
                if (!RegularFont) treenode->NodeFont = RegularFont;
                treenode->Text=treenode->Text;
            }

            // Visual Studio debugging tool points to the condition below

            if (safe_cast<Element^>(treenode->Tag)->IsGroupSelected(treenode->Text) || safe_cast<Element^>(treenode->Tag)->GetTransition(treenode->Text)->IsSelected())
            {
                treenode->BackColor = Color::Aquamarine;
            }
            else
            {
                treenode->BackColor=this->treeview->BackColor;
            } 

        }
        else
        {
            bool red=false;
            if (this->mode=="calculate concentrations" || this->mode=="compare relative intensities")
            {
                if (safe_cast<Composition^>(safe_cast<Element^>(treenode->Tag)->owner)->IsRootComposition())
                {
                    if (safe_cast<Element^>(treenode->Tag)->GetIntensitySum()==0.0)
                    {
                        if (safe_cast<Element^>(treenode->Tag)->GetConcentration()==0.0)
                        {
                            red=true;
                        }
                    }
                }
                else
                {
                    if (safe_cast<Element^>(treenode->Tag)->GetIntensitySum()==0.0)
                    {
                        if (safe_cast<Element^>(treenode->Tag)->GetConcentration()==0.0)
                        {
                            red=true;
                        }
                    }
                }
                array<String^>^ dummy=safe_cast<Element^>(treenode->Tag)->GetRelevantLines();
                if (dummy->Length>1)
                {
                    int x;
                    String^ groupname=safe_cast<Element^>(treenode->Tag)->GetTransition(dummy[0])->GetGroup();
                    for(x=1;x<dummy->Length;x++)
                    {
                        if (groupname!=safe_cast<Element^>(treenode->Tag)->GetTransition(dummy[x])->GetGroup())
                            red=true; 
                    }
                }
            }
            if ((this->mode=="calculate intensities" || this->mode=="compare relative intensities")&& safe_cast<Element^>(treenode->Tag)->GetConcentration()==0.0)
                red=true;
            if (red)
                treenode->BackColor=Color::Red;
            else
                treenode->BackColor=Color::LightGreen;
        } 
    } 
}

注意:在加载包含数据的文件时,应用程序使用少于50 MB的Ram。

1 个答案:

答案 0 :(得分:0)

我同意@Dmitry-T你可能需要进行很多字体更改;只需创建一次bold-face等,然后重用: 而不是代码如:

 treenode->NodeFont=gcnew System::Drawing::Font(treenode->TreeView->Font,FontStyle::Bold);

在函数开始时声明BoldFont并执行以下操作:

 if (!BoldFont) BoldFont=gcnew System::Drawing::Font(treenode->TreeView->Font,FontStyle::Bold);
 treenode->NodeFont=BoldFont;