c++ - New and delete operators without effect on the contents of a Deque of pointers to this class -
i have problem have been tackling since last 2 days, rather inexperienced programmer normal assume. question may have easy solution, couldn't find quick reference, decided ask others.
in 1 thread of application, create new
, pointer user defined class. after it, push new created pointer deque
of pointers user defined class, push_back()
method.
this deque under concern had been passed thread main function of application, contents of user-defined class processed in main loop. thus, when new pointer enters deque, taken in main thread, , used.
my problem need make sure memory allocated contents of class should deallocated after use, because store large image arrays. otherwise, program crashes. this, have used delete
after push_back()
call in thread, assuming memory free, stored in deque
. however, apparently, contents of address pointed pointer in deque
deleted. not desire.
is there way decouple both, maybe without introducing pointers? think new
, delete
valid in pointers. more precisely, there way pass contents of class deque
, delete it, without affecting in deque
?
instead of using raw pointers, consider using boost pointer container library:
boost.pointer container provides containers holding heap-allocated objects in exception-safe manner , minimal overhead. aim of library in particular make oo programming easier in c++ establishing standard set of classes, methods , designs dealing oo specific problems
but valid choice if:
the stored objects not shared, owned exclusively, or overhead implied smart pointers inappropriate
otherwise, if image file pointers shared, consider using std::shared_ptr
pointed out in comments.
if boost pointer container fits needs, can use boost::ptr_deque
Comments
Post a Comment