Boost logo

Boost Users :

From: Ferdinand Prantl (ferdinand.prantl_at_[hidden])
Date: 2003-08-21 03:40:39

Hello Drew,

I implement managed assembly in mc++. I use c++ library with smart pointers
and stlport on the interface to implement a functionality in unmanaged c++,
even if it is exposed to .net through managed classes. So this scenario in
general works.

Holger has already answered you, I see also the problem in different
heaps/runtime versions. You must ensure the same versions of the runtime
(not only debug/release but also version of msvs - 6, 7, 7.1) and of the
templated libraries used (in my case stlport for 6, 7, 7.1, which also must
use the same runtime). I use msvcrt/msvcrtd everywhere and link everything
automatically using pragmas to prevent strange errors from manual
configuration. It can look complex but in reality it is pretty simple.

The second option (as Holger says) is to make a simple facade to the c++
library (all you might use), which will not have templates (here smart
pointers) on the interface, and to use this facade instead.

Good Luck,

P.S. with the first option you will come to problems if you register the
assembly for CCW but that's another story...

-----Original Message-----
From: Holger Grund [mailto:yg-boost-users_at_[hidden]]
Sent: Wednesday, August 20, 2003 18:18
To: boost-users_at_[hidden]
Subject: [Boost-Users] Re: boost::intrusive_ptr and Managed C++?

"Drew" <drewtenn_at_[hidden]>,

> I originally posted this to,
> but they referred me back to here.
> Has anyone used boost's intrusive_ptr with Managed C++ extensions?
Mmh, not really. And it actually doesn't (currently ?) sound like a very
compelling idea to me.

> I have a small project where the executable (managed) is linked to a
> native C++ dll.
> The code looks like:
> int _tmain()
> {
> boost::intrusive_ptr<Unmanaged> pUn = __nogc new Unmanaged();
> return 0;
> }
> when I exit out of main, boost::intrusive_ptr_release() throws an
> exception. The last known method called is:
> UnmanagedLib.dll!_CrtIsValidHeapPointer(...) Line 1807
This probably is not related to the managed extensions. It's more likely
that intrusive_ptr's dtor calls into the DLL to free the pointer which is
only possible if both modules use the DLL version of the CRT.

The message indicates that the pointer to be freed is bogus or more likely
from another heap. This is caused by a mismatch of the CRTs used by the main
program and the DLL. You could use the /MD[d] option for both projects, so
that both modules share a common CRT heap.

I'd prefer to move intrusive_ptr_add_ref/release functions to the program
that uses the instrusive_ptr. Don't export these functions from the DLL.
Rather include these functions at build time.

That way you ensure, that operator delete is called from the same module as
operator new and everything should be fine.

> Unmanaged* pUm = new Unmanaged();
> delete pUm;
Yep. Same module. No problem.


------------------------ Yahoo! Groups Sponsor ---------------------~--> Buy
Ink Cartridges or Refill Kits for Your HP, Epson, Canon or Lexmark Printer
at Free s/h on orders $50 or more to the US & Canada.

Info: <>
Wiki: <>
Unsubscribe: <mailto:boost-users-unsubscribe_at_[hidden]>

Your use of Yahoo! Groups is subject to

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at