我正在尝试将大尺寸对象“推回”到STL容器(deque)中。但我不能'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;
}
答案 0 :(得分:0)
std::deque::push_back
不应该抛出std::length_error
,除非你在双端队列中有数十亿个元素。 (在这种情况下,可能没有足够的内存来保存所有元素,并且它与deque本身无关。)
如果您实际使用的内存过多,则会获得std::bad_alloc
(至少,如果您使用的是默认分配器)。
由于情况并非如此,我的猜测是你的代码中某处存在未定义的行为,导致deque的内部内存以某种方式被破坏。
答案 1 :(得分:0)
使用C ++ 11,您可以使大对象不可复制,然后将对象置于容器中,或将其移入,而不是插入对象的副本。
答案 2 :(得分:-1)
在STL容器中存储指向大对象的指针。
std::list<std::shared_ptr<LargeData>> largeObjects;