[SERVER-1042] mongo c++ headers have a #define for malloc, this breaks code compilation with certain boost headers Created: 22/Apr/10 Updated: 12/Jul/16 Resolved: 23/Apr/10 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Internal Client |
| Affects Version/s: | None |
| Fix Version/s: | 1.5.1 |
| Type: | Improvement | Priority: | Major - P3 |
| Reporter: | Roman Shtylman | Assignee: | Mathias Stearn |
| Resolution: | Done | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
Ubuntu Lucid running mongo-stable |
||
| Issue Links: |
|
||||||||
| Participants: | |||||||||
| Description |
|
When building a very simple app which includes mongo headers and other boost headers, the app fails to build. The problem is that mongo headers have a #define for malloc which is forced upon your program silently. This piece of code will fail to compile. The error will indicate that malloc is now mongo::ourmalloc which is most certainly NOT what the user wants. It also causes a conflict with the boost header which has a member function called malloc. #include <mongo/client/dbclient.h> int main(int argc, char* argv[]) error when building: The expected error here is error: too few arguments to function 'void* malloc(size_t)' when the systems (proper) malloc is used |
| Comments |
| Comment by Roman Shtylman [ 24/Apr/10 ] |
|
playing around with the code a bit, I removed the two new headers and just wrapped the various macros in ifdef blocks which are activated by the scons build when building mongo |
| Comment by Mathias Stearn [ 23/Apr/10 ] |
|
Example code now compiles if you add an argument to malloc() call. No extra headers are needed |
| Comment by auto [ 23/Apr/10 ] |
|
Author: {'login': 'RedBeard0531', 'name': 'Mathias Stearn', 'email': 'mathias@10gen.com'}Message: replace pragmas push_macro and pop_macro |
| Comment by auto [ 23/Apr/10 ] |
|
Author: {'login': 'RedBeard0531', 'name': 'Mathias Stearn', 'email': 'mathias@10gen.com'}Message: Automatic cleanup of global macros in client headers. |
| Comment by Mathias Stearn [ 23/Apr/10 ] |
|
The issue is that these headers are also used internally in mongodb where we do want the macros to be defined. I'll see what I can do to alleviate the issue. |
| Comment by Roman Shtylman [ 23/Apr/10 ] |
|
While this does fix the issue in the immediate case (just like doing an #undef would); this solution seems very brittle and viable to be overlooked by the user of the api. They should not have to include a random "cleanup" header who's sole purpose is to undo bad behavior in another header. I think a much cleaner solution would be to not have a public API overwrite system methods without the user explicitly requesting it. Calling one system method only to discover that it got replaced by a third party library method would not appeal to many users of the c++ api. |
| Comment by Mathias Stearn [ 23/Apr/10 ] |
|
If you include mongo/client/cleanup.h as the last mongo header it will remove all of our macros. This file should cherry-pick cleanly to the 1.4 branch so let us know if you need a backport. |
| Comment by auto [ 23/Apr/10 ] |
|
Author: {'login': 'RedBeard0531', 'name': 'Mathias Stearn', 'email': 'mathias@10gen.com'}Message: cleanup.h to undef macros |
| Comment by auto [ 23/Apr/10 ] |
|
Author: {'login': 'RedBeard0531', 'name': 'Mathias Stearn', 'email': 'mathias@10gen.com'}Message: cleanup namespace pollution |