I've been looking into smart pointers, unit testing how they manage memory and am finding and unexpected issue that all the examples recommend doing, but it creates a huge memory leak for me.
This seems to occur when I use a class that has a constructor that builds from another copy of the same class. I'll give an example.
If I have a class like:
Class foo{
public:
//Ignore unsafe practices here
HeavyInMemory* variable;
foo(){
variable = new HeavyInMemory();
}
foo(foo* copyThis){
variable = nullptr;
if(copyThis){
variable = new HeavyInMemory(copyThis->variable);
}
}
~foo(){
delete variable;
}
}
I find that I will get a huge memory leak because std::make_shared has no way to tell the difference between make_shared(args) and make_shared(new T)
Main(){
for(int i =0; i < 100; i++{
//Should not leak, if I follow examples of how to use make_shared
auto test = make_shared<foo>(new foo());
}
//Checking memory addresses these do not match, checking total program memory use, leaks like a
//sieve.
}
Am I misunderstanding something?
Do the examples just not consider this as most use primitive types as examples rather than classes.
Does c++11 just not support the make_shared(new T) format even though I see old books like scott meyers books from 1992. It just doesn't make sense.
Also why would you use make_shared(new T) over make_shared(args)? I've seen a couple threads where people have asked this on here, but neither seemed to actually answer the question with a code example.
//As they mainly say code compiler order causes the leak but in my example this would still leak:
auto originalObject = new foo();
auto expectedDestructorWhenOutofScope = make_shared<foo>(originalObject);
//I have found if I give if the object instead it doesn't leak, but this is getting into the realms of
//hacks that may sometimes work
auto originalObject = new foo();
auto expectedDestructorWhenOutofScope = make_shared<foo>(*originalObject);
EDIT:
Thanks to Igor Tandetnik I now see I am using make_shared entirely wrong. It should be used as a constructor. Thanks again Igor I appreciate it.
//Create new
auto expectedDestructorWhenOutofScope = make_shared<foo>();
//Use object already created
std::shared_ptr<Object> p2(new foo())
Problem: I have a class of parameters which I'm passing around as const Some_Class& param because these parameters aren't changing. I need to pass these parameters to external library (GSL) which is accepting void* param. I can't cast from const& to void*, except with using const_cast. I heared that const_cast is not generally right solution, is this the correct use case for it?
My solution: As a solution I'm now using wrapper structure
struct wrapper{const Some_class& param;};
void gsl_func(void* param){
const Some_class& my_param = static_cast<wrapper*>(param)->param;
}
void my_func(const Some_class& my_param){
wrapper my_wrapper = {my_param};
gsl_func(&my_wrapper);
}
Which doesn't seems like the most elegant solution as I have to do this before every call to GSL. Is there some standardize way how to do this better?
CPP check is complaining about this construction:
void setThing(std::shared_ptr<Thing> theThing)
{
memberThing = theThing;
}
where memberThing is a std::shared_ptr<Thing>.
When changing to:
void setThing(const std::shared_ptr<Thing>& theThing);
cppcheck is not complaining anymore.
I need a shared pointer, I can't guarantee theThing will exist forever, so I don't want to have a member reference. But why is a const reference working when assigning it to a not-reference member? And what happens actually? Is the shared pointer copied to memberThing? Where did the reference go?
And actually exactly the same question for
void setThings(std::vector<std::shared_ptr<Thing>> theThings)
{
memberThings = theThings;
}
where memberThing is a std::vector<std::shared_ptr<Thing>>.
When using this construction, what is happening with the vector? Is it copied? What happens when the original vector is destroyed? Can I better use the first version or the second?
I am quite confused a const vector& can be assigned to a vector (not-reference). Is that a good idea anyway? What is the best thing to do in this situation? And why?
I just upgraded my V8 version to 3.20.16 (from some very old version). I can no longer use
Handle<Object> obj /* = ... */;
Persistent<Object> p = Persistent<Object>::New( obj );
to create a persistent handle to an object. The compiler suggests using static T* v8::Persistent<T>::New(v8::Isolate*, T*) [with T = v8::Object] instead. However, if I change my code to
Handle<Object> obj /* = ... */;
Persistent<Object> p = Persistent<Object>::New( Isolate::GetCurrent(), *obj );
the compiler complains that this function is private. How do I create a Persistent<Object> handle from a normal Handle<Object> now?
I've googled and the only thing I found was that the documentations seem to contradict each other:
https://developers.google.com/v8/embed#handles says that persistent handles are now created using the Persistence constructor
http://bespin.cz/~ondras/html/classv8_1_1Persistent.html indicates that Persistence<T>::New is still the way to go
thanks for any help in advance
There is a constructor that accepts normal Handle<T> you don't need to dereference it.
Persistent<Object>::New(Isolate::GetCurrent(), obj)
should work.
I have a good idea on how this structure works and how to update it, however when it comes to work with Lazy Propagation I don't know what to do, as many many many problems requires this to pass in competitions I want to know how to make it work.
I am trying this problem on spoj: http://www.spoj.com/problems/CDC12_H/
If somebody can explain me how the lazy propagation can be adapted to this situation I will take that and work on the idea, I really don't want to post my code because the idea for me is to make this work by myself but with a little help.
I hope someone comes with the solution to my problem.
This is my snippet of segment tree implementation with lazy propagation.
Hope this will help you.
#define int long long
#define MAX 100005*3
int stree[MAX],lazy[MAX];
void update(int cur,int cur_lft,int cur_rgt,int st,int en,int val)
{
if(cur_lft>en || cur_rgt<st) return ;
if(cur_lft>=st && cur_rgt<=en)
{
stree[cur]+=val*(cur_rgt-cur_lft+1);
lazy[cur]+=val;
return;
}
int l=cur<<1,r=(cur<<1)+1,mid=(cur_lft+cur_rgt)>>1;
update(l,cur_lft,mid,st,en,val);
update(r,mid+1,cur_rgt,st,en,val);
stree[cur]=stree[l]+stree[r]+lazy[cur]*(cur_rgt-cur_lft+1);
}
int query(int cur,int cur_lft,int cur_rgt,int st,int en,int lzy)
{
if(cur_lft>en || cur_rgt<st) return 0;
if(cur_lft>=st && cur_rgt<=en) return stree[cur]+lzy*(cur_rgt-cur_lft+1);
int l=cur<<1,r=(cur<<1)+1,mid=(cur_lft+cur_rgt)>>1;
int left_tree=query(l,cur_lft,mid,st,en,lzy+lazy[cur]);
int right_tree=query(r,mid+1,cur_rgt,st,en,lzy+lazy[cur]);
return left_tree+right_tree;
}
Edit
To update and query into segment tree we can call following functions:
query(1,0,n-1,lower_range,upper_range,0));
update(1,0,n-1,lower_range,upper_range,v);