No_Excuses
May 24th, 2002, 07:10 PM
I have created a simple project using the wizard. It is a simple dialog and which has ok and cancel buttons. When I exit everything is ok (when you look into the output window (The program '[1472] test1.exe: Native' has exited with code 0 (0x0).).

Note the code snippet for the "ok" run:

CInternetSession m_session;
CStdioFile *m_pfile;

#if 0
m_pfile = m_session.OpenURL("http://www.codeguru.com");
if (m_pfile != NULL)
m_session.Close();
#endif

I then commented out the #if/endif as follows:

CInternetSession m_session;
CStdioFile *m_pfile;

//#if 0
m_pfile = m_session.OpenURL("http://www.codeguru.com");
if (m_pfile != NULL)
m_session.Close();
//#endif

The following is seen in the output window:

Detected memory leaks!
Dumping objects ->
{83} normal block at 0x002F7BC8, 20 bytes long.
Data: < .| > 10 FF 2E 7C 03 00 00 00 03 00 00 00 01 00 00 00
{82} normal block at 0x002F7B78, 18 bytes long.
Data: < .| > 10 FF 2E 7C 01 00 00 00 01 00 00 00 01 00 00 00
{81} normal block at 0x002F7B18, 33 bytes long.
Data: < .| > 10 FF 2E 7C 10 00 00 00 10 00 00 00 01 00 00 00
{80} normal block at 0x002F2A60, 18 bytes long.
Data: < .| > 10 FF 2E 7C 01 00 00 00 01 00 00 00 01 00 00 00
inet.cpp(560) : {79} client block at 0x002F7A90, subtype c0, 76 bytes long.
a CHttpFile object at $002F7A90, 76 bytes long
Object dump complete.
The program '[1460] test1.exe: Native' has exited with code 0 (0x0).

I have searched quite a bit for others with this same problem and have seen people ask for an answer but they never got one.

Here are my questions:

1) Can something be done to fix the memory leak and if the answer is yes what is it?

2) All I do is read the html from the specified URL (not included in my simple example/test above). If there is a better alternative would you please suggest it and if possible give a simple example or reference as to where I can go to view the solution.

Thanks in advance,

Todd G.

NigelQ
May 24th, 2002, 07:32 PM
This should remove the memory leak...


CInternetSession m_session;
CStdioFile *m_pfile;

//#if 0
m_pfile = m_session.OpenURL("http://www.codeguru.com");
if (m_pfile != NULL)
{
m_pfile->Close();
delete m_pfile;
}
m_session.Close();
//#endif


Hope this helps,

- Nigel

No_Excuses
May 24th, 2002, 07:41 PM
Thanks for the response!!! It does eliminate the memory leak.

I do get the following warning:

Warning: destroying an open CInternetFile with handle 00CC000C

I tried putting the delete before and after the close session and got the same warning either way.

{
m_session.Close();
delete m_pfile;
}

and

{
m_session.Close();
delete m_pfile;
}

Any other comments to eliminate this warning?

Thanks in advance,

Todd G.

NigelQ
May 24th, 2002, 07:43 PM
I edited the snippet of code (not before you saw it, it seems). Try the new version above.

- Nigel

No_Excuses
May 24th, 2002, 07:47 PM
Thanks for you quick reply!! No more leaks or warnings!

Todd G.


** New로 선언되어지고 delete를 다 해주었는데.. memory leak이 발생하여 걱정하였는데
    구글에서 찾아보니..... 해결점을 찾았다. 문제는 CHttpfile로 포인터형으로 인스턴스 형으로 선언되어진것을
     delete 해주니까 해결이 되었다 -.-;;;
Posted by 모과이IT
,

ActiveX를 만들었는데, 레지스트리나 시스템 파일 등...의 기능이 포함되어 있을 경우,

" 이 페이지의 ActiveX 컨트롤이 다른 부분과 상호 작용하는데 안전하지 않을 수 있습니다.

상호 작용을 허용하시겠습니까? "

와 같은 메시지가 뜨게 됩니다.

 

이 경우,

아래와 같이 안정성 처리(IObjectSafety)를 추가해줘야 합니다.

 

1. Ctrl 클래스의 헤더파일(h)에 추가해줍니다.

 

    : 색으로 표시된 부분 과감히 CTRL+C, CTRL+V 하시면 됩니다. ^^;... (MS 문서 참조)

#include <objsafe.h>

class CTestCtrl : public COleControl
{
 //////////////////////////////////////////////////////////////////////////
 // EOCS_2010_0217 : IObjectSafety 처리 추가
 DECLARE_INTERFACE_MAP()
       
  BEGIN_INTERFACE_PART(ObjSafe, IObjectSafety)
        STDMETHOD_(HRESULT, GetInterfaceSafetyOptions) (
        /* [in] */ REFIID riid,
        /* [out] */ DWORD __RPC_FAR *pdwSupportedOptions,
        /* [out] */ DWORD __RPC_FAR *pdwEnabledOptions
        );
   
    STDMETHOD_(HRESULT, SetInterfaceSafetyOptions) (
        /* [in] */ REFIID riid,
        /* [in] */ DWORD dwOptionSetMask,
        /* [in] */ DWORD dwEnabledOptions
        );
    END_INTERFACE_PART(ObjSafe);

// Constructor
public:

.

.

.

2. Ctrl 클래스의 소스파일(cpp)에 추가해줍니다.

 

    : 색으로 표시된 부분 과감히 CTRL+C, CTRL+V 하시면 됩니다. ^^;... (MS 문서 참조)

      . MS 자료나 인터넷이나 다 이렇게 정형화(?) 되어 있습니다.

/////////////////////////////////////////////////////////////
// Interface map for IObjectSafety

BEGIN_INTERFACE_MAP( CTestCtrl, COleControl )
INTERFACE_PART(CTestCtrl, IID_IObjectSafety, ObjSafe)
END_INTERFACE_MAP()

/////////////////////////////////////////////////////////////
// IObjectSafety member functions

ULONG FAR EXPORT CTestCtrl::XObjSafe::AddRef()
{
    METHOD_PROLOGUE(CTestCtrl, ObjSafe)
        return pThis->ExternalAddRef();
}

ULONG FAR EXPORT CTestCtrl::XObjSafe::Release()
{
    METHOD_PROLOGUE(CTestCtrl, ObjSafe)
        return pThis->ExternalRelease();
}

HRESULT FAR EXPORT CTestCtrl::XObjSafe::QueryInterface(
                 REFIID iid, void FAR* FAR* ppvObj)
{
    METHOD_PROLOGUE(CTestCtrl, ObjSafe)
        return (HRESULT)pThis->ExternalQueryInterface(&iid, ppvObj);
}

const DWORD dwSupportedBits =
INTERFACESAFE_FOR_UNTRUSTED_CALLER |
INTERFACESAFE_FOR_UNTRUSTED_DATA;
const DWORD dwNotSupportedBits = ~ dwSupportedBits;

HRESULT STDMETHODCALLTYPE
CTestCtrl::XObjSafe::GetInterfaceSafetyOptions(
               /* [in] */ REFIID riid,
               /* [out] */ DWORD __RPC_FAR *pdwSupportedOptions,
               /* [out] */ DWORD __RPC_FAR *pdwEnabledOptions)
{
    METHOD_PROLOGUE(CTestCtrl, ObjSafe)
       
        HRESULT retval = ResultFromScode(S_OK);
   
    // does interface exist?
    IUnknown FAR* punkInterface;
    retval = pThis->ExternalQueryInterface(&riid,
        (void * *)&punkInterface);
    if (retval != E_NOINTERFACE) {  // interface exists
        punkInterface->Release(); // release it--just checking!
    }
   
    // we support both kinds of safety and have always both set,
    // regardless of interface
    *pdwSupportedOptions = *pdwEnabledOptions = dwSupportedBits;
   
    return retval; // E_NOINTERFACE if QI failed
}

HRESULT STDMETHODCALLTYPE
CTestCtrl::XObjSafe::SetInterfaceSafetyOptions(
               /* [in] */ REFIID riid,
               /* [in] */ DWORD dwOptionSetMask,
               /* [in] */ DWORD dwEnabledOptions)
{
    METHOD_PROLOGUE(CTestCtrl, ObjSafe)
       
        // does interface exist?
        IUnknown FAR* punkInterface;
    pThis->ExternalQueryInterface(&riid,
        (void**)&punkInterface);
    if (punkInterface) {    // interface exists
        punkInterface->Release(); // release it--just checking!
    }
    else { // interface doesn't exist
        return ResultFromScode(E_NOINTERFACE);
    }
   
    // can't set bits we don't support
    if (dwOptionSetMask & dwNotSupportedBits) {
        return ResultFromScode(E_FAIL);
    }
   
    // can't set bits we do support to zero
    dwEnabledOptions &= dwSupportedBits;
    // (we already know there are no extra bits in mask )
    if ((dwOptionSetMask & dwEnabledOptions) !=
        dwOptionSetMask) {
        return ResultFromScode(E_FAIL);
    }                              
   
    // don't need to change anything since we're always safe
    return ResultFromScode(S_OK);
}

 

Posted by 모과이IT
,

■ 파일 열고 닫기 함수 : Open()과 Close()

 CFile file ;

 file.Open(_T("File.txt"), CFile::modeCreate| CFile::modeWrite ) ;

 // ... 파일에 데이터를 읽거나 쓰는 작업 수행

 file.Close() ;

 

。Open 함수의 매개 변수

- 첫번째 arg : 파일명 

- 두번째 arg : 파일 접근 모드와 공유모드 정의. OR(|) 연산자를 이용하여 여러 개 조합.

- 기존에 이 파일이 없으면 새로운 파일이 생성되고 있으면 기존 파일의 맨 뒤에 새로 쓰는 데이터를 덧붙인다.

 

。 파일 접근 모드

 접근 모드  설명
 CFile::modeRead  파일 읽기만 가능
 CFile::modeWrite  파일 쓰기만 가능
 CFile::modeReadWrite  파일 읽고 쓰기 모두가능
 CFile::modeCreate  파일 새로 생성
 CFile:modeNoTruncate  기존에 파일 데이터가 존재 시 맨 뒤에 추가하지 않고 덮어쓴다.

 

。파일 공유 모드

 공유 모드  설명
 CFile::shareDenyNone  공유하도록 파일 열기
 CFile::shareDenyRead  다른 프로그램의 읽기 접근 거부
 CFile::shareDenyWrite  다른 프로그램의 쓰기 접근 거부
 CFile::shareExclusive  다른 프로그램의 읽기, 쓰기 모두 거부

 

 

■ 파일 데이터 쓰기 함수 : Write()

- 우선 파일을 '쓰기 모드'로 연다.

 int buffer[1000] ;

 CFile file ;

 file.Open( _T("File.dat", CFile::modeCreate | CFile::modeWrite ) ;

 file.Write( buffer, 1000 * sizeof(int) ) ;

 file.Close() ;

 

。Write 함수의 매개 변수

- 첫번째 arg : 파일에 쓰고자 하는 데이터가 저장된 메모리 포인터를 넘겨준다.

- 두번째 arg : 데이터의 크기를 바이트 단위로 넘겨준다.

 

 

■ 파일 데이터 읽기 함수 : Read()

- 우선 파일을 '읽기 모드'로 파일을 연다.

 int buffer[1000] ;

 CFile file ;

 file.Open( _T("File.dat"), CFile::modeRead ) ;

 file.Read( buffer, 1000 * sizeof(int) ) ;

 file.Close() ;

 

。Read 함수의 맴개 변수

- 첫번째 arg : 읽은 데이터를 저장할 메모리의 포인터를 넘겨 준다.

- 두번째 arg : 읽고자 하는 데이터의 크기를 바이트 단위로 넘 겨준다.

 

 

■ 파일의 길이를 알아내는 함수 : GetLength()

- 파일의 크기를 알아내고 그 크기만큼 데이터를 읽어 온다.

 CFile file ;

 file.Open( _T("File.dat"), CFile::modeRead ) ;

 int nLength = file.GetLength() ;

 int *buffer = new BYTE [nLength] ;

 file.Read( buffer, nLength ) ;

 file.Close() ;

 

 

■ 에러 처리 : TRY ~ CATCH 블록

- TRY 블록 안의 루틴을 수행하다가 실행 중 에러가 발생할 경우에만 CATCH 블록이 실행된다.

- CFileException 함수의 ReportError 함수를 호출한다.

-  ReportError 함수는 사용자에게 어떤 에러가 발생했는지 알려주는 메세지 박스를 출력해 준다.

 TRY

 {

     int buffer[1000] ;

     CFile file ;

     file.Open( _T("File.dat"), CFile::modeCreate | CFile::modeWrite ) ;

     file.Write( buffer, 1000 * sizeof(int) ) ;

     file.Close() ;

 }

 CATCH(CFileException e )

{

     e->ReportError() ;

 }

 END_CATCH

 

 

■ 파일 포인터

。현재 파일 포인터 위치 얻기 : GetPosition

- [TBD]

 

。파일 포인터 옮기기 : Seek

 file.Seek( 1000, CFile::current ) ;

 file.Read( buffer1, sizeof(int) * 10 ) ;

 file.Seek( -2000, CFile::end ) ;

 flie.Read( buffer2, sizeof(int) * 10 ) ; 

 

- Seek 함수의 매개변수

첫번째 arg : 이동 거리

두번째 arg : 이동 시 파일 포인터 기준 위치

 

- 파일 포인터의 기준 

 매개변수  설명 
 CFile::begin  기준은 파일의 처음 위치
 CFile::current  기준은 현재 파일 포인터의 위치
 CFile::end  기준은 파일의 끝 위치

 

 

■ 그 밖에 파일 처리 (기타 멤버 함수)

GetFilePath : 파일의 전체 경로명 반환

GetFileName : 파일 이름 반환

GetFileTitle : 확장자를 제외한 파일 이름 반환

GetStatus : 파일이 생성된 시간, 최종 변경된 시간, 크기, 속성 등 파일의 상태 얻기

SetStatus : 파일의 상태를 지정

Remove : 파일 지우기

Rename : 파일 이름 변경

Posted by 모과이IT
,