CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Hybrid View

  1. #1
    Join Date
    Dec 2010
    Posts
    907

    Why should this code snippet crash?

    I've got memory leak on the delete line when being commented out
    On the other hand, I've got a crash. Where is the bug?
    The reason I use a float* because some function requires me this type of parameter to pass in.
    I can't convert a float[][] to a float*
    Thanks
    Jack

    Code:
    struct NavPath
    {
    	NavPath() {
    		positions = new float[MAX_PATH_NODES*3];
    		count = 0;
    	}
    
    	~NavPath() {
    		if (positions) {
    			delete[] positions;
    			positions = 0;
    		}
    
    	}	 
        float *positions;
        std::size_t count;
    };
    Last edited by lucky6969b; January 10th, 2015 at 02:34 AM.

  2. #2
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Why should this code snippet crash?

    Quote Originally Posted by lucky6969b View Post
    Where is the bug?
    not there, your bug is somewhere else.
    BTW,

    Quote Originally Posted by lucky6969b View Post
    ~NavPath() {
    if (positions) {
    delete[] positions;
    positions = 0;
    }
    avoid this kind of defensive style, it's useless at best ( you can safely delete a null pointer ), and it may hide existing bugs ... ( what if the pointer is erroneously set to null somewhere before the destructor gets called ? you won't know ... )

  3. #3
    Join Date
    Dec 2010
    Posts
    907

    Re: Why should this code snippet crash?

    It simply crashes here after the return statement
    Code:
    std::vector<D3DXVECTOR3> ConvertNavPathToVector3(NavPath* navPath) {
    	std::vector<D3DXVECTOR3> vector3;
    
    	for (int i = 0; i < navPath->count; i++) {
    		D3DXVECTOR3 vec3;
    		vec3.x = *navPath->positions++;
    		vec3.y = *navPath->positions++;
    		vec3.z = *navPath->positions++;
    		vector3.push_back(vec3);
    	}
    
    	return vector3;
    }
    
    bool NavMesher::findPath(const D3DXVECTOR3& from, const D3DXVECTOR3& to, std::vector<D3DXVECTOR3>& resultPath)
    {
            if(nullptr == m_navQuery)
            {
                    return false;
            }
            if(nullptr == m_navMesh)
            {
                    return  false;
            }
         
            dtQueryFilter queryFilter;
            dtPolyRef startRef;
            dtPolyRef endRef;
         
            dtPolyRef returnedPath[MAX_PATH_NODES];
            float extents[3];
            extents[0] = 2.f;
            extents[1] = 4.f;
            extents[2] = 2.f;
         
         
            m_navQuery->findNearestPoly(from, extents, &queryFilter, &startRef, 0);
            if (startRef == 0)
            {
                    return false;
         
            }
            m_navQuery->findNearestPoly(to, extents, &queryFilter, &endRef, 0);
            if (endRef == 0)
            {
                    return false;
         
            }
            dtStatus findStatus = DT_FAILURE;
            int pathCount;
         
            findStatus = m_navQuery->findPath(startRef, endRef, from, to, &queryFilter, returnedPath, &pathCount, MAX_PATH_NODES);
            if (dtStatusFailed(findStatus))
            {
                    return false;
            }
         
            if(pathCount > 0)
            {
                    int numNodes = 0;
    		NavPath path;
    		// find an array of vectors within a path corridor
    		findStatus = m_navQuery->findStraightPath(from, to, returnedPath, pathCount, path.positions, 0, 0, &numNodes, MAX_PATH_NODES);
    		if(dtStatusFailed(findStatus))
                    {
                            return false;
                    }
    		path.count = numNodes;
    		resultPath = ConvertNavPathToVector3(&path);   
    				
    		return true;
    		 
            }
            return true;
    }
    The path.positions parameter is basically passed in as a pointer of floats
    Thanks
    Jack
    Last edited by lucky6969b; January 10th, 2015 at 04:27 AM.

  4. #4
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Why should this code snippet crash?

    you're incrementing the positions member that in turn is delete'd later, causing UB and hence the crash. This would not have happened if the NavPath class and the ConvertNavPathToVector3 function were const-correct ! at worst, it should be something like:

    Code:
    std::vector<D3DXVECTOR3> ConvertNavPathToVector3( NavPath const& navPath ) {
    	std::vector<D3DXVECTOR3> vector3;
    	float const* positions = navPath.positions;
    
    	for (int i = 0; i < navPath->count; i++) {
    		D3DXVECTOR3 vec3;
    		vec3.x = *positions++;
    		vec3.y = *positions++;
    		vec3.z = *positions++;
    		vector3.push_back(vec3);
    	}
    
    	return vector3;
    }
    Last edited by superbonzo; January 10th, 2015 at 05:11 AM.

  5. #5
    Join Date
    Dec 2010
    Posts
    907

    Re: Why should this code snippet crash?

    Thanks, it's working okay now.
    Jack

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured