Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It depends. If you're talking about a static library, then you'll probably be OK -- the code will run in the same context as the main program, using the same C++ runtime library. This means that <code>new</code> and <code>delete</code> will use the same heap.</p> <p>If you're talking about a shared library (a DLL), then you probably won't be OK. The code running in the DLL might be using a different C++ runtime library, which means that the layout of the heap will be different. The DLL might be using a different heap altogether.</p> <p>Calling <code>delete</code> (in the main program) on a pointer allocated by the DLL (or vice versa) will lead to (at best) an immediate crash or (at worst) memory corruption that'll take a while to track down.</p> <p>You've got a couple of options. The first is to use the "factory method" pattern to create and delete these objects:</p> <pre><code>Foo *CreateFoo(); void DeleteFoo(Foo *p); </code></pre> <p>These should <em>not</em> be implemented in the header file.</p> <p>Alternatively, you can define a <code>Destroy</code> method on the object:</p> <pre><code>class Foo { ~Foo(); public: virtual void Destroy(); }; </code></pre> <p>...again, don't implement this in the header file. You'd implement it thus:</p> <pre><code>void Foo::Destroy() { delete this; // don't do anything that accesses this object past this point. } </code></pre> <p>Note that the destructor for Foo is private, so that you have to call <code>Foo::Destroy</code>.</p> <p>Microsoft COM does something similar, where it defines a <code>Release</code> method that deletes the object when its reference count drops to zero.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload