Thread: memory concerns

  1. #1
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    memory concerns

    Hi everyone!
    I´m making a game and using a engine to do it.
    But I have a problem, my game has a lot of pointers(like normal big programs). I need to know if all the pointers are being deallocated. I thought about using auto_ptr, but this could conflict with the engine and I already coded a lot of classes and such.
    So I tried to use C to help me with this. I used a C function(heapcheck), but I had a lot of strange errors. Sorry if I don´t have any code, but it was a long time ago...
    My final question is: How can I know if all my pointers are being deallocated.
    Any help or just comment would be great
    Nothing more to tell about me...
    Happy day =)

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    > How can I know if all my pointers are being deallocated.
    If possible, write a debug variant of new and delete that handle a counter. Increment with every new and decrement with every delete, if the counter is 0 at the end you're good. I've found this to be an easy and reliable way to check for memory leaks.
    p.s. What the alphabet would look like without q and r.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    search for a garbage collector

  4. #4
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    >search for a garbage collector
    Isn't that a bit of overkill? You would have to suffer the detrimental effects of a garbage collector for the life of the program whereas simply making sure there are no leaks is usually sufficient and doesn't effect the finished application.
    p.s. What the alphabet would look like without q and r.

  5. #5
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    ??

    Originally posted by Trauts
    search for a garbage collector
    Where? How? I have never see one...
    Nothing more to tell about me...
    Happy day =)

  6. #6
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    my idea

    Originally posted by Brighteyes
    >search for a garbage collector
    Isn't that a bit of overkill? You would have to suffer the detrimental effects of a garbage collector for the life of the program whereas simply making sure there are no leaks is usually sufficient and doesn't effect the finished application.
    Indeed, there is a bit of processing behind the trash collector. But in my case, using it is better then rebuild all the project. Your ideia is excellent(I thougt it myself), and starting know I´ll use it in every program. But for a project that involves a lot of included files... I don´t know, I can´t mess things can I?
    I hate Java, but it has a garbage collector... and we know how Java is famous....
    Nothing more to tell about me...
    Happy day =)

  7. #7
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    >Where?
    Online somewhere, google it.

    >How?
    With great care. Garbage collectors are not simple and can be very tricky.

    >I have never see one...
    Probably because C++ as a language isn't suited to garbage collection and its so difficult to do it well in a general way. As such, if GC is found to buy the application anything at all (which is rare), a memory pooling and collection system is moulded specifically to that application. Otherwise, the memory pooling is usually still done for efficiency reasons, but the GC is ignored in favor of explicit release in the code.
    p.s. What the alphabet would look like without q and r.

  8. #8
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135
    Is there a way of using the MSVC debugger to detect memory leaks?

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    what i do in cases like this is to overload global new and delete to keep a list of allocations. when the list contains an allocation that isnt deleted by the operator delete then a memory leak is flagged.
    This doesnt allow malloc/free leaks to be caught tho but it is certainly a good starting point. Once you have the code leak free remove the overloaded new and delete as they are for debugging purposes only.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    ???

    Ok! I have never see the overload of the new and delete operator, but I read something about...
    My question is: Do I have to overload the new for every type that I use in my program?Or there is another way?
    And second: I just readed theory, could someone show me an example??

    Thank you all!

    p.s.:I´m already trying to write my own "Smart Pointers" class
    Nothing more to tell about me...
    Happy day =)

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    Re: ???

    Originally posted by gustavosserra
    Ok! I have never see the overload of the new and delete operator, but I read something about...
    My question is: Do I have to overload the new for every type that I use in my program?Or there is another way?
    And second: I just readed theory, could someone show me an example??

    Thank you all!

    p.s.:I´m already trying to write my own "Smart Pointers" class
    yeah. Add this line in front of it:

    template <typename DataType>

    and change the type you have for the overload to DataType.

    Code:
    template <typename DataType>
    function(DataType X)
    {
        whatever to do
    }
    As for the garbage collector, http://www.hpl.hp.com/personal/Hans_Boehm/gc/

  12. #12
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Sorry, but it doesn´t say nothing to me. If I understood well, I must make a template. This means that I´ll have to change the calling for new, right?

    int * pInt = new<int> int(number);
    I´m must be wrong... anyway. Besides this problem, I don´t know how to implement the new function.
    Coud you please give me a "pointer" to some good on-line material? Or show me how it works?
    Thanks for the help!
    Nothing more to tell about me...
    Happy day =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  3. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  4. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  5. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM