2

Please see this question for some context: Linking unreferenced libraries breaks my program

I have the following program:

#include <iostream>

int main( int argc, char* argv[] )
{
    std::cout << "Hello world" << std::endl;

    int *p;
    p = new int(3);
    std::cout << *p << std::endl;
    delete p;

    return 0;
}

It works fine until I link to third-party libraries. When I link to the Abaqus libraries that I'll need in a much larger program, the above program crashes when trying to run delete p;. Using Dependency Walker, I found that the call to operator new is linked to an operator new definition provided by Abaqus. However, the call to operator delete is linked to the standard definition.

I have replaced new/delete with ::new/::delete and get the same result.

Is there a way to resolve the standard new/delete operators? Alternatively, can I force Visual Studio to link to the correct (standard) definition of these operators?

Community
  • 1
  • 1
Derek
  • 1,104
  • 13
  • 35
  • 1
    Does [this](http://stackoverflow.com/a/4163102/241631) fix your problem? It's hard to believe that the library is overriding `::operator new` without doing the same for `::operator delete`. – Praetorian Jun 24 '13 at 21:47
  • @Praetorian Not quite, although it is relevant. Here I am not trying to use the Abaqus ODB API (yet). I believe that Abaqus has defined both `new` and `delete`. In that case this is more of a linking issue. – Derek Jun 24 '13 at 22:04
  • @Derek: Seems to me that Praetorian is right - you can't use `new` or `delete` that is part of the Abaqus library, until you have called the Abaqus init function. Since the standard library is linked after Abaqus, you will get the Abaqus version, rather than the one in the C runtime. Which seems a rather loony way to do things, but hey, I don't work for Abaqus, so I don't know what they were smoking, drinking or thinking when they came up with that solution - maybe it fixes some other problem that is really difficult to fix in some other way. – Mats Petersson Jun 24 '13 at 23:34
  • @MatsPetersson I'm sorry, maybe my question is unclear. I've spent many months working with the Abaqus ODB API, so I am very familiar with the proper initialization of it. Please see my code. I'm *not* trying to use the Abaqus ODB API. I don't want to use the `new` or `delete` operators provided by Abaqus -- those should not be polluting the global namespace. However, I have found that their implementation *is* in the global namespace and I want to get around their implementation. Does this help? – Derek Jun 24 '13 at 23:38
  • @MatsPetersson Perhaps, it is worth mentioning that yes, you and Praetorian are correct. I'm *not* initializing the Abaqus ODB API. If I initialize the Abaqus ODB API properly, my code is fine under Abaqus 6.9 EF1. However, it does not work with Abaqus 6.12. That is the larger problem at hand. I'm receiving the same error message using Abaqus 6.12 and the above code as I'm receiving in my large program (regardless of initialization). It's rather confusing :). – Derek Jun 24 '13 at 23:42
  • Yes, and I'm saying that according to the linked answer, you will get an error message/crash from simply calling `new` when you have linked with Abaqus, whether you are using the services of that library or not, because they replace `new` and `delete` (which is nasty), causing your application to crash if it hasn't called the Abacus init function. – Mats Petersson Jun 24 '13 at 23:44
  • @MatsPetersson All right. I'll reread that answer. However, I've had great success in the past using `new` and `delete` with Abaqus 6.9 EF1. – Derek Jun 24 '13 at 23:45
  • Well, I know nothing about Abaqus - I'm just reading the answer to the other question, and seeing something that looks like the same thing. It may of course be that you are having a different problem. But it's also worth noting that the Abaqus appears to indicate that ANY allocation using `new` will crash - this means a global `std::string s = "A rather long string goes here .....";` in your main code could cause this to happen, because it's using `new` before you hit main. Again, I think this is a very bad design, but I'm sure Abaqus had some reason for this. – Mats Petersson Jun 24 '13 at 23:48
  • Using advice I received here: http://stackoverflow.com/questions/12408787/call-a-function-before-static-member-allocation I can guarantee that the Abaqus ODB API is initialized properly. I get the same error using Abaqus 6.12 regardless of whether or not I initialize the Abaqus ODB API. – Derek Jun 24 '13 at 23:49
  • @MatsPetersson I believe everything boils down to Visual Studio linking the `new` to the Abaqus `new` and the `delete` to the standard implementation. These cannot be used together, but since the Abaqus `new` is in the global namespace, Visual Studio doesn't know any better. I appreciate your comments! – Derek Jun 24 '13 at 23:53
  • Yes, that's what I think too - and that's why I'm saying it's not a great thing to do. Of course, there can only be ONE ::new function, and to allow your application to replace it, the standard library one is linked last, meaning that the one from Abaqus is taking priority. And like I said, if there is ANY global object that calls new inside the constructor, or a global `T* x = new T;` (or array variant thereof), this will cause this to go wrong, since that will happen before the odb initialization. – Mats Petersson Jun 25 '13 at 00:39
  • @Praetorian I used DUMPBIN to see the functions in the .lib that `operator new` is defined in. I found many definitions for `operator new` and nothing for `operator delete`. – Derek Jun 25 '13 at 14:37
  • 1
    @Derek Well, that kinda explains the problem. There should be some other Abaqus supplied lib where the corresponding `operator delete` definition resides. You'll probably need to contact their support if you can't find anything about this in the documentation. – Praetorian Jun 25 '13 at 15:00

1 Answers1

0

Specifying the linker option "/DEFAULTLIB:MSVCRT.LIB" causes the program to execute successfully.

Derek
  • 1,104
  • 13
  • 35
  • Although, Dependency Walker is still showing that the Abaqus .dll containing their definition of `operator new` is linked. – Derek Jul 01 '13 at 17:05