在 C++ 中通过析构函数销毁动态数组的向量
Destroying vectors of dynamic arrays via destructor in c++
我正在做一项与图像处理(混合和缩放)有关的作业,我 运行 遇到了一个我很难克服的问题。
我的应用程序结构
Class 图片
- rgb 结构:包含浮点数(它们是扁平像素)和重载运算符。
- 像素:一维像素数组,通过构造函数初始化为 h * w
- Class 析构函数。
析构函数看起来有点像这样...
virtual ~Image()
{
if (pixels != NULL)
delete[] pixels;
}
现在我正在使用另一个名为 Filter 的 class,它继承自 Image
class 过滤器:public class 图片
- std::图像imgStack的向量;我要混合的图像的容器
- std::Rgb pixelBuffer 的向量;每个图像一个像素的像素容器。这不是动态的,所以我不担心删除它。
派生的析构函数 class。
~Blend()
{
delete &imageStack;
}
我的 main.cpp 这部分是什么样的...
while (userInput != 5)
{
switch(userInput)
case 1:
{
Blend *meanImage = new Blend(3263, 2505, 13);
meanImage->writePPM("Mean_" + std::to_string(count) + ".ppm");//every iteration, the file name is unique
meanImage->~Blend();
}
}
在我的 main.cpp 中,我基本上 运行 将 13 张图像放入 Blend 对象中,该对象将图像存储在矢量容器中以执行我的所有功能。在 运行 期间,使用的 space 约为 1.3GB,但由于我的对象处于循环中(我有一个用于多种功能的菜单),该对象通常不会离开作用域,因此析构函数是'自动调用,所以我是这样手动调用的; medianImage->~Blend();现在所有的错误都说我的应用程序 "has triggered a breakpoint" 就是这样......注意,在任何地方都找不到断点。我知道使用动态数组通常是不好的,因为它会导致各种内存问题(如果它是由我完成的),但我想解决这个问题,以便我知道将来如何解决这些问题。
如果您对代码有任何疑问,我可以post snippers。
编辑:这是我的 Blend class。
#pragma once
#include "stdafx.h"
#include "Image.h"
#include <vector>
#include <string>
class Blend : public Image
{
private:
std::vector<Image> imageStack;
std::vector<Rgb*> pixelBuffer;//only works with pointers (no copying)
int m_noOfImages;
Rgb* getMedianFromSet();
Rgb getMeanFromSet();
Rgb getStdDev(Rgb meanPix);
public:
Blend(int width = 0, int height = 0, int noOfImages = 0):Image(width, height), m_noOfImages(noOfImages), imageStack(noOfImages), pixelBuffer(noOfImages)
{}
public:
void stack(bool push = true);
void meanBlend();
void medianBlend();
void sigmaClipping(float sigma = 0.5f);
~Blend()
{
delete &imageStack;
}
};
#pragma once
#include "stdafx.h"
#include "Image.h"
#include <vector>
#include <string>
#include <memory>
class Blend : public Image
{
private:
std::vector<Image> imageStack;
// Changed to shared_ptr<T> could use unique_ptr<T> depending on need.
std::vector<std::shared_ptr<Rgb>> pixelBuffer;//only works with pointers (no copying)
int m_noOfImages;
Rgb* getMedianFromSet();
Rgb getMeanFromSet();
Rgb getStdDev(Rgb meanPix);
public:
Blend(int width = 0, int height = 0, int noOfImages = 0):Image(width, height), m_noOfImages(noOfImages), imageStack(noOfImages), pixelBuffer(noOfImages)
{}
public:
void stack(bool push = true);
void meanBlend();
void medianBlend();
void sigmaClipping(float sigma = 0.5f);
// Clear Entire Buffer
void cleanup() {
// When using the heap with smart pointers
for ( auto item : containerVariable ) {
item.reset();
item = nullptr;
}
containerVariable.clear();
}
// Remove Single Element
void remove( unsigned idx ) {
// Write function to remove a single element from vector
}
~Blend()
{
// This is definitely wrong here: You are trying to delete a reference
// to a template container that is storing `Image` objects that
// are on the stack.
// delete &imageStack;
}
};
最好编写一个函数来清理内存,并在使用动态内存时从容器中删除特定元素,而不是使用 class 的析构函数。
我正在做一项与图像处理(混合和缩放)有关的作业,我 运行 遇到了一个我很难克服的问题。
我的应用程序结构
Class 图片
- rgb 结构:包含浮点数(它们是扁平像素)和重载运算符。
- 像素:一维像素数组,通过构造函数初始化为 h * w
- Class 析构函数。
析构函数看起来有点像这样...
virtual ~Image()
{
if (pixels != NULL)
delete[] pixels;
}
现在我正在使用另一个名为 Filter 的 class,它继承自 Image
class 过滤器:public class 图片
- std::图像imgStack的向量;我要混合的图像的容器
- std::Rgb pixelBuffer 的向量;每个图像一个像素的像素容器。这不是动态的,所以我不担心删除它。
派生的析构函数 class。
~Blend()
{
delete &imageStack;
}
我的 main.cpp 这部分是什么样的...
while (userInput != 5)
{
switch(userInput)
case 1:
{
Blend *meanImage = new Blend(3263, 2505, 13);
meanImage->writePPM("Mean_" + std::to_string(count) + ".ppm");//every iteration, the file name is unique
meanImage->~Blend();
}
}
在我的 main.cpp 中,我基本上 运行 将 13 张图像放入 Blend 对象中,该对象将图像存储在矢量容器中以执行我的所有功能。在 运行 期间,使用的 space 约为 1.3GB,但由于我的对象处于循环中(我有一个用于多种功能的菜单),该对象通常不会离开作用域,因此析构函数是'自动调用,所以我是这样手动调用的; medianImage->~Blend();现在所有的错误都说我的应用程序 "has triggered a breakpoint" 就是这样......注意,在任何地方都找不到断点。我知道使用动态数组通常是不好的,因为它会导致各种内存问题(如果它是由我完成的),但我想解决这个问题,以便我知道将来如何解决这些问题。
如果您对代码有任何疑问,我可以post snippers。
编辑:这是我的 Blend class。
#pragma once
#include "stdafx.h"
#include "Image.h"
#include <vector>
#include <string>
class Blend : public Image
{
private:
std::vector<Image> imageStack;
std::vector<Rgb*> pixelBuffer;//only works with pointers (no copying)
int m_noOfImages;
Rgb* getMedianFromSet();
Rgb getMeanFromSet();
Rgb getStdDev(Rgb meanPix);
public:
Blend(int width = 0, int height = 0, int noOfImages = 0):Image(width, height), m_noOfImages(noOfImages), imageStack(noOfImages), pixelBuffer(noOfImages)
{}
public:
void stack(bool push = true);
void meanBlend();
void medianBlend();
void sigmaClipping(float sigma = 0.5f);
~Blend()
{
delete &imageStack;
}
};
#pragma once
#include "stdafx.h"
#include "Image.h"
#include <vector>
#include <string>
#include <memory>
class Blend : public Image
{
private:
std::vector<Image> imageStack;
// Changed to shared_ptr<T> could use unique_ptr<T> depending on need.
std::vector<std::shared_ptr<Rgb>> pixelBuffer;//only works with pointers (no copying)
int m_noOfImages;
Rgb* getMedianFromSet();
Rgb getMeanFromSet();
Rgb getStdDev(Rgb meanPix);
public:
Blend(int width = 0, int height = 0, int noOfImages = 0):Image(width, height), m_noOfImages(noOfImages), imageStack(noOfImages), pixelBuffer(noOfImages)
{}
public:
void stack(bool push = true);
void meanBlend();
void medianBlend();
void sigmaClipping(float sigma = 0.5f);
// Clear Entire Buffer
void cleanup() {
// When using the heap with smart pointers
for ( auto item : containerVariable ) {
item.reset();
item = nullptr;
}
containerVariable.clear();
}
// Remove Single Element
void remove( unsigned idx ) {
// Write function to remove a single element from vector
}
~Blend()
{
// This is definitely wrong here: You are trying to delete a reference
// to a template container that is storing `Image` objects that
// are on the stack.
// delete &imageStack;
}
};
最好编写一个函数来清理内存,并在使用动态内存时从容器中删除特定元素,而不是使用 class 的析构函数。