Memory allocation without assignment?

This is a discussion on Memory allocation without assignment? within the C++ Programming forums, part of the General Programming Boards category; First, the relevant source code can be downloaded from here (it was not written by me). And the section of ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    Memory allocation without assignment?

    First, the relevant source code can be downloaded from here (it was not written by me).

    And the section of code that has me scratching my head can be found in line 350 of hmm.cpp:
    Code:
    349:	if (possibleSrc && possibleSrc->find((*it)->state())!=possibleSrc->end())
    350:	  new Transition(*it, node, o);
    The above portion of code is part of a function definition within a class definition. At first I thought it was just code that the author somehow forgot about, so I commented it out. When I did that, the results did not come out right. So it is doing something. I just have no idea what.

    To give it a vague outline, it looks like this.
    Code:
    class A
    {
    public:
    
    A(){}
    }; class B { public:
    B(){}
    some_memfunc(){new A();} // HUH?????
    };
    I've been tracking the thing in gdb for hours now, and there is no light at the end of the tunnel.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,262
    Two posiblities:
    1. The new'd item should be assigned to a pointer so that it can properly be freed. In general it could be something that is only ever allocated once, like a singleton, which means that they may have just been lazy about such barely-consequential leaks.
    2. It may not be a bug at all and may not leak. The constructor of the object might store the pointer to 'this' in some global list, so that the pointer can be freed later.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Quote Originally Posted by iMalc View Post
    2. It may not be a bug at all and may not leak. The constructor of the object might store the pointer to 'this' in some global list, so that the pointer can be freed later.
    Yup, you were (kind of) right. I looked more closely (I guess my brain is fried right now) at the constructor for Transition and it's doing something quite funky:
    Code:
    Transition::Transition(HmmNode* from, HmmNode* to, unsigned long obs)
    {
      _from = from;
      _to = to;
      _obs = obs;
      if (_from && _to) {
        _from->outs().push_back(this);
        _to->ins().push_back(this);
      }
    }
    It's getting pointers to some other data structure and then appending itself to them. Don't know why I didn't notice that before. Thanks.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,262
    My suspicions were correct then
    Actually I have code that does something like that too. The constructor takes the nodes parent, and pushed itself onto the parent's list of objects. Come to think of it, it's extremely similiar to that!

    That doesn't excuse the programmer from not commenting the fact that this technique has been used.
    Last edited by iMalc; 10-13-2007 at 02:31 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Increasing memory allocation in function
    By Ramses800 in forum C Programming
    Replies: 3
    Last Post: 12-16-2008, 04:30 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  4. C memory allocation to c++
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2005, 04:56 AM
  5. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 07:36 AM

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