Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / April 2004

Tip: Looking for answers? Try searching our database.

Bug including a static lib in a MC++ application

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Arnaud Debaene - 29 Apr 2004 07:25 GMT
Hello,

I think I found a bug in VC 7.1 concerning destruction of stack
objects when linking a static, non managed, C++ library within a
managed C++ application. Here is a repro case :

1) Build a static, unmanaged, library containing the following 2
files. Compiles with /MDd.
// FILE BaseHolder.h
#pragma once
#include <boost/shared_ptr.hpp>

class Base
{};

class Derived : public Base
{};

class Holder
{
public:
  Holder(boost::shared_ptr<Base> pointer);
private:
  boost::shared_ptr<Base> my_pointer;
};

//FILE BaseHolder.cpp
#include "StdAfx.h"
#include ".\baseholder.h"

Holder::Holder(boost::shared_ptr<Base> pointer)
> my_pointer(pointer)
{
}

2) Build a MC++ console application with the following main file (also
compiles with /MDd) :
#include "stdafx.h"
#using <mscorlib.dll>
#include <boost/shared_ptr.hpp>
#include "../static_lib/BaseHolder.h"

void Function()
{
  boost::shared_ptr<Derived> pointer (new Derived);  //line 1
  Holder my_holder (pointer);                        //line 2
}

int _tmain()
{
  Function();
  return 0;
}

3) Now, put breakpoints on boost::detail::sp_counted_base::add_ref()
and boost::detail::sp_counted_base::release() to see the shared_ptr
counter goes up and down : these are lines 119 and 129 of
shared_count.hpp in my version of Boost()

Run the program and observes the behaviour :
1) On line 2, add_ref() is called when copying a copy of pointer for
the parameter of Holder constructor.
2) BUG!!! release() is called from somewhere deep inside mscorwks.dll.
There is no shared_ptr to be destroyed at this point (the Holder
contructor has not yet been called).
3) The Holder constructor is called
4) add_ref is called during construction of Holder::my_pointer (counst
is now 2, should be 3).
5) Normal stuff continues (the parameter of Holder constructor is
destroyed, etc etc...). However, we have leaked an abnormal release,
and the program hangs (in
boost::detail::lightweight_mutex::scoped_lock constructor) during
leaving Function, when trying to delete twice the shared_ptr counter.

If you compile the main application as traditionnal C++ (not managed),
everything goes smooth, so I believe the problem lies somewhere within
mscorwks.dll.

If anyone has an idea on a workaround...

Arnaud
MVP - VC
Gary Chang - 29 Apr 2004 09:23 GMT
Hi Arnaud,

Now I am looking for some resource to help you on this problem.
We will reply in the microsoft.public.vc.language group with more
information soon.

Thanks for your understanding!

Best regards,

Gary Chang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.