15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


Using DAO in an OCX

pjn -- pjn@indigo.ie
Monday, December 16, 1996

Environment: VC++ 4.1, Win95


I am having a problem with using DAO MFC classes in my OCX. I have
read the Microsoft Knowledge Base article Q143084 which says that
there was a problem but that it has been fixed in 4.1. There is also
another article giving guidelines about making sure all DAO objects
are closed before calling AfxDaoTerm. I have isolated the problem in
an easy-to-reproduce project created by the OCX Wizard accepting all
of the defaults. The project was called DAOTest here are the only
modifications I made to the code:

int CDAOTestApp::ExitInstance()
{
	AfxDaoTerm();
	return COleControlModule::ExitInstance();
}

The wizard was used to add a single method which was modified to:

void CDAOTestCtrl::DoIt()=20
{
	CDaoDatabase* pDb =3D NULL;
 =20
	TRY
	{
		pDb =3D new CDaoDatabase;
		pDb->Open("DAOTest.MDB");
		pDb->Close();
	}
	END_TRY

	delete pDb;
}

The program ASSERTs in daocore.cpp line 36 on exiting.


                             '''	  =20
                             @ @
+=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
ooO-(_)-Ooo=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D+
|                                           PJ Naughter              |
|                                                                    |
| Software Developer                   Email: pjn@indigo.ie          |
| Softech Telecom                      Tel:   +353-1-2958384         |
|                                      Fax:   +353-1-2956290         |
| Author of DTime - A Collection       URL:   http://indigo.ie/~pjn  |
| of Date & Time classes for MFC       Mail:  Cahore,                |
|            And                              Ballygarret,           |
| Notpad, the best Notepad clone              Gorey                  |
| for Windows 95 and NT 4                     Co. Wexford            |
|                                             Ireland                |
|                                                                    |
+=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D+



Mast-CBT -- mast@master.csg.it
Thursday, December 19, 1996

At 19.44 16/12/96 GMT, you wrote:
>Environment: VC++ 4.1, Win95
>
>
>I am having a problem with using DAO MFC classes in my OCX. I have
>read the Microsoft Knowledge Base article Q143084 which says that
>there was a problem but that it has been fixed in 4.1. There is also
>another article giving guidelines about making sure all DAO objects
>are closed before calling AfxDaoTerm. I have isolated the problem in
>an easy-to-reproduce project created by the OCX Wizard accepting all
>of the defaults. The project was called DAOTest here are the only
>modifications I made to the code:
>
>int CDAOTestApp::ExitInstance()
>{
>	AfxDaoTerm();
>	return COleControlModule::ExitInstance();
>}
>
>The wizard was used to add a single method which was modified to:
>
>void CDAOTestCtrl::DoIt() 
>{
>	CDaoDatabase* pDb = NULL;
>  
>	TRY
>	{
>		pDb = new CDaoDatabase;
>		pDb->Open("DAOTest.MDB");
>		pDb->Close();
>	}
>	END_TRY

Are you sure here that pDb is != NULL ?

If pDb = NULL when you delete pDb, an error occourse because pDb has invalid
value

>	delete pDb;           <--- 

You can change your code with:

{
        ....
        
        if pDb != NULL then
                delete pDb;
        end if

        ....
}

Hope this help you in some way !

Bye 

Fabio

>}
>
>The program ASSERTs in daocore.cpp line 36 on exiting.
>
>
--------------------------------------------------
Fabio Saponaro
Context Systems Group  -   Italian Branch
Magenta (MI)

Tel. ++ 39 2 97298145
Fax  ++ 39 2 97298225

http://www.web.csg.it
--------------------------------------------------





Mike Blaszczak -- mikeblas@nwlink.com
Thursday, December 19, 1996

At 08:55 12/19/96 +0100, Mast-CBT wrote:

>Are you sure here that pDb is != NULL ?

>If pDb = NULL when you delete pDb, an error occourse because pDb has invalid
>value

>>	delete pDb;           <--- 

>You can change your code with:


That isn't necessary.  The C++ language that

        delete NULL;
and
        delete [] NULL;

are benign.

.B ekiM
http://www.nwlink.com/~mikeblas/
I'm afraid I've become some sort of speed freak.
These words are my own. I do not speak on behalf of Microsoft.




Dan Kirby -- dkirby@accessone.com
Friday, December 20, 1996

[Mini-digest: 2 responses]

 >int CDAOTestApp::ExitInstance()
> >{
> >	AfxDaoTerm();
> >	return COleControlModule::ExitInstance();
> >}

The article you mention Q143084 basically states that you shouldn't be
calling AfxDaoTerm() from the ExitInstance() of the DLL yet this is
precisely what you are doing.  Articles Q149889 and Q152315  also provide
more information which you find helpful.

--dan

----------
> From: Mast-CBT 
> To: mfc-l@netcom.com
> Subject: Re: Using DAO in an OCX
> Date: Wednesday, December 18, 1996 11:55 PM
> 
> At 19.44 16/12/96 GMT, you wrote:
> >Environment: VC++ 4.1, Win95
> >
> >
> >I am having a problem with using DAO MFC classes in my OCX. I have
> >read the Microsoft Knowledge Base article Q143084 which says that
> >there was a problem but that it has been fixed in 4.1. There is also
> >another article giving guidelines about making sure all DAO objects
> >are closed before calling AfxDaoTerm. I have isolated the problem in
> >an easy-to-reproduce project created by the OCX Wizard accepting all
> >of the defaults. The project was called DAOTest here are the only
> >modifications I made to the code:
> >
> >int CDAOTestApp::ExitInstance()
> >{
> >	AfxDaoTerm();
> >	return COleControlModule::ExitInstance();
> >}
> >
> >The wizard was used to add a single method which was modified to:
> >
> >void CDAOTestCtrl::DoIt() 
> >{
> >	CDaoDatabase* pDb = NULL;
> >  
> >	TRY
> >	{
> >		pDb = new CDaoDatabase;
> >		pDb->Open("DAOTest.MDB");
> >		pDb->Close();
> >	}
> >	END_TRY
> 
> Are you sure here that pDb is != NULL ?
> 
> If pDb = NULL when you delete pDb, an error occourse because pDb has
invalid
> value
> 
> >	delete pDb;           <--- 
> 
> You can change your code with:
> 
> {
>         ....
>         
>         if pDb != NULL then
>                 delete pDb;
>         end if
> 
>         ....
> }
> 
> Hope this help you in some way !
> 
> Bye 
> 
> Fabio
> 
> >}
> >
> >The program ASSERTs in daocore.cpp line 36 on exiting.
> >
> >
> --------------------------------------------------
> Fabio Saponaro
> Context Systems Group  -   Italian Branch
> Magenta (MI)
> 
> Tel. ++ 39 2 97298145
> Fax  ++ 39 2 97298225
> 
> http://www.web.csg.it
> --------------------------------------------------
> 
> 
-----From: Mike Blaszczak 

At 19:44 12/16/96 GMT, pjn wrote:
>Environment: VC++ 4.1, Win95
>I am having a problem with using DAO MFC classes in my OCX. I have
>read the Microsoft Knowledge Base article Q143084 which says that
>there was a problem but that it has been fixed in 4.1. There is also
>another article giving guidelines about making sure all DAO objects
>are closed before calling AfxDaoTerm. I have isolated the problem in
>an easy-to-reproduce project created by the OCX Wizard accepting all
>of the defaults. The project was called DAOTest here are the only
>modifications I made to the code:

Your DoIt() function is broken.  You need to protect it with a manage
state macro, like this:

void CDAOTestCtrl::DoIt() 
{
   AFX_MANAGE_STATE(_afxModuleAddrThis);
   CDaoDatabase* pDb = NULL;
 
   TRY
   {
      pDb = new CDaoDatabase;
      pDb->Open("DAOTest.MDB");
      pDb->Close();
   }
   END_TRY
   delete pDb;
}

The program asserts because the work you do with DAO isn't known to MFC as
being related to your module.  When it comes time to clean up, things look
screwy so MFC asserts.

Arguably, some would say that you can't safely use DAO from an OLE control.
DAO only likes to be initialized and shutdown once per process, always
from the same thread.  I'm not sure what would happen if you tried to use
your control in Access, for example, but I'm sure that the use of your
control from multiple threads (even if not concurrently) would be likely
to cause trouble.

.B ekiM
http://www.nwlink.com/~mikeblas/
I'm afraid I've become some sort of speed freak.
These words are my own. I do not speak on behalf of Microsoft.





| Вернуться в корень Архива |