|
-
March 6th, 2003, 09:48 AM
#1
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 BufferAllocator ublic 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.....
-
March 6th, 2003, 10:04 AM
#2
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.
-
March 6th, 2003, 10:16 AM
#3
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..
-
March 6th, 2003, 01:20 PM
#4
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.
-
March 6th, 2003, 01:43 PM
#5
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.
-
March 6th, 2003, 01:52 PM
#6
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
-
March 6th, 2003, 02:30 PM
#7
Graham is right. I'm thinking that elsewhere in his code he does something like
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|