Like Tree1Likes
  • 1 Post By Salem

new and delete, pointer arrays not working with static variable

This is a discussion on new and delete, pointer arrays not working with static variable within the C++ Programming forums, part of the General Programming Boards category; I am using the following code. Code: void CPathFind::AllocatePathFindInfoMem(void){ if (g_pCarPathInfos){ delete [] g_pCarPathInfos; g_pCarPathInfos = NULL; } if (g_pPedPathInfos){ ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    83

    new and delete, pointer arrays not working with static variable

    I am using the following code.
    Code:
    void CPathFind::AllocatePathFindInfoMem(void){
        if (g_pCarPathInfos){
            delete [] g_pCarPathInfos;
            g_pCarPathInfos = NULL;
        }
        if (g_pPedPathInfos){
            delete [] g_pPedPathInfos;
            g_pPedPathInfos = NULL;
        }
        if (g_pTempDetachedNodes){
            delete [] g_pTempDetachedNodes;
            g_pTempDetachedNodes = NULL;
        }
    
    
        g_pCarPathInfos = new CPathInfoForObject[12288];
    //    memset(g_pCarPathInfos, 0, sizeof(CPathInfoForObject) * 12288);
        g_pPedPathInfos = new CPathInfoForObject[14568];
        memset(g_pCarPathInfos, 0, sizeof(CPathInfoForObject) * 14568);
        g_pTempDetachedNodes = new CTempDetachedNode[4600];
        memset(g_pTempDetachedNodes, 0, sizeof(CTempDetachedNode) * 4600);
        g_nPedGroupNodes = 0;
        g_nCarGroupNodes = 0;
    }
    
    
    void CPathFind::PreparePathData(void){
        //CDebug::DebugAddText("Custom Code: PreparePathData");
        if ( g_pCarPathInfos && g_pPedPathInfos && g_pTempDetachedNodes){
            CTempNode* pTempNodes = new CTempNode[5000];
            m_nAttachedPoints = 0;
            m_nAttachedNodes = 0;
            PreparePathDataForType(PATHDATAFOR_CAR, pTempNodes, NULL, 1.0f, g_pCarPathInfos, g_nCarGroupNodes);
            m_nCarAttachedNodes = m_nAttachedNodes;
            PreparePathDataForType(PATHDATAFOR_PED, pTempNodes, NULL, 1.0f, g_pPedPathInfos, g_nPedGroupNodes);
            m_nPedAttachedNodes = m_nAttachedNodes - m_nCarAttachedNodes;
            if (pTempNodes)
                delete [] pTempNodes;
            CountFloodFillGroups(PATHDATAFOR_CAR);
            CountFloodFillGroups(PATHDATAFOR_PED);
            delete [] g_pCarPathInfos;
            g_pCarPathInfos = NULL;
            delete [] g_pPedPathInfos;
            g_pPedPathInfos = NULL;
            if (g_pTempDetachedNodes)
                delete [] g_pTempDetachedNodes;
            g_pTempDetachedNodes = NULL;
        }
    The 2 functions belong to a class named CPathFind. The variable whose prefixes begin with g_ are the static elements of the class.

    At first the AllocatePathFindMemInfo is called and then at last the PreparePathData is called. And the error is encountered at this line :
    delete [] g_pCarPathInfos;

    MSVC then gives me this error
    Code:
    Windows has triggered a breakpoint in NewPathFind.exe.
    
    
    This may be due to a corruption of the heap, which indicates a bug in NewPathFind.exe or any of the DLLs it has loaded.
    
    
    This may also be due to the user pressing F12 while NewPathFind.exe has focus.
    
    
    The output window may have more diagnostic information.
    Help!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,726
    Why are you not using std::vector or some other container instead of manual memory management? Are you absolutely sure that using static member variables here is the right approach?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Yes, I am pretty sure, since there are large number of dependencies of this specific class and its static variable. What could be the problem,?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,726
    Quote Originally Posted by Swoorup
    Yes, I am pretty sure, since there are large number of dependencies of this specific class and its static variable.
    That sounds like circular reasoning. If you want to justify the use of a static non-const member variable, it could be because it makes sense to have state that is shared by all objects of the class, not because "there are large number of dependencies of this specific class and its static variable". Are the static member variables private?

    Quote Originally Posted by Swoorup
    What could be the problem,?
    Could be a double deletion.

    I would re-look into your class design and change the static member variables into non-static member variables if feasible. Instead of using new[] and delete[] directly, I would use std::vector or some other appropriate container.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    Well the kludge of trying to clear memory needs to be fixed.

    g_pCarPathInfos = new CPathInfoForObject[12288];
    memset(g_pCarPathInfos, 0, sizeof(CPathInfoForObject) * 14568);

    That's a lot of memory you're trashing.

    Now, is CPathInfoForObject just a struct containing ONLY simple data like char and int (NOT pointers, doubles or C++ objects like std::string)?
    If it isn't, then you're doing the wrong thing.

    Actually, scrub that - you're doing the wrong thing PERIOD.
    Just make a constructor for your CPathInfoForObject (yes, even if it's just a C-style struct, it can have a constructor) and be done with it. new will arrange for your ctor to be called when allocating the memory to begin with.

    Next, replace some magic numbers with say
    const int defaultMaxCarPaths = 12288;

    > Yes, I am pretty sure, since there are large number of dependencies of this specific class and its static variable.
    Your classes should be defined entirely by a public function interface. Nobody should care whether you're using a static array or a std::vector (or anything else).
    Data Encapsulation in C++
    antred likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    The static variable is public, and is accessed directly by other functions using CPathFind::g_pPedPathInfos. It seems that the delete operator is automatically invoked somewhere

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,726
    Quote Originally Posted by Swoorup
    The static variable is public, and is accessed directly by other functions using CPathFind::g_pPedPathInfos.
    Therefore, you are using global variables, and that is part of the problem.

    Quote Originally Posted by Swoorup
    It seems that the delete operator is automatically invoked somewhere
    Since you are not using containers or smart pointers, it wouldn't be automatic. More likely, you used delete[] elsewhere when you shouldn't, but because of the use of global variables, you find it hard to determine where.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Nop, I haven't really used anywhere else other than those two function.

    @Salem,
    the CPathInfoForObject just simply contains simple data like char, int, short,etc.
    (Next, replace some magic numbers with say
    const int defaultMaxCarPaths = 12288; )
    Thanks, I will take note of this.

    CPathInfoForObject
    Code:
     
    Code:
    struct CPathInfoForObject{
      float fX; 
      float fY; 
      float fZ; 
      uint8_t byteNodeType;
      signed char sbNextNode;
      unsigned char byteLeftLanes;
      unsigned char byteRightLanes;
      unsigned char byteSpeedLimit;
      signed char sbMedianWidth;
      unsigned char byteFlags;
      unsigned char byteSpawnRate;
    };
    EDIT: Same error happens even when using malloc and free
    Last edited by Swoorup; 05-31-2012 at 11:57 PM.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Sorry about the double post.
    The error was due to limited heap size when actually greater array was used!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ pointer arrays as static class members
    By l1F in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2010, 04:55 AM
  2. Replies: 8
    Last Post: 01-19-2009, 06:42 PM
  3. Managing function-scope pointer variable - delete?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2008, 02:25 AM
  4. Replies: 6
    Last Post: 12-13-2007, 07:20 PM
  5. Static variable length arrays
    By VirtualAce in forum C++ Programming
    Replies: 17
    Last Post: 09-14-2006, 11:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21