Pushing pointers into c++ vectors, and cleanup -
Pushing pointers into c++ vectors, and cleanup -
i converting code between different systems, , have question regarding c++ vectors.
if this:
in header file:
struct vertex { float x; float y; float z; } struct submesh { vertex *meshdata; } std::vector<submesh> meshes;
in routine in c++ file:
{ vertex *data = new vertex[1024]; submesh g; g.meshdata = data; meshes.push_back(g); delete [] data; }
will in trouble? assumption vector hold pointer info no longer valid 1 time called delete on it. need write re-create constructor vertex info copied first?
additional:
the question more how set pointer allocated memory std::vector<> , still cleanup locally allocated data. essentially, how re-create info vector can still clean copy.
the original code in directx. porting iphone. original code allocated submesh locally in routine using:
{ id3dxmesh* submesh = 0; d3dxcreatemesh(subgrid::num_tris, subgrid::num_verts, d3dxmesh_managed, elems, gd3ddevice, &submesh)); // // ... magical things submesh // subgrid g; g.mesh = submesh; g.box = bndbox; msubgrids.push_back(g); }
i trying duplicate how id3dxmesh able added vector, lose it's scope in routine.
as don't have access d3dxcreatemesh(), figured allocate vertices needed, throw them vector, , clean up.
sorry, wanted maintain nitty gritty details out of it, question how allocate chunk of data, set pointer std::vector<>, clean locally allocated memory. :)
i assumed re-create constructor had written somewhere. wasn't sure or how.
a subgrid looks this:
struct subgrid { id3dxmesh* mesh; aabb box; // sorting. bool operator<(const subgrid& rhs)const; const static int num_rows = 33; const static int num_cols = 33; const static int num_tris = (num_rows-1)*(num_cols-1)*2; const static int num_verts = num_rows*num_cols; };
and vector added looks like:
std::vector<subgrid> msubgrids;
don't straight dynamicly-allocate when don't need to, , in case don't. since you're filling own submesh info rather using id3dxmesh
, container of info should raii-compliant. if coding remove submesh
class exclusively , use:
// vector containing list of vertices. typedef std::vector<vertex> submesh;
your subgrid
class can become simple container holds, 1 of properties, submesh
collection. noticed have class aabb
box object. go on maintain within subgrid
. don't have ton work here, i'm making of these go along, following:
// simple 3-value triplet of floats struct vertex { float x,y,z; }; // submesh arbitrary collection of vertex objects. typedef std::vector<vertex> submesh; // i'm defining aabb 8-vertex object. definition // different, needed compile =) typedef vertex aabb[8]; class subgrid { public: subgrid() {}; // comparator container ordering bool operator <(const subgrid&); // submesh accessors void setsubmesh(const submesh& mesh) { submesh = mesh;} submesh& getsubmesh() { homecoming submesh; } const submesh& getsubmesh() const { homecoming submesh; } // box accessors aabb& getbox() { homecoming box; } const aabb& getbox() const { homecoming box;} private: submesh submesh; aabb box; }; // arbitrary collection of subgrid objects typedef std::vector<subgrid> subgrids;
when adding global subgrid
collection g
, have several possibilities. this:
// declared globally subgrids g; // in function adding subgrid item subgrid subgrid; aabb& box = subgrid.getbox(); subbesh& submesh = subgrid.getsubmesh(); // ... initialize box , submesh info ... g.push_back(subgrid);
but you'd copying lot of info around. tighten memory access instead:
// force empty subgrid first, set in-place g.push_back(subgrid()); subgrid& subgrid = *(g.back()); aabb& box = subgrid.getbox(); submesh& submesh = subgrid.getsubmesh(); //... initialize box , submesh info ...
this found reference subgrid
added global collection, allow modify in-place. but-one of number of possible setup options. should noted if have c++11 in toolchain (and if you're doing on macos or ios, do, apple llvm 4.2's clang pretty on c++11 compliance) can more efficient judicious usage of move-constructors , move-assignment-operators.
most importantly, not new
or delete
seen.
anyway, hope gives ideas.
c++ pointers memory-leaks vector push-back
Comments
Post a Comment