CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2004
    Posts
    198

    std::string going out of scope

    i'm sure the whole stl being used in a dll thing has been discussed a lot already, but i always seem to have 1 specific problem, as demonstrated by this code:
    Code:
    #include "stdafx.h"
    #include "MyLibrary.h"
    
    int main(int argc, char* argv[])
    {
    	Node node;
    	node.x = 900;
    	node.y = 955;
    	node.description = "node 1";
    
    	return 0;
    }
    MyLibrary is a dll project, and the MyLibrary header includes this definition od Node:
    Code:
    class ITINARARYFINDA_API Node {
    public:
    	float x;
    	float y;
    	std::string description;
    };
    the problem is, that there is a debug assertion failed error on expression _CrtIsValidHeapPointer(pUserData) at runtime, when the main function returns. i'm fairly sure it occurs when the std::string goes out of scope, since i've had similar problems before.

    am i doing something wrong here?

  2. #2
    Join Date
    Feb 2002
    Location
    Krispl, Austria
    Posts
    197

    Re: std::string going out of scope

    There are problems exporting classes using templates as discussed here http://www.codeproject.com/cpp/MemberTemplateDLL.asp .

    std:string is a template STL class so I'm you're probably experiencing the same sorts of problems.
    Requests such as
    "I need to write an new language compiler by next week, I have teach yourself c++ in 21 days, can someone give me example code?" will be ignored.

  3. #3
    Join Date
    Oct 2004
    Posts
    198

    Re: std::string going out of scope

    Quote Originally Posted by Bob Sheep
    There are problems exporting classes using templates as discussed here http://www.codeproject.com/cpp/MemberTemplateDLL.asp .

    std:string is a template STL class so I'm you're probably experiencing the same sorts of problems.
    these seem to be linker errors, but i dont have a problem compiling it, just at runtime..

    useful link though, thanks very much.

  4. #4
    Join Date
    Feb 2002
    Location
    Krispl, Austria
    Posts
    197

    Re: std::string going out of scope

    can u post your test project, I'll look at it with some diagnostice software I have here.
    Requests such as
    "I need to write an new language compiler by next week, I have teach yourself c++ in 21 days, can someone give me example code?" will be ignored.

  5. #5
    Join Date
    Oct 2004
    Posts
    198

    Re: std::string going out of scope

    Quote Originally Posted by Bob Sheep
    can u post your test project, I'll look at it with some diagnostice software I have here.
    thanks, the dll project is call itinararyfinda and the app is called iftestapp.
    Attached Files Attached Files

  6. #6
    Join Date
    Feb 2002
    Location
    Krispl, Austria
    Posts
    197

    Re: std::string going out of scope

    I get this error compiling the dll with Visual Studion 2003

    warning C4251: 'Itinarary::coords' : class 'std::vector<_Ty>' needs to have dll-interface to be used by clients of class 'Itinarary'

    I do not get a crash however but the cause is explained here

    http://support.microsoft.com/kb/q168958/
    http://support.microsoft.com/kb/q172396/
    Requests such as
    "I need to write an new language compiler by next week, I have teach yourself c++ in 21 days, can someone give me example code?" will be ignored.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: std::string going out of scope

    Quote Originally Posted by peterworth
    i'm sure the whole stl being used in a dll thing has been discussed a lot already,
    I guess you missed the many discussions that both the app and DLL must use the DLL version of the runtime library. The reason why is that classes that allocate memory internally in one module must use the same heap manager when deallocating in another module. Only the DLL runtime library ensures this.

    Also another issue with what you're doing is that you will get in a lot of trouble with your application if the app uses a different version of the string class as the DLL. In other words, your DLL will only work with an app created with the exact same version and service pack build of Visual C++ as the DLL was created with. For example, if you created the DLL with the stock version of Visual C++, and I create an app that uses the Visual C++ STLPort version of the STL classes, your DLL cannot be used by my application.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Oct 2004
    Posts
    198

    Re: std::string going out of scope

    is my dll not using the dll version of the runtime library then? how do i fix that?

  9. #9
    Join Date
    Feb 2002
    Location
    Krispl, Austria
    Posts
    197

    Re: std::string going out of scope

    This is another KB article which explains how to export STL classes.
    It also says you require < and == operator functions to be defined as well.

    http://support.microsoft.com/default...b;EN-US;168958

    Because I had similar problems I ended up writing my own map, array classes to manage void pointers and then wrapped these basic classes in a class specific to each object type.
    Last edited by Bob Sheep; July 21st, 2005 at 07:52 AM.
    Requests such as
    "I need to write an new language compiler by next week, I have teach yourself c++ in 21 days, can someone give me example code?" will be ignored.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured