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

  • SEARCH
  • Home
  • 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 5928415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:17:22+00:00 2026-05-22T14:17:22+00:00

I have a straightforward problem but I don’t understand why I have it. I

  • 0

I have a straightforward problem but I don’t understand why I have it.

I would greatly appreciate any insight.

I wrote this code to test that I was correctly creating and using DLLs in Visual Studio 2010 under Win 7 64bit that could execute on Windows XP. The code executes correctly, and because it is a small test program freeing the allocated memory is not critical, but certainly will be in the future.

I am implicitly calling the DLL, as I say, it appears to work just fine. When I add the line “delete dllMsg;” to toyUseDLL.cpp it crashes, and the debugger shows _unlock_fhandle in osfinfo.c.

If it’s relevant I am compiling the program with /MT to embed the runtime library (for a small handful of not important reasons).

It seems pretty obvious that I’m deallocating something not allocated, but the program output is correct since the pointers are passing the referenced memory locations. The only thing I can think of is that my pointer isn’t valid, and it’s only working by pure chance that the memory wasn’t overwritten.

Thanks for any help, I’m pretty new to C++ and have already found a lot of great help on this site, so thanks for everyone who has posted in the past!! 🙂

msgDLL.h

#include <string>
using namespace std;

namespace toyMsgs {
    class myToyMsgs {
        public:
        static __declspec(dllexport) string* helloMsg(void);
        static __declspec(dllexport) string* goodbyeMsg(void);
    };
}

msgDLL.cpp

#include <iostream>
#include <string>
#include "msgDLL.h"

using namespace std;

namespace toyMsgs {
    string* myToyMsgs::helloMsg(void) {
        string *dllMsg = new string;
        dllMsg->assign("Hello from the DLL");
        cout << "Here in helloMsg, dllMsg is: \"" << *(dllMsg) << "\"" << endl;
        return (dllMsg);
    }

    string* myToyMsgs::goodbyeMsg(void) {
        string *dllMsg = new string;
        dllMsg->assign("Good bye from the DLL");
        cout << "Here in goodbyeMsg, dllMsg is: \"" << *(dllMsg) << "\"" << endl;
        return (dllMsg);
    }
}

toyUseDLL.cpp

#include <iostream>
#include <string>

#include "stdafx.h"
#include "msgDLL.h"

using namespace std;

int _tmain(int argc, _TCHAR* argv[]) {
    string myMsg;
    string *dllMsg;

    myMsg.assign ("This is a hello from the toy program");
    cout << myMsg << endl;

    dllMsg = toyMsgs::myToyMsgs::helloMsg();
    cout << "Saying Hello? " << *(dllMsg) << endl;
    delete dllMsg;

    myMsg.assign ("This is the middle of the toy program");
    cout << myMsg << endl;

    dllMsg = toyMsgs::myToyMsgs::goodbyeMsg();
    cout << "Saying goodbye? " << *(dllMsg) << endl;

    myMsg.assign ("This is a goodbye from the toy program");
    cout << myMsg << endl;

    return 0;
}

Program Output:

This is a hello from the toy program
Here in helloMsg, dllMsg is: "Hello from the DLL"
Saying Hello? Hello from the DLL
This is the middle of the toy program
Here in goodbyeMsg, dllMsg is: "Good bye from the DLL"
Saying goodbye? Good bye from the DLL
This is a goodbye from the toy program
  • 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-22T14:17:22+00:00Added an answer on May 22, 2026 at 2:17 pm

    The problem is that you are using /MT to compile your EXE and DLL. When you use /MT, each executable gets its own copy of the C runtime library, which is a separate and independent context. CRT and Standard C++ Library types can’t safely be passed across the DLL boundary when both DLLs are compiled /MT. In your case the string is allocated by one CRT (on its private OS Heap), and freed by the EXE (which has a different heap) causing the crash in question.

    To make the program work, simply compile /MD.

    General advice: /MT is almost never the right thing to do (for a large handful of relatively important reasons including memory cost, performance, servicing, security and others).

    Martyn

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

Sidebar

Related Questions

I have no idea why this is happening. I have some very straightforward code,
IS this straightforward? Does any one have any good examples? All my google searches
This should be pretty straightforward I would think. I have this string: [quote=Joe Johnson|1]Hi![/quote]
This seems too straightforward as first to be a problem - but here is
I have this naive regex "<([\s]|[^<])+?>" (excluding the quotation marks). It seems so straightforward
Well, I'm gonna be pretty straightforward here, I just have a piece of code
OK, this begins to drive me crazy. I have an asp.net webapp. Pretty straightforward,
I don't know if i should ask this question here, but it's related to
This issue is giving me serious headaches, I don't have a clue what's going
It seems that similar questions have been asked, but they don't quite fit what

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.