【问题标题】:How can I 'push_back' large size objects in STL containers?如何在 STL 容器中“推回”大尺寸对象?
【发布时间】:2014-07-24 02:57:45
【问题描述】:

我正在尝试将大型对象“推回”到 STL 容器(双端队列)中。但我不能“push_back”使用大内存的对象。

Try-catch 块获取 std::length_error 异常。

如何在 STL 容器中存储大而复杂的对象?

typedef std::deque <
    LargeData,
    boost::pool_allocator <
    LargeData
    , boost::default_user_allocator_new_delete
    , boost::details::pool::default_mutex
    , 32
    , 0
    >
> LargeDataDeque;

LargeDataDeque v;
LargeData d;
try
{ 
    v.push_back(d); 
}
catch( std::length_error &e )
{
    cout << e.what();// this line outputs 'too long'
}

//LargeData body
struct LargeData
{
std::wstring name;
CRect desktop_rect;

// computer vision attributes
bool is_cv_target;
bool is_vm_master;
bool is_face_detect_target;
bool is_facemask;
FaceMaskPart facemask_part;
std::deque<std::wstring> masters;
std::deque<std::wstring> slaves;
CPoint facetrack_com;
//

CImage image;
CPoint com;
Vec2 curr_coord_ratio; // %
Vec2 prev_coord_ratio; // %
Vec2 prev_size_ratio;
//CPoint translation;
Vec2 translation;
CPoint trans_diff;
double scale;
double scale_diff;
double degree;
CRect region;
CartesianRect rot_region;
CRect initial_region;
CartesianRect initial_rot_region;

CRect orig_bmp_region; // special tmp data for bmp-wipes
int cap_number;
std::wstring id;
CRect tmp_region;
int depth;
int alpha;

// cv
IplImage *effect_frame;
IplImage *effect_tmp_frame;
IplImage *gray_frame;
IplImage *sepia_frame;

bool use_brightness;
double brightness;
bool use_contrast;
double contrast;
bool use_blur;
int blur_intensity;

bool blur_face;
cv::Mat ft_frame, smallImg;
cv::CascadeClassifier cascade_face_cpu;
std::deque<FaceTrackRegions> facetrack_regions;

bool flip_vertical;
bool flip_horizontal;
bool flip_hv;
bool use_negaposi;
bool use_sepia;

CvMat *cvm_clusters;
CvMat *cvm_points;
CvMat *cvm_color;// = cvCreateMat (MAX_CLUSTERS, 1, CV_32FC3);
CvMat *cvm_count;// = cvCreateMat (MAX_CLUSTERS, 1, CV_32SC1);
CvMat *cvm_centers;// = cvCreateMat (MAX_CLUSTERS, 1, CV_32SC1);

bool use_erode;
bool use_dilate;
int erode_iteration;
int dilate_iteration;

bool use_threshold;
int min_threshold;
int max_threshold;
int threshold_type;
// cv

std::deque<WipeEffect> effects;

bool selected;
bool activated;
bool visible;

WipeSourceType type;
WipeAlignment alignment;

int imagesource_index;
CString image_path;
FIBITMAP* fib_image;
double image_ratio;
bool use_alpha_blend;
bool use_chromakey;

// WS_Bitmap specific
CPoint last_diff_from_ecom;

// Direct2D
// captured bitmap
D2D1_SIZE_U d2dcapbitmap_size;
unsigned int d2dbitmap_bytes;
unsigned int d2dbitmap_pitch;
CComQIPtr<ID2D1Bitmap> d2dcapimage;

LPVOID *d2dtmpbits;
boost::shared_ptr<BYTE> d2dcapbits;
HBITMAP hbm_d2dtmpbits;
HBITMAP hbm_d2dtmpbits_old;
HDC hdc_d2dtmpbits;
BITMAPINFO bi_d2dtmpbits;

// CaptureWorker thread stuffs
boost::shared_ptr<CaptureWorker> capture_worker;
boost::shared_ptr<boost::thread> capture_worker_thread;
//boost::mutex capture_mutex;

// static bitmap
D2D1_SIZE_U d2dstaticbitmap_size;
BITMAPINFO bi_d2dtmpbmpbits;

BYTE *d2dorigbmpbits;
HDC hdc_d2dorigbmpbits;
HBITMAP hbm_d2dorigbmpbits;

// static bitmap's working resources
BYTE *d2dworkbmpbits;
HDC hdc_d2dworkbmpbits;
HBITMAP hbm_d2dworkbmpbits;
CComQIPtr<ID2D1Bitmap> d2dbitmap;

/*CamImageGrabber camgrabber;
CSize prev_cam_reso;*/
CamDeviceInfo caminfo;


// Video frame stuffs
boost::shared_ptr<Video> vid;
bool vid_ready;
bool vid_loop;
bool vid_use_slomo;
boost::shared_ptr<BYTE> vid_buff;
}

【问题讨论】:

  • deque::push_back 不会抛出 length_error。您确定LargeData 的复制构造函数没有抛出错误吗?您应该使类移动可构造并使用v.push_back(std::move(d));
  • 也可以考虑使用emplace_back
  • 我尝试了 emplace_back 并且肯定使用了 deque。但是 length_error 已被抛出。并且 what() 返回 'vector too long'。这是正确的行为吗?我正在使用 VC++2013,boost1.55,Windows8.1 64bit。
  • 我在 myapp.exe 中的 0x76991D4D 处出现未处理的异常:Microsoft C++ 异常:内存位置 0x0099DCC0 处的 std::length_error。在 boost::pool_allocator::construct 上。
  • 请在您的问题正文中填写所有相关信息。

标签: c++ exception stl containers


【解决方案1】:

std::deque::push_back 不应该抛出 std::length_error 除非你在双端队列中有数十亿个元素。 (在这种情况下,可能没有足够的内存来保存所有元素,并且与双端队列本身无关。)

如果您实际上使用了太多内存,您会得到std::bad_alloc(至少,如果您使用的是默认分配器)。

由于情况并非如此,我的猜测是您的代码中某处存在未定义的行为,这导致双端队列的内部内存以某种方式损坏。

【讨论】:

  • 我在 VC++2010、boost1.46 和 Windows7 64bit 中使用工作代码。但在VC++2013、boost1.55和Windows8.1 64bit上不行。
  • @user3840701 您的代码中可能仍然存在错误。未定义的行为意味着它可能有效,也可能无效。
  • 感谢您的回复。我正在检查。
  • 我在 myapp.exe 中的 0x76991D4D 处出现未处理的异常:Microsoft C++ 异常:内存位置 0x0099DCC0 处的 std::length_error。在 boost::pool_allocator::construct 上。
  • @tensor01 如果分配器抛出异常,则表示您的内存不足。虽然实际上我不确定现在发生了什么,因为据我所知 boost::pool_allocator 不应该抛出 std::length_error 。 boost.org/doc/libs/1_38_0/libs/pool/doc/interfaces/…
【解决方案2】:

使用 C++11,您可以使大型对象不可复制,然后将对象放置到容器中,或者将其移动到容器中,而不是插入对象的副本。

【讨论】:

  • 哦,这太可怕了。当我通过'new'分配大型数组时,我可以'push_back'。
  • 我决定使用池指针方法来避免这个问题并且我的应用程序运行良好。谢谢大家。
【解决方案3】:

将指向大型对象的指针存储在 STL 容器中。

std::list<std::shared_ptr<LargeData>> largeObjects;

【讨论】:

  • 我知道这个方法。但是在 VisualC++2010&boost1.46 和 Windows7 中不会出现这种“length_error 异常”。我现在用的是VC++2013&boost1.55。
  • 我决定使用池指针方法来避免这个问题并且我的应用程序运行良好。谢谢大家。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-03
  • 2016-09-09
相关资源
最近更新 更多