Winforms 我必须取消分配gcnew System::EventHandler吗?
所以我在windows窗体中制作了我的第一个windows应用程序,我注意到当我连续多次按下按钮时,RAM的使用率会上升。我以为我有内存泄漏,我发现:Winforms 我必须取消分配gcnew System::EventHandler吗?,winforms,c++-cli,Winforms,C++ Cli,所以我在windows窗体中制作了我的第一个windows应用程序,我注意到当我连续多次按下按钮时,RAM的使用率会上升。我以为我有内存泄漏,我发现: this->button_to_decompress->Click += gcnew System::EventHandler(this, &MyForm::button_to_decompress_Click); 我必须手动解除分配吗?如果是,我该怎么做 我的全部代码: #pragma once #include <
this->button_to_decompress->Click += gcnew System::EventHandler(this, &MyForm::button_to_decompress_Click);
我必须手动解除分配吗?如果是,我该怎么做
我的全部代码:
#pragma once
#include <msclr/marshal_cppstd.h>
#include "tree.h"
using namespace msclr::interop;
namespace CompressionTool {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO;
/// <summary>
///
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
}
protected:
/// <summary>
///
/// </summary>
~MyForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::ProgressBar^ progressBar1;
protected:
private: System::Windows::Forms::Button^ button_to_compress;
private: System::Windows::Forms::Button^ button_to_decompress;
private: System::Windows::Forms::OpenFileDialog^ openFileDialog1;
private: System::Windows::Forms::OpenFileDialog^ openFileDialog2;
private: System::Windows::Forms::SaveFileDialog^ saveFileDialog1;
private: System::Windows::Forms::SaveFileDialog^ saveFileDialog2;
private:
/// <summary>
///
/// </summary>
System::ComponentModel::Container^ components;
#pragma region Windows Form Designer generated code
/// <summary>
///
///
/// </summary>
void InitializeComponent(void)
{
this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar());
this->button_to_compress = (gcnew System::Windows::Forms::Button());
this->button_to_decompress = (gcnew System::Windows::Forms::Button());
this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog());
this->openFileDialog2 = (gcnew System::Windows::Forms::OpenFileDialog());
this->saveFileDialog1 = (gcnew System::Windows::Forms::SaveFileDialog());
this->saveFileDialog2 = (gcnew System::Windows::Forms::SaveFileDialog());
this->SuspendLayout();
//
// progressBar1
//
this->progressBar1->Location = System::Drawing::Point(12, 144);
this->progressBar1->Margin = System::Windows::Forms::Padding(3, 2, 3, 2);
this->progressBar1->MarqueeAnimationSpeed = 20;
this->progressBar1->Name = L"progressBar1";
this->progressBar1->Size = System::Drawing::Size(557, 60);
this->progressBar1->Style = System::Windows::Forms::ProgressBarStyle::Marquee;
this->progressBar1->TabIndex = 0;
this->progressBar1->Value = 100;
//
// button_to_compress
//
this->button_to_compress->Location = System::Drawing::Point(12, 12);
this->button_to_compress->Margin = System::Windows::Forms::Padding(3, 2, 3, 2);
this->button_to_compress->Name = L"button_to_compress";
this->button_to_compress->Size = System::Drawing::Size(131, 60);
this->button_to_compress->TabIndex = 1;
this->button_to_compress->Text = L"Compress file";
this->button_to_compress->UseVisualStyleBackColor = true;
this->button_to_compress->Click += gcnew System::EventHandler(this, &MyForm::button_to_compress_Click);
//
// button_to_decompress
//
this->button_to_decompress->Location = System::Drawing::Point(12, 78);
this->button_to_decompress->Margin = System::Windows::Forms::Padding(3, 2, 3, 2);
this->button_to_decompress->Name = L"button_to_decompress";
this->button_to_decompress->Size = System::Drawing::Size(131, 60);
this->button_to_decompress->TabIndex = 2;
this->button_to_decompress->Text = L"Decompress file";
this->button_to_decompress->UseVisualStyleBackColor = true;
this->button_to_decompress->Click += gcnew System::EventHandler(this, &MyForm::button_to_decompress_Click);
//
// openFileDialog1
//
this->openFileDialog1->FileName = L"openFileDialog1";
//
// openFileDialog2
//
this->openFileDialog2->FileName = L"openFileDialog2";
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(581, 217);
this->Controls->Add(this->button_to_decompress);
this->Controls->Add(this->button_to_compress);
this->Controls->Add(this->progressBar1);
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedToolWindow;
this->Margin = System::Windows::Forms::Padding(3, 2, 3, 2);
this->MaximizeBox = false;
this->Name = L"MyForm";
this->Text = L"CompressionTool";
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void button_to_compress_Click(System::Object^ sender, System::EventArgs^ e)
{
OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;
openFileDialog1->InitialDirectory = "c:\\";
openFileDialog1->Filter = "txt files (*.txt)|*.txt";
openFileDialog1->FilterIndex = 1;
openFileDialog1->RestoreDirectory = true;
if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
Node node;
Tree tree;
const char* directory;
marshal_context^ context = gcnew marshal_context();
directory = context->marshal_as<const char*>(openFileDialog1->FileName);
tree.root = node.first_read_of_the_file(directory);
node.sort_list(tree.root);
node.make_tree(tree.root);
tree.create_compressed_file(directory);
tree.delete_tree(tree.root);
SaveFileDialog^ saveFileDialog1 = gcnew SaveFileDialog;
saveFileDialog1->InitialDirectory = "c:\\";
saveFileDialog1->Filter = "huf files (*.huf)|*.huf";
saveFileDialog1->FilterIndex = 1;
saveFileDialog1->RestoreDirectory = true;
if (saveFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
directory = context->marshal_as<const char*>(saveFileDialog1->FileName);
int result = rename("compressed.huf", directory);
}
else
remove("compressed.huf");
delete context;
delete saveFileDialog1;
}
delete openFileDialog1;
}
private: System::Void button_to_decompress_Click(System::Object^ sender, System::EventArgs^ e)
{
OpenFileDialog^ openFileDialog2 = gcnew OpenFileDialog;
openFileDialog2->InitialDirectory = "c:\\";
openFileDialog2->Filter = "huf files (*.huf)|*.huf";
openFileDialog2->FilterIndex = 1;
openFileDialog2->RestoreDirectory = true;
if (openFileDialog2->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
Tree tree;
const char* directory;
marshal_context^ context = gcnew marshal_context();
directory = context->marshal_as<const char*>(openFileDialog2->FileName);
tree.decompress_file(directory);
tree.delete_tree(tree.root);
SaveFileDialog^ saveFileDialog2 = gcnew SaveFileDialog;
saveFileDialog2->InitialDirectory = "c:\\";
saveFileDialog2->Filter = "txt files (*.txt)|*.txt";
saveFileDialog2->FilterIndex = 1;
saveFileDialog2->RestoreDirectory = true;
if (saveFileDialog2->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
directory = context->marshal_as<const char*>(saveFileDialog2->FileName);
int result = rename("decompressed.txt", directory);
}
else
remove("decompressed.txt");
delete context;
delete saveFileDialog2;
}
delete openFileDialog2;
}
};
}
#pragma一次
#包括
#包括“tree.h”
使用名称空间msclr::interop;
名称空间压缩工具{
使用名称空间系统;
使用名称空间System::ComponentModel;
使用名称空间系统::集合;
使用命名空间System::Windows::Forms;
使用名称空间System::Data;
使用名称空间系统::绘图;
使用名称空间系统::IO;
///
///
///
公共引用类MyForm:public System::Windows::Forms::Form
{
公众:
我的表格(作废)
{
初始化组件();
}
受保护的:
///
///
///
~MyForm()
{
if(组件)
{
删除组件;
}
}
私有:系统::Windows::窗体::ProgressBar^progressBar1;
受保护的:
私有:系统::Windows::窗体::按钮^Button\u压缩;
私有:系统::Windows::窗体::按钮^Button\u解压;
私有:系统::Windows::窗体::OpenFileDialog^openFileDialog1;
私有:系统::Windows::窗体::OpenFileDialog^openFileDialog2;
private:System::Windows::Forms::SaveFileDialog^saveFileDialog1;
private:System::Windows::Forms::SaveFileDialog^saveFileDialog2;
私人:
///
///
///
系统::组件模型::容器^组件;
#pragma区域Windows窗体设计器生成的代码
///
///
///
///
void初始化组件(void)
{
这->progressBar1=(gcnew System::Windows::Forms::ProgressBar());
这个->按钮压缩=(gcnewsystem::Windows::Forms::button());
这个->按钮到解压=(gcnew System::Windows::Forms::button());
这->openFileDialog1=(gcnew System::Windows::Forms::OpenFileDialog());
这->openFileDialog2=(gcnew System::Windows::Forms::openFileDialog2());
这->saveFileDialog1=(gcnew System::Windows::Forms::SaveFileDialog());
这->saveFileDialog2=(gcnew System::Windows::Forms::saveFileDialog2());
此->SuspendLayout();
//
//progressBar1
//
这->progressBar1->位置=系统::绘图::点(12144);
这->progressBar1->Margin=System::Windows::Forms::Padding(3,2,3,2);
此->进度条1->MarqueeAnimationSpeed=20;
此->progressBar1->Name=L“progressBar1”;
这->progressBar1->尺寸=系统::图纸::尺寸(557,60);
这->progressBar1->Style=System::Windows::Forms::ProgressBarStyle::Marquee;
此->progressBar1->TabIndex=0;
此->进度条1->值=100;
//
//按钮\u至\u压缩
//
此->按钮压缩->位置=系统::绘图::点(12,12);
这个->按钮\u to \u compress->Margin=System::Windows::Forms::Padding(3,2,3,2);
此->按钮压缩->名称=L“按钮压缩”;
这个->按钮压缩->尺寸=系统::绘图::尺寸(131,60);
此->按钮要压缩->选项卡索引=1;
此->按钮\u to \u compress->Text=L“压缩文件”;
此->按钮要压缩->UseVisualStyleBackColor=true;
此->按钮\u压缩->单击+=gcnew System::EventHandler(此,&MyForm::按钮\u压缩\u单击);
//
//解压按钮
//
这个->按钮到解压->位置=系统::绘图::点(12,78);
这个->按钮到解压->边距=系统::窗口::窗体::填充(3,2,3,2);
此->按钮到解压->名称=L“按钮到解压”;
这个->按钮被解压->大小=系统::绘图::大小(131,60);
此->按钮\u至\u解压->选项卡索引=2;
此->按钮\u至\u解压->文本=L“解压文件”;
此->按钮\u至\u解压缩->UseVisualStyleBackColor=true;
此->按钮解压缩->单击+=gcnew System::EventHandler(此,&MyForm::按钮解压缩\u单击);
//
//openFileDialog1
//
此->openFileDialog1->文件名=L“openFileDialog1”;
//
//openFileDialog2
//
此->openFileDialog2->文件名=L“openFileDialog2”;
//
//MyForm
//
这->自动缩放尺寸=系统::图纸::尺寸(8,16);
这->AutoScaleMode=System::Windows::Forms::AutoScaleMode::Font;
这->ClientSize=System::Drawing::Size(581217);
此->控制->添加(此->按钮\u至\u解压);
此->控制->添加(此->按钮要压缩);
此->控制->添加(此->进度条1);
这->FormBorderStyle=System::Windows::Forms::FormBorderStyle::FixedToolWindow;
这->边距=系统::窗口::窗体::填充(3,2,3,2);
此->MaximizeBox=false;
此->名称=L“MyForm”;
此->文本=L“压缩工具”;
此->恢复布局(错误);
}
#布拉格端区
私有:系统::无效按钮\u压缩\u单击(系统::对象^sender,系统::事件参数^e)
{
OpenFileDialog^openFileDialog1=gcnew OpenFileDialog;
openFileDialog1->InitialDirectory=“c:\\”;
openFileDialog1->Filter=“txt文件(*.txt)|*.txt”;
openFileDialog1->FilterIndex=1;
openFileDialog1->RestoreDirectory=true;
如果(openFileDialog1->ShowDialog()==System::Windows::Forms::DialogResult::OK)
{
节点;