[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:
Related
related to SERVER-5348 Redefinition of macros while using th... Closed
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>
#include <boost/pool/pool.hpp>

int main(int argc, char* argv[])
{
malloc();
return 0;
}

error when building:
In file included from /usr/include/boost/pool/pool.hpp:34,
from shit.c:2:
/usr/include/boost/pool/simple_segregated_storage.hpp:99: error: invalid use of '::'
In file included from shit.c:2:
/usr/include/boost/pool/pool.hpp:54: error: invalid use of '::'
/usr/include/boost/pool/pool.hpp:65: error: invalid use of '::'
/usr/include/boost/pool/pool.hpp:219: error: invalid use of '::'
/usr/include/mongo/client/../util/allocator.h: In function 'int main(int, char**)':
/usr/include/mongo/client/../util/allocator.h:22: error: too few arguments to function 'void* mongo::ourmalloc(size_t)'
shit.c:6: error: at this point in file

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

http://github.com/shtylman/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 SERVER-1042
http://github.com/mongodb/mongo/commit/0638bbfee235b70df345bf251b36228ec0416763

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. SERVER-1042
http://github.com/mongodb/mongo/commit/2f197d519217eee23d04ecff2d0050cda842e89b

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 SERVER-1042
http://github.com/mongodb/mongo/commit/60002a474169de84eac6bce20836ee553abf731c

Comment by auto [ 23/Apr/10 ]

Author:

{'login': 'RedBeard0531', 'name': 'Mathias Stearn', 'email': 'mathias@10gen.com'}

Message: cleanup namespace pollution SERVER-1042
http://github.com/mongodb/mongo/commit/1d746cc4ca39790b89c8904c63ab42b41ed3d83b

Generated at Thu Feb 08 02:55:53 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.