Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 631011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:55:05+00:00 2026-05-13T19:55:05+00:00

I have some trouble figuring out why the following crashes (MSVC9): //// the following

  • 0

I have some trouble figuring out why the following crashes (MSVC9):

//// the following compiles to A.dll with release runtime linked dynamically
//A.h
class A {
  __declspec(dllexport) std::string getString();
};
//A.cpp
#include "A.h"
std::string A::getString() {
   return "I am a string.";
}

//// the following compiles to main.exe with debug runtime linked dynamically
#include "A.h"
int main() {
   A a;
   std::string s = a.getString();
   return 0;
} // crash on exit

Obviously (?) this is due to the different memory models for the executable and DLL. Could it be that the string A::getString() returns is being allocated in A.dll and freed in main.exe?

If so, why – and what would be a safe way to pass strings between DLLs (or executables, for that matter)? Without using wrappers like shared_ptr with a custom deleter.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:55:05+00:00Added an answer on May 13, 2026 at 7:55 pm

    This isn’t actually being caused by differing heap implementations – the MSVC std::string implementation doesn’t use dynamically allocated memory for strings that small (it uses the small string optimization). The CRTs do need to match, but that isn’t what bit you this time.

    What’s happening is that you’re invoking undefined behaviour by violating the One Definition Rule.

    The release and debug builds will have different preprocessor flags set, and you’ll find that std::string has a different definition in each case. Ask your compiler what sizeof(std::string) is – MSVC10 tells me that it’s 32 in a debug build and 28 in a release build (this isn’t padding – 28 and 32 are both 4 bytes` boundaries).

    So what’s happening? Variable s is initialized using the debug version of the copy constructor to copy a release version of std::string. The offsets of the member variables are different between the versions, so you copy garbage. The MSVC implementation effectively stores begin and end pointers – you’ve copied garbage into them; because they’re no longer null, the destructor tries to free them and you get an access violation.

    Even if the heap implementations were the same it would crash, as you’re freeing garbage pointers to memory that was never allocated in the first place.


    In summary: the CRT versions need to match but so do the definitions – including the definitions in the standard library.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble figuring this out. I have the following CSV string hello
I'm having some trouble figuring out to call methods that I have in other
I'm having some trouble figuring this out. I have an unordered list menu that
I having some trouble figuring this problem out where I have one button which
I have been having some trouble figuring out how exactly I get a process's
I'm having some trouble figuring out the right way to do this: I have
I have some trouble of figuring out how to use group_concat to produce certain
I have some trouble figuring out the best way to store my programming todo
I'm having some trouble figuring out the appropriate FluentNHibernate mapping syntax for the following
I'm having some trouble figuring out the best/Djangoic way to do this. I'm creating

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.