 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
quikquic@yahoo.com Guest
|
Posted: Sun Sep 28, 2003 11:04 pm Post subject: delete objects created inside a dll |
|
|
I am working on windows and create a dll. In side the dll, there is
something like
class B;
class A{
public:
B* GetB(){
B* pB = ...
return B;
}
};
Now the user use my dll:
A a;
B* pB = a.GetB();
....
delete pB;
There is a problem here. Since pB is allocated inside the dll. It
should be delete inside the dll too. But when the user call delete pB,
he is deleting it outsite the dll. How can I handle this?
Thanks,
qk
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Sun Sep 28, 2003 11:17 pm Post subject: Re: [OT, welcome msg, redir, link] delete objects created in |
|
|
<quikquic (AT) yahoo (DOT) com> wrote
| Quote: | I am working on windows and create a dll. In side the dll, there is
something like
class B;
class A{
public:
B* GetB(){
B* pB = ...
return B;
}
};
Now the user use my dll:
A a;
B* pB = a.GetB();
...
delete pB;
There is a problem here. Since pB is allocated inside the dll. It
should be delete inside the dll too.
|
Are you sure? Or just guessing? I'm not sure, and I
would not guess.
If I needed to know, I'd ask in a Windows programming
group, e.g. comp.os.ms-windows.programmer32, or perhaps
visit www.msdn.microsoft.com where all things Windows-y
are documented and explained.
Here we only discuss the ISO standard C++ language,
which has no such thing as a 'DLL'.
Information about the nature and topic of newsgroup comp.lang.c++:
http://www.slack.net/~shiva/welcome.txt
-Mike
|
|
| Back to top |
|
 |
Rob Williscroft Guest
|
Posted: Mon Sep 29, 2003 1:56 am Post subject: Re: delete objects created inside a dll |
|
|
[email]quikquic (AT) yahoo (DOT) com[/email] wrote in news:ead1f717.0309281504.1bfcb399
@posting.google.com:
| Quote: | I am working on windows and create a dll. In side the dll, there is
something like
class B;
class A{
public:
B* GetB(){
B* pB = ...
return B;
}
};
Now the user use my dll:
A a;
B* pB = a.GetB();
...
delete pB;
There is a problem here. Since pB is allocated inside the dll. It
should be delete inside the dll too. But when the user call delete pB,
he is deleting it outsite the dll. How can I handle this?
|
This is a hack but Standard conforming hack, add operator new and
operator delete to your defenition of B that way all invocations of
new and delete will use whatever code the dll would use for new and
delete.
<example omit="platform-dependant-dll-cruft">
class B
{
//...
public:
void *operator new ( unsigned sz );
void operator delete ( void *p );
};
// in a TU compiled into your dll
void *B::operator new ( unsigned sz )
{
return ::operator new ( sz );
}
void B::operator delete ( void *p )
{
::operator delete ( p );
}
</example>
There are other platform dependant ways off doing what you want, they
may be more effecient, you should ask about those elsewhere eg:
news://comp.os.ms-windows.programmer.win32
HTH
Rob.
--
http://www.victim-prime.dsl.pipex.com/
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|