Solved

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

Posted on 2010-11-08
16
935 Views
Last Modified: 2012-05-10
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


0
Comment
Question by:GiovanniSV
  • 9
  • 7
16 Comments
 
LVL 5

Expert Comment

by:Xper4net
ID: 34083818
Can you post these header files please?
0
 

Author Comment

by:GiovanniSV
ID: 34084071
Of course, here they are:

Thank you,
Danilo Pompei
loqtts.h
loqtypes.h
0
 
LVL 5

Expert Comment

by:Xper4net
ID: 34084223
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

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:Xper4net
ID: 34084265
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.
0
 

Author Comment

by:GiovanniSV
ID: 34084467
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
0
 
LVL 5

Expert Comment

by:Xper4net
ID: 34084547
Ok. So we have to check compilation options.
Please attach your project file (only the project, other sources are not necessary).
0
 

Author Comment

by:GiovanniSV
ID: 34084715
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
0
 
LVL 5

Expert Comment

by:Xper4net
ID: 34084964
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.
0
 

Author Comment

by:GiovanniSV
ID: 34085371
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
0
 
LVL 5

Expert Comment

by:Xper4net
ID: 34085658
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.
0
 

Author Comment

by:GiovanniSV
ID: 34085873
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
0
 
LVL 5

Accepted Solution

by:
Xper4net earned 500 total points
ID: 34086146
There's nothing wrong in your code. So I suppose there's an incompatibility between the C-Runtime used by your library and the one used by VS2010. Or maybe you link the wrong library.
Are you sure that LoqTTS7.lib is the link DLL library version or the static one (what's its size)?
It seems indeed that this library exists in a static library version. If you're not wrong with the DLL lib, then you can try it if you have. As i see in library's header, you just have to add ";INCLUDE_STATIC_LIB" in your preprocessor definitions, and replace the link library name with the static version name.
0
 

Author Comment

by:GiovanniSV
ID: 34091423
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
0
 
LVL 5

Expert Comment

by:Xper4net
ID: 34091715
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.
0
 

Author Comment

by:GiovanniSV
ID: 34093658
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
0
 
LVL 5

Expert Comment

by:Xper4net
ID: 34094515
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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Creating a route in asp.net webforms 2 32
Help with exporting to excel 4 35
get combo value in class 5 14
Looping through datagridview and dataset ? 6 24
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question