CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2001
    Posts
    745

    memory problem -new

    Hi All,

    Iam using "placement new" in my application and I have a custom allocator as well.

    The problem is a memory leak which I reduced considerably,but still I have some..I would like to fix that as well.
    Under Windows There is no memory Leak what so ever.


    In the custom allocator which is described below:
    [1]I had to add the commented portion Under Linux to make it compile.Why is it so?

    [2]Iam using Kaldera gcc compiler version 3.1.1.I read in a place that if you are using placement new & if the compiler doesnt support the placement delete ,we will have a memory Leak.

    Does the version 3.1.1 support that. ?

    The custom allocator-

    //static T_BUFFERTYPE rb(1);- uNDER Linux needed

    template<class T, class B>
    class BufferAllocatorublic allocator<T>

    {
    public:

    BufferAllocator(B& buffer):m_buffer(buffer){}
    // BufferAllocator():m_buffer(rb){} *********For Linux
    //Other functions are there which iam not including.

    protected:

    B& m_buffer;

    };


    Thanks.....

  2. #2
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    Re: memory problem -new

    Originally posted by Kohinoor24
    [1]I had to add the commented portion Under Linux to make it compile.Why is it so?
    Is this custom allocator used for STL ? If so, then it doesn't really depend on the compiler, but on the STL implementation you are using. If not, I don't know.

    [2]Iam using Kaldera gcc compiler version 3.1.1.I read in a place that if you are using placement new & if the compiler doesnt support the placement delete ,we will have a memory Leak.
    Honestly, I haven't heard of placement delete, and I can't really see any need for it. You can call a destructor explictly and so that's what you should be doing. For example:
    Code:
    template <class T>
    void free_object(T *p)
    {
      // do some bounds checking
      // call destructor
      p->~T();
      // update free list
    }
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  3. #3
    Join Date
    Oct 2001
    Posts
    745
    Okay,Iam calling the destructors explicitly.so thats fine.
    The custom allocator,Iam not using for STL.Iam using it to allocate some bytes in a Buffer.

    Do you know about why I have to add somethings in the custom allocator class under Linux.
    Any suggestion is welcomed..

  4. #4
    Join Date
    Jan 2001
    Posts
    588
    Code:
    BufferAllocator(B& buffer):m_buffer(buffer){} 
    // BufferAllocator():m_buffer(rb){} *********For Linux
    This worked OK on Windows? Sounds like you don't have an ANSI-compliant compiler. Remember, if you define a non-default constructor for a class, you must define the default constructor, i.e., one with no arguments. If you don't define a constructor at all, then the compiler will generate a default one. If you do define a constructor, then you don't get anything for free. Your Linux compiler is recognizing this and is following the standard.

  5. #5
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Really ? I thought you only had to define a default constructor when you want to be able to contruct objects of that class without specifying parameters to the contructor.

    Why exactly you have to provide a default constructor here lies hidden away in code you haven't shown.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    I think Bob means that if you still want a default ctor, you must write it yourself.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  7. #7
    Join Date
    Jan 2001
    Posts
    588
    Graham is right. I'm thinking that elsewhere in his code he does something like

    Code:
    BufferAllocator b;
    That would require the additional code.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured