Jump to content

Deej

Members
  • Posts

    2
  • Joined

  • Last visited

    Never
  • Donations

    0.00 GBP 

Posts posted by Deej

  1. I've never paid attentation to how the Deleting , and Freeing memory work.

    I bought a very very huge book year ago, but this one didn't help me in this case.

    There weren't any good examples for that. I also tried to look for another one on the internet.

    But without any result. So if anyone of you guyz know about some helpful one or about some tutorial or good example

    how the delete, free, work, please let me know.

    Cheers!

    Pretty much boils down to pairing a delete with every new. Whenever you dynamically allocate memory in your program YOU are responsible for freeing that allocated memory. Otherwise, the memory will always be flagged as in use and if you keeping using new without delete, more and more memory will become unusable.. hence a memory leak. ;)

    example: Two classes, Player and Item. Player class contains an Item pointer for its weapon.
    class Player
    {
    public:
     Player() : weapon(new Item) { }  // Default constructor. Create player and make a new weapon
    
    private:
     Item* weapon;
    };

    So you see here, we have a new Player that has dynamically allocated memory for a weapon. Say we have 500 players online, each object has dynamically allocated memory for their weapons.

    Now a player logs off, the player object is gone. But we have a problem, the memory used for the Item is still 'in use' because we never freed that memory. So player will keep logging in and out and in and out tieing up more memory!

    We need to fix that with a delete in our destructor.

    ~Player() { delete item; } // Frees the memory from weapon when player object is destroyed.

    Problem solved. ;)

    A note for dynamically allocated arrays.. say you have:

    // Dynamic array! User creates size.
    int n;
    std::cout << "Enter an array size: ";
    std::cin >> n;
    int* x = new int[n]; // Dynamically allocated array of size n.

    The syntax for deleting arrays is a little different:

    delete [] x; // Brackets are used to delete dynamically allocated arrays.

    So just remember, everytime you use the 'new' keyword.. there better be a 'delete' for it somewhere! ;)

    Hope this helps.

  2. Two crashes 5 minutes apart. Same crashdump.

    Revision: * * 7517 *
    Date 22:3:2009. Time 13:17 
    //=====================================================
    *** Hardware ***
    Processor: Intel(R) Core(TM)2 Quad CPU    Q6600  @ 2.40GHz
    Number Of Processors: 4
    Physical Memory: 4194303 KB (Available: 4194303 KB)
    Commit Charge Limit: 4194303 KB
    
    *** Operation System ***
    Microsoft Windows Server 2003 Enterprise Edition Service Pack 2 (Version 5.2, Build 3790)
    
    //=====================================================
    Exception code: C0000005 ACCESS_VIOLATION
    Fault address:  00434284 01:00033284 D:\\MangosTest\\mangosd.exe
    
    Registers:
    EAX:00000000
    EBX:036A6880
    ECX:65919A47
    EDX:785BBB78
    ESI:00000000
    EDI:00000000
    CS:EIP:001B:00434284
    SS:ESP:0023:04F5FE98  EBP:04F5FEA8
    DS:0023  ES:0023  FS:003B  GS:0000
    Flags:00010202
    
    Call stack:
    Address   Frame     Function      SourceFile
    00434284  00000000  FreezeDetectorRunnable::run+F4
    008C8505  00000000  ZThread::ThreadImpl::Dispatch+1D5
    008C8963  00000000  ZThread::`anonymous namespace'::Launcher::run+33
    008CCA17  00000000  ZThread::ThreadOps::_dispatch+17
    78543433  00000000  _endthreadex+44
    785434C7  00000000  _endthreadex+D8
    77E64829  00000000  GetModuleHandleA+DF
    
    Call stack:
    Address   Frame     Function      SourceFile
    7C8285EC  00000000  KiFastSystemCallRet+0
    77E61C8D  00000000  WaitForSingleObject+12
    10009C84  00000000  __WSAFDIsSet+FFFFFFFFFFF84BEC
    100764AA  00000000  __WSAFDIsSet+FFFFFFFFFFFF1412
    1007266F  00000000  __WSAFDIsSet+FFFFFFFFFFFED5D7
    0064A10A  00000000  WorldSocketMgr::Wait+4A
    0BD4F79C  00000000  0000:00000000 
    0BD4F79C  00000000  0000:00000000 
    6C696146  00000000  

    So is the last function on the stack where it crashed at? Not too sure on how these dumps are read. :confused:

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy Terms of Use