Click to See Complete Forum and Search --> : memory problem -new


Kohinoor24
March 6th, 2003, 08:48 AM
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 BufferAllocator:public 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.....

Yves M
March 6th, 2003, 09:04 AM
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:

template <class T>
void free_object(T *p)
{
// do some bounds checking
// call destructor
p->~T();
// update free list
}

Kohinoor24
March 6th, 2003, 09:16 AM
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..

Bob Davis
March 6th, 2003, 12:20 PM
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.

Yves M
March 6th, 2003, 12:43 PM
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.

Graham
March 6th, 2003, 12:52 PM
I think Bob means that if you still want a default ctor, you must write it yourself.

Bob Davis
March 6th, 2003, 01:30 PM
Graham is right. I'm thinking that elsewhere in his code he does something like


BufferAllocator b;


That would require the additional code.