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 / .NET Framework / General / January 2005

Tip: Looking for answers? Try searching our database.

Ocx using dao gives error upon exit when used in .NET app

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
pafgbs - 24 Jan 2005 15:55 GMT
I wanted to use some of my MFC stuff in a new C# applikation, so I made an
activeX/ocx of my a database viewer. Tested ocx i VB 6.0 and MFC app, worked
fine. Tested ocx i a C# .NET app, and upon exit I get ".NET
BroadcastEventWindow 1.0.5000.0.2" error.
Some debugging indicates that the error occurs in AfxDaoTerm:
pDaoState->m_pDAODBEngine->Release();

Anyone know the solution to this one ?
Gary Chang[MSFT] - 25 Jan 2005 05:47 GMT
Hi pafgbs,

When you calling an MFC object in the managed code(e.g. a C# program), some
COM references are not released on the exit , so you need to force the
garbage collection explicity right before exiting your managed application:
..
System.GC.Collect();
..

Thanks!

Best regards,

Gary Chang
Microsoft Community Support
--------------------
pafgbs - 25 Jan 2005 11:21 GMT
Thank yuo for yuor ansver, unfortunately I still get the error.

To test this error is easy, in VC++ 6.0 just start the new project wizard,
select "MFC ActiveX Controlwizard" and accept all defaults. Add an OnCreate
handler in the control with code like this:

int CMyCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (COleControl::OnCreate(lpCreateStruct) == -1)
         return -1;
    CDaoDatabase test(NULL);
    try
    {
           test.Open(_T("c:\\test.mdb"));
    }
    catch( CDaoException* e )
    {
            e->Delete();
    }

    return 0;
}

In the app object's ExitInstance() add AfxDaoTerm call to avoid ASSERT in
CWinApp::ExitInstance.

Using this ocx in any windows app made with .NET 2003 will produse an error
upon exit.

If I try to call System.GC.Collect() before exiting Main, like this:

static void Main()
{
    Application.Run(new Form1());
    System.GC.Collect();
}

the error message will after the call to the garbage collector has finished.
Gary Chang[MSFT] - 26 Jan 2005 09:59 GMT
Hi pafgbs,

>...

OK, I can repro this problem on my side. According to the info from the
call stack, it appears the problem happens when your MFC control calling
the AfxDaoTerm().

I think maybe you call the AfxDaoTerm() too later, when it is executed in
your control's ExitInstance(), the corresponding DAO object/reference has
already been collected by the GC. So you need to call the AfxDaoTerm() in
the MFC control's destructor, it would be OK :)

CMyCtrl::~CMyCtrl()
{
    // TODO: Cleanup your control's instance data here.
               AfxDaoTerm();
}

Thanks!

Best regards,

Gary Chang
Microsoft Community Support
--------------------
Get Secure! ??C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.asp
&SD=msdn

This posting is provided "AS IS" with no warranties, and confers no rights.
pafgbs - 27 Jan 2005 08:31 GMT
Ok, this seems to work, thanks :-)
I might get in trouble later when adding several other controls to the ocx,
but som household code might fix it. I still dont get why GC should have any
buisness collecting a reference counted object allocated in my ocx without
its knowledge, but hey, what do I know.

Thanks
Gary Chang[MSFT] - 28 Jan 2005 05:33 GMT
>I might get in trouble later when adding several other controls to the ocx,
>but som household code might fix it. I still dont get why GC should have any
>buisness collecting a reference counted object allocated in my ocx without
>its knowledge, but hey, what do I know.

Next time if you get the similar problem which involved the unmanaged
object, I suggest you can also post that problem to the newsgroup
dotnet.framework.interop.

Good Luck!

Best regards,

Gary Chang
Microsoft Community Support
--------------------
Get Secure! ??C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.asp
&SD=msdn

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



©2008 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.