Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible memory leak? #24

Closed
ghost opened this issue Aug 12, 2014 · 6 comments
Closed

Possible memory leak? #24

ghost opened this issue Aug 12, 2014 · 6 comments

Comments

@ghost
Copy link

ghost commented Aug 12, 2014

I'm boread, and I checked your streamer.

In MoveDynamicObject I found something interesting..

cell AMX_NATIVE_CALL Natives::MoveDynamicObject(AMX *amx, cell *params)
{
CHECK_PARAMS(8, "MoveDynamicObject");
if (!amx_ctof(params[5]))
{
return 0;
}
boost::unordered_map<int, Item::SharedObject>::iterator o = core->getData()->objects.find(static_cast(params[1]));
if (o != core->getData()->objects.end())
{
if (o->second->attach)
{
sampgdk::logprintf("MoveDynamicObject: Object is currently attached and cannot be moved");
return 0;
}
Eigen::Vector3f position(amx_ctof(params[2]), amx_ctof(params[3]), amx_ctof(params[4]));
Eigen::Vector3f rotation(amx_ctof(params[6]), amx_ctof(params[7]), amx_ctof(params[8]));
o->second->move = boost::intrusive_ptrItem::Object::Move(new Item::Object::Move);
o->second->move->duration = static_cast((static_cast(boost::geometry::distance(position, o->second->position) / amx_ctof(params[5])) * 1000.0f));
o->second->move->position.get<0>() = position;
o->second->move->position.get<1>() = o->second->position;
o->second->move->position.get<2>() = (position - o->second->position) / static_cast(o->second->move->duration);
o->second->move->rotation.get<0>() = rotation;
if ((o->second->move->rotation.get<0>().maxCoeff() + 1000.0f) > std::numeric_limits::epsilon())
{
o->second->move->rotation.get<1>() = o->second->rotation;
o->second->move->rotation.get<2>() = (rotation - o->second->rotation) / static_cast(o->second->move->duration);
}
o->second->move->speed = amx_ctof(params[5]);
o->second->move->time = boost::chrono::steady_clock::now();
for (boost::unordered_map<int, Player>::iterator p = core->getData()->players.begin(); p != core->getData()->players.end(); ++p)
{
boost::unordered_map<int, int>::iterator i = p->second.internalObjects.find(o->first);
if (i != p->second.internalObjects.end())
{
StopPlayerObject(p->first, i->second);
MovePlayerObject(p->first, i->second, o->second->move->position.get<0>()[0], o->second->move->position.get<0>()[1], o->second->move->position.get<0>()[2], o->second->move->speed, o->second->move->rotation.get<0>()[0], o->second->move->rotation.get<0>()[1], o->second->move->rotation.get<0>()[2]);
}
}
core->getStreamer()->movingObjects.insert(o->second);
return static_cast(o->second->move->duration);
}
return 0;
}

o->second->move = boost::intrusive_ptrItem::Object::Move(new Item::Object::Move);

There is a memory leak, isn't? Because you never delete this pointer.

@zilberman-rafael
Copy link

No, the pointer o->second->move getting deleted after the object finished moving.
Also for the next time please put the code inside code block (https://guides.github.com/features/mastering-markdown/#GitHub-flavored-markdown).

@ghost
Copy link
Author

ghost commented Aug 12, 2014

Oh, my fail!

Thanks.

@ghost ghost closed this as completed Aug 12, 2014
@ghost ghost reopened this Aug 12, 2014
@ghost
Copy link
Author

ghost commented Aug 12, 2014

And get deleted, if you call MoveDynamicObject 2-3 timer, before object finished moving? Because i have not saw any delete here. Now i understood, If object has finished moving, o->second->move wil be deleted. But this should be deleted if somebody calls 2-3times MoveDyanmicObject before object finished moving and use just 1 pointer. (delete last pointer, allocate new)

@samp-incognito
Copy link
Owner

It's an intrusive_ptr, an efficient kind of smart pointer with an embedded reference count. The memory is automatically deallocated when the pointer to the object goes out of scope. In this case, since the assignment operator is equivalent to swap(*this), if anything was assigned to o->second->move, it will be destroyed as soon as the function exits since the old intrusive_ptr is essentially a local variable at that point.

@zilberman-rafael
Copy link

@samp-incognito It is not accurate, intrusive_ptr will deallocated when the reference count will get to zero, that means that all ownerships will be released. This will happen when you using reset for example.
So I was right, the pointer will be destroyed in Streamer::processMovingObjects, if you will take close look at the code you will found that when object is finished moving you calling:

(*o)->move.reset();

That destroying the pointer.

About @kurta999 comment: If you calling MovdeDynamicObject it's rewrites the pointer that means that the old pointer is released that causes him to be deleted.

More information can be found on wikipedia: http://en.wikipedia.org/wiki/Smart_pointer
And boost site:http://www.boost.org/doc/libs/1_55_0/libs/smart_ptr/smart_ptr.htm

@samp-incognito
Copy link
Owner

Yes, that's correct. When I mentioned the intrusive_ptr going out of scope, I meant the reference count reaching zero.

In any case, this isn't a memory leak, but thanks for reviewing the code anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants