Changing body Velocity results in Segmentation Fault

Official forum for the Chipmunk2D Physics Library.
User avatar
Tam Toucan
Posts: 141
Joined: Tue Jun 23, 2009 4:26 pm
Contact:

Re: Changing body Velocity results in Segmentation Fault

Post by Tam Toucan »

TurboLento wrote:
Tam Toucan wrote:Actually what you are doing is valid, but not best practice. When you create a local object on the stack then yes it disappears when it goes out of scope, but putting it on the vector will take a copy of the object i.e. create a new copy of the object. That's why it's not best practice. You are creating/copying/destroying entire objects. Using the pointer means you create one new object and then copy the pointer into the vector and the original pointer goes out of scope (so it's actually the same thing, but dealing with pointers not entire objects).
I hadn't thought of this problem, it has never happened to me. Do you have any suggestions on how to create objects in functions and add it to a vector to avoid it?
Not sure what you mean by "this problem". What you're doing now by new'ing the objects and storing pointers is the usual way to use containers. I would only store very simple objects where the copy overhead is minimal. In fact for most of my complex objects copying is explicitly blocked (and I'll have a clone method if needed).
TurboLento
Posts: 11
Joined: Mon Feb 22, 2010 11:21 pm
Contact:

Re: Changing body Velocity results in Segmentation Fault

Post by TurboLento »

Ok, I misread as "you're still not doing it right, as the pointer itself will go out of scope as well and the problem will return". Glad that it's correct now :) Thanks for all the help.
Post Reply

Who is online

Users browsing this forum: Heise IT-Markt [Crawler] and 11 guests