Crash when linking with Microsoft C Run-Time library

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Crash when linking with Microsoft C Run-Time library

Steve Koester
Hi all,

I am running into a problem with MinGW when I link with the Microsoft C Run-Time library (msvcr100).  I am experiencing a crash when I deallocate memory of a dynamically created stringstream object.  Although the following code snippet is derived from a much larger program, it easily demonstrates the crash.  Interestingly, the code runs fine if 1) I compile without linking to the Microsoft C Run-Time library and use what's provided by MinGW, or 2) I compile using the Microsoft Visual C++ 2010 Express compiler directly, or 3) I comment out the global new/delete operator overloads.

Does anyone have a solution for this?  Just for clarity, I am running 64-bit Windows 7, I installed the lastest version of MinGW, and I do have a need for the larger code set that I am working on to be linked with the Microsoft C Run-Time library and to use overloaded new and delete operators.

example.cpp code:
-----------------
#include <stdlib.h>
#include <sstream>

using namespace std;

void* operator new(size_t length)
{
    return malloc(length);
}

void operator delete(void* address)
{
    free(address);
}

int main(void)
{
    stringstream* ss = new stringstream;
    delete ss;
    return 0;
}

compile line:
-------------
g++ example.cpp -o example -lmsvcr100

Thank you,
Steve    
------------------------------------------------------------------------------
Master HTML5, CSS3, ASP.NET, MVC, AJAX, Knockout.js, Web API and
much more. Get web development skills now with LearnDevNow -
350+ hours of step-by-step video tutorials by Microsoft MVPs and experts.
SALE $99.99 this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122812
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Crash when linking with Microsoft C Run-Time library

George Koehler
There might be a problem with your operator delete.

To your example, I added "#include <stdio.h>" and inserted printf()
statements to check if the program is running the overloaded operators.
The program runs the overloaded new, but never seems to run the
overloaded delete.

Perhaps the program called the wrong operator delete? The code looks
correct to me, so I don't know what the problem is.
--George Koehler


#include <stdio.h>
#include <stdlib.h>
#include <sstream>

using namespace std;

void* operator new(size_t length)
{
     printf("Overloaded new(%ld)\n", (long)length);
     return malloc(length);
}

void operator delete(void* address)
{
     printf("Overloaded delete\n");
     free(address);
}

int main(void)
{
     stringstream* ss = new stringstream;
     delete ss;
     return 0;
}


------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. SALE $99.99 this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122912
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Crash when linking with Microsoft C Run-Time library

Steve Koester
In reply to this post by Steve Koester
> Date: Sat, 05 Jan 2013 04:07:49 +0000
> From: George Koehler <[hidden email]>

> There might be a problem with your operator delete.

> To your example, I added "#include <stdio.h>" and inserted printf()
> statements to check if the program is running the overloaded operators.
> The program runs the overloaded new, but never seems to run the
> overloaded delete.

I tried that earlier today too.  Since nothing printed in the delete
function I initially suspected the function wasn't getting overloaded
properly.  But when I tried the new and delete on a primitive, the printf
worked fine.  From this I concluded that the problem isn't in the
overloading, but is probably in the stringstream destructor.

Anybody have an idea why the stringstream destructor would be misbehaving
like that?

Steve


------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. SALE $99.99 this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122912
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Crash when linking with Microsoft C Run-Time library

Kai Tietz-2
The issue is related to use of DLL version of libstdc++.  As gcc was
build with default Microsoft Runtime Library (msvcrt), but your app is
using msvcr100 as runtime, you have runtime-collision.  So libstdc++
(built with msvcrt) can't free a pointer allocated by your app (built
with msvcr100).  Additionally you have to know that you can't overload
later operators used in libraries built as DLL.  A DLL is a
finally-linked pe-image.
Only way to solve you issue is to use option -static.

Regards,
Kai

------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. SALE $99.99 this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122912
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Crash when linking with Microsoft C Run-Time library

Roumen Petrov
In reply to this post by Steve Koester
Steve Koester wrote:
[SNIP]
> I tried that earlier today too.  Since nothing printed in the delete
> function I initially suspected the function wasn't getting overloaded
> properly.  But when I tried the new and delete on a primitive, the printf
> worked fine.  From this I concluded that the problem isn't in the
> overloading, but is probably in the stringstream destructor.
>
> Anybody have an idea why the stringstream destructor would be misbehaving
> like that?
Look like issue with gcc 4* for mingw target. Version 3.4.5 work fine.

> Steve

Roumen


------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. SALE $99.99 this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122912
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Crash when linking with Microsoft C Run-Time library

Steve Koester
In reply to this post by Steve Koester
> Date: Sat, 5 Jan 2013 11:07:41 +0100
> From: Kai Tietz <[hidden email]>

> Only way to solve you issue is to use option -static.

Thank you.  The static option works fine.  I appreciate your clear and
concise explanation.

Steve


------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnmore_123012
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe