Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19
Memory leak?
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Memory leak?

Hybrid View

  1. #1
    Senior Member
    Join Date
    Jul 2001
    Posts
    280

    Memory leak?

    Is this a memory leak? If so, what would be the best way to work around it?

    Code:
    #include "myclass.h"
    
    void func (MyClass *mc){
       // do something with the class
    }
    
    MyClass *getAClass (){
       
       MyClass *c = new MyClass ();
       // Do some stuff here
       return c;
    }
    
    int main (){
    
       func (getAClass());
       return 0;
    }
    What happens to that class instantiated in the [tt]getAClass[/tt] function? Once [tt]func[/tt] returns there are no more pointers to this class. Will it be automatically deleted because its an anonymous variable (I think thatís the correct term, anyway)? Or is it just hanging out in the heap waiting for the program to end? Thanks for your help in clearing up this matter.

  2. #2

    Re:Memory leak?

    [quote author=t048 link=board=9;threadid=5118;start=0#50705 date=1032993608]
    Once [tt]func[/tt] returns there are no more pointers to this class. Will it be automatically deleted because its an anonymous variable (I think thatís the correct term, anyway)? Or is it just hanging out in the heap waiting for the program to end? Thanks for your help in clearing up this matter.
    [/quote]

    There is no garbage collection in C++. you should do:

    Code:
    int main () { 
      MyClass *c;
      c = getAClass();
      func (c);
      delete(c);
      return 0;
    }
    this way after the class is done being used you delete it.

  3. #3

    Re:Memory leak?

    [qualifier]
    i know damn near nothing about icky languages like C++
    [/qualifier]

    i thought ya freed memory in C++ with a negation? e.g.

    foo(blah)::~foo(blah) ...


    please eduacte me gorn!

  4. #4

    Re:Memory leak?

    To pbharris:

    I don't know too much C++, but AFAIK you dynamically allocate memory in C++ with the "new" keyword, and you deallocate the memory with the "delete" keyword. The ~ before a name is a class destrutor, where you would put the delete statements for any dynamically allocated memory inside of that class. Example:

    Code:
    class foo {
      float* mvFloat;
    public:
      foo() //could be written outside of the class as foo::foo()
      {
        mvFloat = new float[1000];
      }
      ~foo() //could be written outside of the class as foo::~foo()
      {
        delete [] mvFloat;
      }
    };
    (The "delete []" is for arrays)

  5. #5

    Re:Memory leak?

    thanks GotAnyGrapes!

  6. #6
    Moderator
    Good Guru
    Schotty's Avatar
    Join Date
    Jul 2001
    Location
    Milwaukee, WI
    Posts
    5,760

    Re:Memory leak?

    [quote author=pbharris link=board=9;threadid=5118;start=0#50721 date=1033010019]
    [qualifier]
    i know damn near nothing about icky languages like C++
    [/qualifier]
    [/quote]

    Icky my ass! C++ is sacred and holy.

  7. #7

    Re:Memory leak?

    you sir are refering to C and assembly, not C++ - mother of all bloat!

  8. #8
    Senior Member
    Join Date
    Jul 2001
    Posts
    280

    Re:Memory leak?

    Thanks for the replies. Gorn, that's what I thought one needed to do, but there is a problem with that. I'll post the example I was looking at that got me wondering when I get home later today.

    [quote author=pbharris link=board=9;threadid=5118;start=0#50796 date=1033132201]
    you sir are refering to C and assembly, not C++ - mother of all bloat!
    [/quote]
    It's not bloat; it's power!

  9. #9
    Moderator
    Good Guru
    Schotty's Avatar
    Join Date
    Jul 2001
    Location
    Milwaukee, WI
    Posts
    5,760

    Re:Memory leak?

    [me=Schotty]tips his fedora to t048[/me]

    Precisely, power. Its going from a go-kart to a Winston Cup NASCAR engine!
    (in the Tim "the toolman" Taylor voice) HOHOHOHOHO

  10. #10

    Re:Memory leak?

    Nah. It's more like going from a race car to a New Yorker. You don't have the speed you did before, but it's considerably more comfortable and easy to drive.

Similar Threads

  1. Squid memory leak problems
    By comet in forum Linux - Hardware, Networking & Security
    Replies: 0
    Last Post: 01-20-2012, 04:26 PM
  2. Memory????
    By comtux in forum Linux - General Topics
    Replies: 10
    Last Post: 09-25-2004, 12:41 AM
  3. Web leak of Linux lets Hat out of the bag
    By trickster in forum Linux - General Topics
    Replies: 1
    Last Post: 09-25-2002, 11:50 PM
  4. Is this a memory leak?
    By t048 in forum Linux - Software, Applications & Programming
    Replies: 3
    Last Post: 04-27-2002, 06:34 AM
  5. Another "Valentine Memo"  leak...
    By pbharris in forum General Chat
    Replies: 12
    Last Post: 01-05-2002, 07:40 PM

Bookmarks

Posting Permissions

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