-
June 2nd, 2005, 07:20 AM
#1
Reference Counters, Pointers, Abstract Classes, Memory Leaks
Hi.
The project I'm working at turned larger and larger and I fear I produce some memory leaks there. I read about auto_ptr, smart pointers, reference counting classes but I have some problems to implement it in my code.
I was happy to find a template class which is doing a reference counting for any object (of type given as template parameter).
I found this template class here:
http://ootips.org/yonat/4dev/counted_ptr.h
Now my problem:
Code:
#include "counted_ptr.h"
//------------------------------------------------------------------------------
class IAnything
{
public:
virtual char GetAny()=0;
};
//------------------------------------------------------------------------------
class CAny1 : public IAnything
{
public:
char m_anyMember;
void AMethod(){
m_anyMember='1';
}
virtual char GetAny(){
AMethod();
return m_anyMember;
}
};
//------------------------------------------------------------------------------
class CAny2 : public IAnything
{
public:
char m_anyMember;
void AMethod(){
m_anyMember='2';
}
virtual char GetAny(){
AMethod();
return m_anyMember;
}
};
//------------------------------------------------------------------------------
void AcceptAnything(counted_ptr<IAnything> pAnything){
if (pAnything.get())
char test = pAnything->GetAny();
}
//------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
// simple demo
{
counted_ptr<int> pCounted1(new int);
*pCounted1 = 0x12;
counted_ptr<int> pCounted2(new int);
*pCounted2 = 0x99;
counted_ptr<int> pCounted3;
pCounted3 = pCounted1;
pCounted2 = pCounted3;
}
// demo with classes of interface derived class
{
counted_ptr<CAny1> pAnyPtr1(new CAny1);
AcceptAnything((counted_ptr<class IAnything>)pAnyPtr1); // <--- WHAT TO DO HERE ???
counted_ptr<CAny2> pAnyPtr2(new CAny2);
AcceptAnything((counted_ptr<class IAnything>)pAnyPtr2); // <--- WHAT TO DO HERE ???
}
return 0;
}
The "simple demo" part compiles successful but not the other part.
In my project I have a lot of methods using Interface* as arguments.
I also store objects being derived from that interface into a generic list using CArray.
What can I do to replace the memory leak advantaging mechanism of using directly "new" and "delete" with a more secure mechanism but still being able to use a structure accepting interface* as arguments ?
Last edited by Stoodent; June 2nd, 2005 at 07:42 AM.
-
June 2nd, 2005, 09:51 AM
#2
Re: Reference Counters, Pointers, Abstract Classes, Memory Leaks
Code:
class IAnything
{
public:
virtual char GetAny()=0;
};
Before you will be able to use counted_ptrs on IAnything you will need to give that class a virtual destructor.
Correct to
Code:
class IAnything
{
public:
virtual char GetAny()=0;
virtual ~IAnything() {}
};
and your smart pointers should hopefully clean up properly.
-
June 2nd, 2005, 10:43 AM
#3
Re: Reference Counters, Pointers, Abstract Classes, Memory Leaks
thank you,
but it doesn't help the line
Code:
AcceptAnything((counted_ptr<class IAnything>)pAnyPtr1);
to compile.
The problem is that the method "AcceptAnything()" shall get any object of class being derived from the interface...
Last edited by Stoodent; June 2nd, 2005 at 10:59 AM.
-
June 2nd, 2005, 05:33 PM
#4
Re: Reference Counters, Pointers, Abstract Classes, Memory Leaks
C-style casting like this:
Code:
counted_ptr<CAny1> pAnyPtr1(new CAny1);
AcceptAnything((counted_ptr<class IAnything>)pAnyPtr1); // <--- WHAT TO DO HERE ???
counted_ptr<CAny2> pAnyPtr2(new CAny2);
AcceptAnything((counted_ptr<class IAnything>)pAnyPtr2); // <--- WHAT TO DO HERE ???
is bad. And is plain wrong too.
What compiler are you on? If VC6 there may be problems, but if you have a compiler that allows member templates then you should not have NO_MEMBER_TEMPLATES defined and then it will be able to construct a counted_ptr<IAnything> from a counted_ptr<CAny1> as CAny1 is derived from IAnything.
Otherwise declare your pointers like this:
Code:
counted_ptr< IAnything > pAnyPtr1( new CAny1 );
counted_ptr< IAnything > pAnyPtr2( new CAny2 );
-
June 6th, 2005, 05:14 AM
#5
Re: Reference Counters, Pointers, Abstract Classes, Memory Leaks
Oh thank you...
Why didn't see it myself ?
Thank you for your help.
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
|