0

My application is crashing when i am trying to call SetName() in the below code.

main.cpp

using namespace std;
int main()
{ 
    Schema * schemaExp = new Schema();

    //Application is getting crash when calling this function 
    schemaExp -> SetName("ExpSchema");

    string srctable;
    srctable=schemaExp->GetName();
    cout <<"\nConnection EXPORT using the target table:" << srctable.c_str()  << endl;
    delete schemaExp;
    return 0;
}

Schema Class Definition:

using namespace std;
class Schema
{
   public:
   TELAPI_EXPORT void   SetName(string name); 
   TELAPI_EXPORT string     GetName(); 
   protected: 
   string tableName; 
};

void Schema::SetName(string name){ tableName = name; }

string Schema::GetName()
{
  return tableName;
}

I am using MSVS 9 (VS 2008). My application as well as shared library(dll)(I am using to link with my application) is also of c++ environment.Now observe the below cases:

1.when the shared library/dll is built in Debug mode and my application is also built in Debug mode Result: Application executed successfully

2.when the shared library/dll is built in Release mode and my application is also built in Release mode Result: Application executed successfully

3.when the shared library/dll is built in Release mode and my application is built in Debug mode Result:Application throws a crash report with following break statement.

Unhandled exception at 0x1003f3a5 in multiple.exe: 0xC0000005: Access violation reading location 0x00134000.

Note: The above code is just a part from my application. Schema Class Definition is from shared library and main.cpp from my application. Also, this problem seems to fail on Windows only, unix versions worked fine.

One more important thing if I comment out schemaExp -> SetName("ExpSchema"); in main.cpp , the application passes in the above three cases, I mean any combination of release and debug build

In the entire code of my original application(of which above code is a part), only the above function call is troubling me

I guess something going wrong in using string as a parameter to function call, but also note when I wrote the sample program(not linking to shared library/dll) implementing the above scenario my application runs fine

Completely struck over here. Unable to predict what's going wrong and what's causing access violation that too only in the #3 case.

Please help me in fixing this problem. Any kind of help is greatly appreciated.

Thanks in advance.

ybc
  • 67
  • 1
  • 3
  • 10
  • `void main` isn't legal. Use `int main`. – chris Jul 17 '13 at 07:25
  • 1
    when the shared library/dll is built in Release mode and the application is built in Debug mode, it's not expected to work – spiritwolfform Jul 17 '13 at 07:29
  • 1
    @chris : Please tell me why `void main` is illegal, Thanks – frogatto Jul 17 '13 at 07:30
  • 2
    @ABFORCE, Because the standard forbids it. *An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int...* – chris Jul 17 '13 at 07:49
  • We already went through that in [your earlier question](http://stackoverflow.com/questions/17564869/release-mode-vs-debug-mode-in-ms-visual-studio-windows). Why are you repeating it? – Hans Passant Jul 17 '13 at 14:19
  • I am sorry, you are no way wrong in asking me this question, but please observe the post clearly...both are errors are entirely different...what I specified in this post is my exact error and which hits when I am calling `schemaExp -> SetName("ExpSchema");`. To fix this I changed the function definition from here to there. There the problem is with destructor but here with the function call itself... – ybc Jul 18 '13 at 04:05
  • I want to know why I am getting these issues when I am using only the SetName() call and working fine in every other case...apart from release-debug combination(though may be a cause) of libraries, I think there is something wrong with the function call and its definition itself(please confirm)....Please tell me if you need any more details...Thanks for understanding! – ybc Jul 18 '13 at 04:06

1 Answers1

0

This is what I would expect. Consider:

  • The debug build and the release build use different run-time libraries in the background. So allocating and releasing memory for example will be done differently depending on the build you have selected.

  • If you do not declare constructors or destructors to be imported or exported then they will be auto-generated and use the run-time with which the project is build that includes the shared lib.

  • Depending on the build type certain macros might be set or not. For example the NDEBUG macro is set in release mode usually. This affects the not only the functions (and assertions) but sometimes even the size of data structures. If you have a checked STL implementation for example, then the iterators of a container may store a pointer to their container for internal validity checks.

As you can see, mixing Debug and Release is not a good thing. Just avoid it, if possible.

Ralph Tandetzky
  • 22,780
  • 11
  • 73
  • 120