Solved

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

Posted on 2010-11-08
16
925 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now