Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2005
    Posts
    74
    Thanks
    0
    Thanked 1 Time in 1 Post

    Question Can I pass a NEW object into a function by-ref & without a delete? [C++]

    I want to pass an object by reference into a function (of another class), can this be done without causing a memory leak or requiring me to perform a delete?

    Allow me to illustrate what I mean ...
    Code:
    class A
    {
    vector<B*> v;	// vector of pointers to Class B objects
    
    void Add(B &b)	// function to add pointers to vector
      {
      v.push_back(b);
      }
    ~A {... delete everything from the vector ...}
    };
    
    class B
    {
    ... some stuff ...
    };
    
    void main()
    {
    A *a;	// pointer to instance of A
    a = new A();
    a->Add(new B());
    }
    So, the question is for the following line in particular:
    a->Add(new B());
    In this case I create a new Instance of B() which is passed (by reference) to the A.Add() function ... Can I do this or will it cause a memory leak? Also will the vector in A() have a correct pointer to the object created and passed in?

    My goal is to simply pass the instance to Class A without making the vector public and accessing it directly...

    Let me know if that makes any sense...
    Thanks,
    Last edited by Shaitan00; 06-20-2009 at 07:06 AM.

  • #2
    New Coder
    Join Date
    Jan 2005
    Posts
    74
    Thanks
    0
    Thanked 1 Time in 1 Post

    Question

    If I make the following code-changes it compiles and seems to work ... but is there a memory leak due to my "NEW" inside of MAIN?

    Code:
    void Add(B *b)	// function to add pointers to vector

  • #3
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Yes there would be a memory leak. You'd need to delete 'a' before the end of the program. If your class goes through the vector and calls delete on the pointers, then you can call new the way you are doing without leaking memory. Albeit calling new as a function parameter is a little messy in my opinion.
    OracleGuy

  • #4
    New Coder
    Join Date
    Jan 2005
    Posts
    74
    Thanks
    0
    Thanked 1 Time in 1 Post
    I do delete A when I am done already (in main), my worry is more the NEW in the function parameter ... will that cause any issues?

  • #5
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    As oracleguy said, the use of 'new' will cause an issue, memory leaks in the pointers to dynamically-allocated instances of B.

    A container (like vector) of raw pointers cannot safely deallocate its contents when it is destroyed because it doesn't know if some other part of the program contains pointers to the resource that it might still use.

    Two solutions:

    1. use boost::shared_ptr.

    You pass the shared pointer a raw pointer, and it keeps track of how many things in your program are using it ("reference-counting"). For example, adding it pointer to a vector would increase the reference count by one, and the destruction of that vector would then decrease it by one. When the count reaches zero, the shared pointer deletes the raw pointer. This makes memory leaks difficult to achieve except in cases of objects that contain nested references to themselves, in which case see the linked docs.

    The big caveat about this method is that, if it matters, you'll need to think carefully about which parts of the program hold a reference to the pointer to make sure that it goes away at the appropriate time.

    Depending on how you add it to the example code you posted it could trigger deletions either at the vector's destruction or at the end of main. Deleting things that are still allocated at the end of main is a good idea, but it doesn't really help much. At that point the OS is going to step in and reclaim memory whether you still have a pointer to it or not.

    2. Iterate the vector and delete its contents prior to its destruction. In ~A do something like:
    Code:
    for (std::vector<B*>::iterator it = v.begin(); it != v.end(); ++it)
        delete *it;
    Which has the advantage of being unambiguous about exactly which point in the program the memory is going to be deallocated. The disadvantage is that you're assuming the vector is the only reference to that pointer, which may not be the case if someone else is working with your code or if you forget about the implicit contract you're in to sign over the management of that pointer to the class for the remainder of the execution time.

  • #6
    Banned
    Join Date
    Jun 2007
    Location
    Web Designer
    Posts
    321
    Thanks
    0
    Thanked 6 Times in 6 Posts
    You can use a smart pointer to take care of memory leak.


  •  

    Posting Permissions

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