[CDRIVER-2347] mongo-cxx-driver and visual studio 2017 and vcpkg Created: 06/Nov/17  Updated: 27/Oct/23  Resolved: 07/Dec/17

Status: Closed
Project: C Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Mark Hodge Assignee: A. Jesse Jiryu Davis
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 10 Professional and Visual Studio 2017 and vcpkg


Attachments: JPEG File codecrash.jpg    

 Description   

I installed the mongo-cxx-driver with the vcpkg successfully and made a new project with Visual Studio 2017 for a Visual C++ console application. I am running with Windows 10 Professional version. I compiled the test sample from the Mongo web site and it all worked perfectly - so great so far.

I then for my project need to change the default compiler settings to use Struct Member Alignment of 1 byte which is the /Zp1 setting on the
compiler options. When I do this the same code generates an error when a document is added to the database using the insert_one(). I believe the
error is thrown in the bson.c file. My question is I do not think the compiler settings for my project should really effect this library. I
need my main project to use the Struct Member Alignment of 1 since it passed Struct over the network wire to other services/programs which rely on
packed data so it would be very hard to change that.



 Comments   
Comment by A. Jesse Jiryu Davis [ 07/Dec/17 ]

Closing for now, please feel free to reopen if you've investigated and determined that there is a reproducible bug in libbson.

Comment by A. Jesse Jiryu Davis [ 01/Dec/17 ]

I don't think CDRIVER-596 is very closely related: that bug is a question about using the compiler's default alignment versus explicitly setting the alignment of each libbson struct type. This bug is a question about setting the compiler's alignment to 1 byte.

I'm sorry to say we don't have time right now to try to reproduce this issue and debug it with you. I have a couple suggestions:

  • Try recompiling libbson and libmongoc with /Zp1 and then link your program to them.
  • In libbson's CMakeLists.txt try replacing "set (BSON_EXTRA_ALIGN 1)" with "set (BSON_EXTRA_ALIGN 0)"? I'm just curious what effect that has.
Comment by Mark Hodge [ 01/Dec/17 ]

I realize I could set the alignment of the structures in my code via the #pragma or as above - the only problem is there is a lot of legacy code that is using the packed /Zp1 flag. I will do that though if no other solution can be found. Was the issue that Derick pointed out related to this since I see that that issue has been resolved, or is there any other work arounds by including the mongo libraries in some other fashion - presently I am using the vcpkg from microsoft to install it for Visual Studio 2017.

Comment by A. Jesse Jiryu Davis [ 20/Nov/17 ]

Hi, if you need to set the alignment of some structs that represent network messages, please align only those structs:

typedef __declspec( align( 1 ) ) _my_struct {
    int field1;
    char field2;
} my_struct;

Do that only for the structs you must set the alignment of, and leave the overall alignment of the code set to the default.

For more info:
https://msdn.microsoft.com/en-us/library/83ythb65.aspx

Comment by Derick Rethans [ 07/Nov/17 ]

Hi marcus, I have moved this over to the CDRIVER project, as it seems that this is where the error is. In the past we have had other issues with alignment: https://jira.mongodb.org/browse/CDRIVER-596, which also introduces a workaround that might work for you: Compiling the C driver with an extra flag. I'll let the C driver team decide on whether this could be a duplicate, or a new issue.

Comment by Mark Hodge [ 06/Nov/17 ]

I have added a screen capture of the exception i hope that helps.

seems to be saying in bson_init_static in bson.c on the memcpy that is in that routine an exception <Error reading characters of string>. As I say does not seem to happen when default alignment is used. only happens it seems with /Zp1 and /Zp2
works ok with /Zp4 and /Zp8 and /Zp16 and the default setting.

Comment by Derick Rethans [ 06/Nov/17 ]

Can you share the full error message please?

Generated at Wed Feb 07 21:14:57 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.