Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I've been trying to figure out what exactly goes wrong in the application.
I basically made a sort of wrapper dll file in c++ for using "LexEngine".
lexengine
lexengine is an mfc application. from the above link, its front end is SmartLexicon.
I keep getting the following error.
Unhandled exception at 0x102e683e (msvcr100d.dll) in teenagedreams.exe:
0xC0000005: Access violation reading location 0x0065006e.


the part in bold changes usually depending on how i'm debugging.
I know i havent initialised something properly or i'm not deleting it properly. but i have no idea what.

i create a lexManagement pointer as a global. i collected the rest of the functions going through the lexview code which used lexEngine.

please help. i've been at this for quite a long while and i am very lost.

the end result i really need is just the meaning of a word. so i load the particular dictionary and get the meaning. the functions require CStrings as inputs and create an object that is to my knowledge derived from CObject....
Posted
Updated 15-Sep-10 0:37am
v2

I notice that msvcr100d.dll is the debug version of an mfc dll, does this mean you are running in debug? If you are go to the Debug menu in developer studio and find the menu item "Exceptions.." check C++ exceptions to break when thrown and then run you application through the debugger.

Hopefully this should give you the source of the exception. The exception itsself is most likely caused by a pointer not pointing at memory that belongs to your program.

A code example of your call and the functions you're calling may help.
 
Share this answer
 
Comments
Dalek Dave 15-Sep-10 10:04am    
Sound Advice.
"the end result i really need is just the meaning of a word. so i load the particular dictionary and get the meaning. the functions require CStrings as inputs and create an object that is to my knowledge derived from CObject...."

You really need to understand the library functions to use them. Your description seems a bit vague. Do you have any documentation?
 
Share this answer
 
i checked the c++ part but the exception still occurs in msvcr100d.dll
here's a breakdown of the code...
globals i created in the dll cpp
//class
    template <class T>
    class less_locale
    {
    public:
        bool operator() (const T& s1, const T& s2)
        {
            return (wcsicoll(s1.c_str(), s2.c_str()) < 0);
        }
    };
//declarations
    //dictionary stuff
    CLexiconObject *lexObject;
    CLexManagement *lexMng = new CLexManagement;
    CLexDataBase *lexDB;
    //parameters and typeders
    static const int PARAM_COUNT = 3;//CLexIndexFileBase::PARAM_COUNT;
    CString paramName[PARAM_COUNT],param[PARAM_COUNT];
    int paramType[PARAM_COUNT];
    typedef multimap<wstring, int, less_locale<wstring> > MultimapKeyWord;
    //word finding stuff
    CString wordToSearch;
    CString xo;
    vector<int> wordAppearancesVector;
    MultimapKeyWord keyWordTable;
    vector<WordEntry> mWordEntryVector;
    CLexSourceFileBase *lexSourceFile;
    CLexIndexFileBase *lexIndexFile;
    vector<CString> langstore;

//globals i created to help returning stuff 
    CString retmessage = _T(" ");
    char * retloaded = new char; //used to return value for function load dictionary
    wchar_t* testing = new wchar_t; // used to return value for function findword


the functions i made... the body of the function is taken from what i observed going on in the source stuff
char * loaddictionary(CString m_FileName,CString pathName, CString m_DictionaryName, CString language1, CString language2, int type, int* wlen)
{
    lexObject = lexMng->CreateNewObject(type,language1);
    lexObject->SetSourceFileName(m_FileName);
    lexObject->SetSourceFilePath(pathName);
    lexObject->SetName(m_DictionaryName);
    for(int i=0; i < PARAM_COUNT; i++)
        lexObject->GetIndexFile()->SetParam(i, param[i]);


    BOOL OK = lexObject->GetSourceFile()->Open(pathName);//m_FileName);
    if (OK)
    {
        lexObject->GetSourceFile()->Analyse();

        int result = lexObject->GetIndexFile()->LoadNew(lexObject->GetSourceFile());
        if (result == CLexIndexFileBase::LOADING_PENDING)
        {
            CString str;
            lexObject->GetIndexFile()->GetProgressMessage(str);

            CString keyw=_T("Reading keywords...");
            CString writin=_T("Writing index file...");

            char *toshow = (char *)(LPCTSTR)(str);
            for(int i=0;i<str.GetLength();i++)
            cout<<*(toshow+(i*2));
            cout<<endl;
            while(str==keyw)
            {
                int progress = lexObject->GetIndexFile()->GetProgress();
                cout<<progress<<"...";
                //for(int i=0;i<100;i++);
                cout<<'\xd';

                lexObject->GetIndexFile()->GetProgressMessage(str);
            }
            cout<<"done"<<endl;

                lexObject->GetIndexFile()->GetProgressMessage(str);
            toshow = (char *)(LPCTSTR)(str);
            for(int i=0;i<str.GetLength();i++)
            cout<<*(toshow+(i*2));
            cout<<endl;
        //  lexObject->GetIndexFile()->IsLoadingOver(result);
        for(int i=0;i<1000;i++)
        {
             int progress = lexObject->GetIndexFile()->GetProgress();
                cout<<progress<<"...";
                //for(int i=0;i<100;i++);
                cout<<'\xd';

                lexObject->GetIndexFile()->GetProgressMessage(str);
         }
        lexObject->GetIndexFile()->IsLoadingOver(result);
        if(result)
        {
            cout<<"done"<<endl;
            str=_T("Loaded");
        }
        else
        {
            cout<<"not done"<<endl;
            str=_T("Not Loaded");
        }
        memcpy(retloaded,str,(str.GetLength())*2);
        *wlen=str.GetLength();


        }
        else if (result != CLexIndexFileBase::LOADING_SUCCESSFUL)
        {
            lexMng->Delete(lexObject);
            lexObject = NULL;

            xo=_T("Loading Unsuccessful");
            *wlen=xo.GetLength();

            memcpy(retloaded,xo,(xo.GetLength())*2);
        }
        else
        {
            xo=_T("Loading Successful");
            memcpy(retloaded,xo,(xo.GetLength())*2);
            *wlen=xo.GetLength();
            }
        storelangind(language1,language2);
    }
    else
    {
        lexMng->Delete(lexObject);
        lexObject = NULL;
        xo=_T("No such File");
        memcpy(retloaded,xo,(xo.GetLength())*2);
        *wlen=xo.GetLength();

    }

    return retloaded;
}

wchar_t* findword(CString aWordToSearch,int* wlen)
	{
		//char *toret=new char;
		lexIndexFile = lexObject->GetIndexFile();
		lexSourceFile = lexObject->GetSourceFile();
		wordToSearch=aWordToSearch;
		BOOL completeWord = 1;
		BOOL exactMatch = 1;
        mWordEntryVector.clear();
        keyWordTable.clear();
        wordAppearancesVector.clear();
		
		int count = 0;
	
		if (wordToSearch.GetLength() > 0)
		{
			count = lexIndexFile->FindWords(wordToSearch,
                                            &wordAppearancesVector,
                                            completeWord,
                                            exactMatch);
			
			
		
			if(count>0)
			{
			int i = 0;
			CLang *lang = lexSourceFile->GetLang();
			CString tempLocale;
			lang->GetLocale(tempLocale);
			_wsetlocale(LC_ALL, tempLocale);
	        vector<int>::iterator it;
			for(it = wordAppearancesVector.begin(); it < wordAppearancesVector.end(); it++)
			{
				int entryLine = *it;
				CString entryWord = lexSourceFile->GetHeadWord(*it, wordToSearch);

				if (entryWord!= _T(""))
				{
					wstring wstr(entryWord.GetBuffer());
				

					keyWordTable.insert(pair<wstring, int>(wstr, entryLine));
				}
			}
			_wsetlocale(LC_ALL, L"C");
			vector<WordEntry>::iterator its;

			MultimapKeyWord::iterator mm_it = keyWordTable.begin();
			lexSourceFile->GetWordEntryAt(mm_it->second,
                                      mWordEntryVector);

			its = mWordEntryVector.begin();
			CString orig = (*its).meaning;

			retmessage = orig;
 *wlen=orig.GetLength();
			
	
			}
			else
			{
				xo=_T("no meaning found");
					
			memcpy(retmessage,xo,(xo.GetLength())*2);
				retmessage = xo;
			*wlen=xo.GetLength();
			}
		}
		else
		{
		xo=_T("no word entered");
	
			retmessage = xo;
		*wlen=xo.GetLength();
		}

		memcpy(testing,retmessage.GetBuffer(),retmessage.GetLength());
		wchar_t * we = retmessage.GetBuffer();
		//memcpy(testing,we,*wlen);
		for(int i=0;i<*wlen;i++)
		{
		*(testing+i) = *(we+i);
		}
		return testing;
	}
 
Share this answer
 
Thankyou so much for your reply and help it worked =D
i made the following changes

XML
const int maxstring =255;
vector<CString> langstore;
char * retloaded= new char[maxstring];
wchar_t* testing= new wchar_t[maxstring];

C#
wchar_t *toshow = str.GetBuffer();
                for(int i=0;i<str.GetLength();i++)
                {
    cout<<(char)*(toshow+(i));}
                cout<<endl


MIDL
*wlen=str.GetLength();
        if (*wlen>maxstring)
                *wlen=maxstring;
        memcpy(retloaded,str,*wlen

but now i have another problem that deals with interop... before these changes the interop part was fine but...
so i'll look into that and if i have issues i'll post another question i guess =D
thankyou so much
 
Share this answer
 
I've added some annotations to the code with >>>>

there are many potentcial access violations in this code. On the information given the line :memcpy(retloaded,str,(str.GetLength())*2); is a certain access violation

the memory allocated to retloaded is much smaller than twice str.GetLength().

You may be interested to know there are functions to convert from wchar_t to char. see this link http://msdn.microsoft.com/en-us/library/ms235631%28VS.80%29.aspx

    template <class T>
    class less_locale
    {
    public:
        bool operator() (const T& s1, const T& s2)
        {
            return (wcsicoll(s1.c_str(), s2.c_str()) < 0);
        }
    };
//declarations
    //dictionary stuff
    CLexiconObject *lexObject;
    CLexManagement *lexMng = new CLexManagement;
    CLexDataBase *lexDB;
    //parameters and typeders
    static const int PARAM_COUNT = 3;//CLexIndexFileBase::PARAM_COUNT;
    CString paramName[PARAM_COUNT],param[PARAM_COUNT];
    int paramType[PARAM_COUNT];
    typedef multimap<wstring, int, less_locale<wstring> > MultimapKeyWord;
    //word finding stuff
    CString wordToSearch;
    CString xo;
    vector<int> wordAppearancesVector;
    MultimapKeyWord keyWordTable;
    vector<WordEntry> mWordEntryVector;
    CLexSourceFileBase *lexSourceFile;
    CLexIndexFileBase *lexIndexFile;
    vector<CString> langstore;

//globals i created to help returning stuff 
    CString retmessage = _T(" ");
    char * retloaded = new char; //used to return value for function load dictionary
    wchar_t* testing = new wchar_t; // used to return value for function findword

    // >>>> Could do with example call of this e.g. passing of wlen and return char*
    char * loaddictionary(CString m_FileName,CString pathName, CString m_DictionaryName, CString language1, CString language2, int type, int* wlen)
    {
        lexObject = lexMng->CreateNewObject(type,language1);
        // >>>> assumning this function copy the cstrings passed in they should be ok
        lexObject->SetSourceFileName(m_FileName);
        lexObject->SetSourceFilePath(pathName);
        lexObject->SetName(m_DictionaryName);
        for(int i=0; i < PARAM_COUNT; i++)
            // >>>> What happens in SetParam, does GetIndexFile return a valid address
            lexObject->GetIndexFile()->SetParam(i, param[i]);
        
        // >>>> Does GetSourceFile() return a valid address
        BOOL OK = lexObject->GetSourceFile()->Open(pathName);//m_FileName);
        if (OK)
        {
            // >>>> Does GetSourceFile() return a valid address
            lexObject->GetSourceFile()->Analyse();

            // >>>> Does GetSourceFile() and GetIndexFile() return a valid address
            int result = lexObject->GetIndexFile()->LoadNew(lexObject->GetSourceFile());
            if (result == CLexIndexFileBase::LOADING_PENDING)
            {
                CString str;
                lexObject->GetIndexFile()->GetProgressMessage(str);
                
                CString keyw=_T("Reading keywords...");
                CString writin=_T("Writing index file...");
                // >>>> **** THIS LOOKS LIKE A BAD IDEA ****
                char *toshow = (char *)(LPCTSTR)(str);
                for(int i=0;i<str.GetLength();i++)
                // >>>> **** This will cause an access violation when i*2 is >= str.GetLength() i.e. halfway through unless LPCTSTR == LPCWSTR
                cout<<*(toshow+(i*2));
                cout<<endl;
                while(str==keyw)
                {
                    int progress = lexObject->GetIndexFile()->GetProgress();
                    cout<<progress<<"...";
                    //for(int i=0;i<100;i++);
                    cout<<'\xd';
        
                    lexObject->GetIndexFile()->GetProgressMessage(str);
                }
                cout<<"done"<<endl;
            
                    lexObject->GetIndexFile()->GetProgressMessage(str);
                // >>>> **** THIS LOOKS LIKE A BAD IDEA ****
                toshow = (char *)(LPCTSTR)(str);
                for(int i=0;i<str.GetLength();i++)
                    // >>>> **** This will cause an access violation when i*2 is >= str.GetLength() i.e. halfway through unless LPCTSTR == LPCWSTR
                cout<<*(toshow+(i*2));
                cout<<endl;
            //    lexObject->GetIndexFile()->IsLoadingOver(result);
            for(int i=0;i<1000;i++)
            {
                 int progress = lexObject->GetIndexFile()->GetProgress();
                    cout<<progress<<"...";
                    //for(int i=0;i<100;i++);
                    cout<<'\xd';
        
                    lexObject->GetIndexFile()->GetProgressMessage(str);
             }
            lexObject->GetIndexFile()->IsLoadingOver(result);
            if(result)
            {
                cout<<"done"<<endl;
                str=_T("Loaded");
            }
            else
            {
                cout<<"not done"<<endl;
                str=_T("Not Loaded");
            }
            // >>> ***** ACCESS VIOLATION HERE if     char * retloaded = new char; holds truen writing many character into on char

            memcpy(retloaded,str,(str.GetLength())*2);
            *wlen=str.GetLength();
                
            
            }
            else if (result != CLexIndexFileBase::LOADING_SUCCESSFUL)
            {
                lexMng->Delete(lexObject);
                lexObject = NULL;

                xo=_T("Loading Unsuccessful");
                *wlen=xo.GetLength();

                memcpy(retloaded,xo,(xo.GetLength())*2);
            }
            else
            {
                xo=_T("Loading Successful");
                memcpy(retloaded,xo,(xo.GetLength())*2);
                *wlen=xo.GetLength();
                }
            storelangind(language1,language2);
        }
        else
        {
            lexMng->Delete(lexObject);
            lexObject = NULL;
            xo=_T("No such File");
            memcpy(retloaded,xo,(xo.GetLength())*2);
            *wlen=xo.GetLength();
       
        }
        
        return retloaded;
    }
 
Share this answer
 
If you call str.GetBuffer() always call str.ReleaseBuffer() when you have finished with the buffer otherwise your CString will become unpredictable.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900