Link to home
Start Free TrialLog in
Avatar of GiovanniSV
GiovanniSV

asked on

Unmanaged Visual c++ Memory Access Violation with C External Library

Hi all,

I'm devoloping an unmanaged visual c++ application (Win32) which uses an external dll written in C.

The application compiles successfully but it immediately throws a Memory Access Violation Error (0xC0000005:) at the first call to the external library.
The application is used to access a Loquendo Text To Speech Engine.

Here there is the code which makes references to the external code:

extern "C"
{
      #include "C:\Program Files (x86)\Loquendo\LTTS7\include\loqtts.h"
}
extern "C"
{
      #include "C:\Program Files (x86)\Loquendo\LTTS7\include\loqtypes.h"
}

......

   ttsHandleType hReader;      // Reader handle
   //Initializes a LoquendoTTS Reader using the implicit session
   ttsNewReader(&hReader, NULL);


Thanks to everybody,
Best Regards,
Danilo Pompei


Avatar of Xper4net
Xper4net
Flag of France image

Can you post these header files please?
Avatar of GiovanniSV
GiovanniSV

ASKER

Of course, here they are:

Thank you,
Danilo Pompei
loqtts.h
loqtypes.h
Are you sure that you can let the second parameter as NULL without creating at least one session with ttsNewSession?
I think that code should be as demonstrated on http://www.voip-info.org/wiki/view/Loquendo+TTS :

ttsHandleType hSession, hReader; /* Reader handle */ 
ttsResultType r; /* Error code returned by TTS APIs */ 

/* Initializes a LoquendoTTS Reader using the implicit session */ 
r = ttsNewSession(&hSession,"/opt/Loquendo/LTTS7/bin/default.session"); 
if (r != tts_OK) { 
fprintf(stderr, "%s\n", ttsGetErrorMessage(r)); 
ttsDeleteSession(NULL); /* deallocates all tts resources */ 
return r; 
} 

r = ttsNewReader(&hReader, hSession); 
if (r != tts_OK) { 
fprintf(stderr, "%s\n", ttsGetErrorMessage(r)); 
ttsDeleteSession(NULL); /* deallocates all tts resources */ 
return r; 
}

Open in new window

Note that you don't have to use extern "C" for inclusion, as these header already embed this preprocessor directive when necessary. Just remove them.
Thank you for your help.

Yes I'm sure it can be called with a null parameter. I have the same piece of code in a C program which calls this method with a null second parameter and it runs smoothly. I also tried in C++ to pass a not null second parameter and it doesn't work either. In C everything seems to run ok but in C++ it doesn't work at all.

I removed the extern C as you suggested but it has no effect.

I would like also to notify that when I build for Release in Visual C++ it fails with a LNK2001 error. The linker doesn't seem to find the library I attached to the process. Everything builds fine when I build in Debug mode.

Thank you again for your help,
Best Regards,
Danilo Pompei
Ok. So we have to check compilation options.
Please attach your project file (only the project, other sources are not necessary).
Here there is the project attached.

Previously I didn't mention the fact that in Debug modes it builds fine but that it doesn't run and results in the Memory Access error I talked about.

In Release mode it doesn't build.

The thing that I did in Debug mode was to add the LoqTTS7.lib to the AdditionalDependencies and the LoquendoDLL directory to the AdditionalLibraryDirectories. I cannot find where to add it in Release mode.

I'm sorry for this confusion I have made but this is my first project in Visual C++ I always worked in Visual C#.

Thank you,
Best Regards,
Danilo Pompei
TEST-TTS-C-WIN32.vcxproj.txt
Ok, so you're using VS2010. I dont have it on my machine, so I'm not sure if it defines _WIN32 or not, as required by your library.
Try to add these lines in one of your source file:
#ifdef _WIN32
#pragma message("_WIN32 is defined")
#else
#pragma message("_WIN32 is not defined")
#endif
Then look at your logs when you compile. If it's not defined, just add it in preprocessor list (Project|Properties|C/C++|Preprocessor definitions : add ";_WIN32" to existing list)
To do the same that you've done in release configuration, just change configuration in the left top combobox of project properties.
It seems _WIN32 is defined.

As for the other subject I changed the properties in my Release configuration and now it builds but it gives the same Memory Access Violation Error.

Thank you,
Best Regards,
Danilo
Ok. As I dont see anything else in your project options which could lead to this issue, the las thing that I could do is take a look at your whole calling code. Please attach TEST_TTS_C_WIN32.cpp.
Here is the code. There is nothing added except for Local Variables identified by my comment and a linke in the main function also identified by a comment of mine.

Thank you,
Danilo
TEST-TTS-C-WIN32.cpp
ASKER CERTIFIED SOLUTION
Avatar of Xper4net
Xper4net
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I've got a LoqTTS7.lib file which is 73KB and a LoqTTS7.dll which is 746 KB.

I think that the lib version is used in my project. I don't know how to add a DLL to the project.

The Add Reference menu in Visual C++ allows me only to add Project References and not external dll.

Thank you,
Danilo
In C++, to link a DLL, you just have to link the .lib provided with the DLL. Some components are delivered as static library (a .lib too), but in this case, this .lb is bigger, that's why I asked you the library size.
In your case, you have correctly linked the good .lib, and the corresponding DLL seems to be reachable at execution time (otherwise you would have a different error message).
So, my only supposition is that your DLL is not compatible with the C++ project generated by VS2010. Maybe a solution exists, but some attempts are necessary.
If you can post both LoqTTS7.lib and LoqTTS7.dll, I could do some quicks tests.
Unfortunately I cannot post the library because of confidentiality reasons.

It would also be useless because the library is licensed and bound to my mac address.

I'm sorry,
Thank you,
Danilo
Ok, so the last idea that I have is the following :

In Project|Properties|C/C++|Runtime Library, choose "Multi threaded Debug /MTd" in debug configuration and "Multi Threaded /Mt" in release configuration.

It's avoid using of C++ DLL runtime Library.

If it doesn't work, let me know, you should try to see by yourself what are your DLL dependencies.