CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2006
    Posts
    7

    Lightbulb Free is getting called for string object

    Hi,
    I have a class with a string variable like

    Code:
    #ifdef __GNUC__
    #define DLL_PUBLIC __attribute__ ((visibility ("default")))
    #elif DLL_PUBLIC_IMPORT
    #define DLL_PUBLIC __declspec(dllimport) // Note: actually gcc seems to also supports this syntax.
    #else
    #define DLL_PUBLIC __declspec(dllexport) // Note: actually gcc seems to also supports this syntax.
    #endif
    
    class Status
        {
        public:
            enum Errors
            {
                SUCCESS,
                FAILURE,
             };
            
            std::string msg;
    }
    
    DLL_PUBLIC  Status fun()
    {
    Status obj;
    return obj;
    }
    No when i call the function fun in a client problem then it is crashing.

    Code:
    #0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
    #1  0x00007ffffe880921 in __GI_abort () at abort.c:79
    #2  0x00007ffffe8c9967 in __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x7ffffe9f6b0d "%s\n") at ../sysdeps/posix/libc_fatal.c:181
    #3  0x00007ffffe8d09da in malloc_printerr (str=str@entry=0x7ffffe9f8720 "munmap_chunk(): invalid pointer") at malloc.c:5342
    #4  0x00007ffffe8d7fbc in munmap_chunk (p=0x7fffff7d1938) at malloc.c:2846
    #5  __GI___libc_free (mem=0x7fffff7d1948) at malloc.c:3127
    #6  0x00000000080062dc in __gnu_cxx::new_allocator<char>::deallocate(char*, unsigned long) ()
    #7  0x0000000008005e61 in std::allocator_traits<std::allocator<char> >::deallocate(std::allocator<char>&, char*, unsigned long) ()
    #8  0x000000000800590c in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_destroy(unsigned long) ()
    #9  0x0000000008005734 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_dispose() ()
    #10 0x00000000080052c4 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() ()
    #11 0x0000000008004b94 in Status::~Status() ()
    #12 0x000000000800480d in main ()
    so free is getting called for the string object and it is crashing. Any suggestion how to avoid this
    requirements
    1) i need to return the object not the pointer
    2) i need the string object in the class i.e std::string msg

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,563

    Re: Free is getting called for string object

    OK. This compiles and runs OK with VS2019

    Code:
    #include <string>
    #include <iostream>
    
    class Status
    {
    public:
    	std::string msg;
    };
    
    auto fun(const std::string& str)
    {
    	Status obj {str};
    
    	return obj;
    }
    
    int main()
    {
    	auto stat {fun("hello!")};
    
    	std::cout << stat.msg << '\n';
    }
    Last edited by 2kaud; June 14th, 2021 at 07:50 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2022 (17.0.2)

  3. #3
    Join Date
    Dec 2006
    Posts
    7

    Re: Free is getting called for string object

    This is part of a program from multiple files.

    What is done is a object is returned from a function with extern "C". An so(shared object) file is created with this to be used by client programs. Also the function is having a string object.
    Now the so file is used in a client program and the function is called. I am doing it in c++17 in gcc.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,563

    Re: Free is getting called for string object

    Haven't tried across a dll - but there are issues re using std::containers across .dlls. Not advised.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2022 (17.0.2)

  5. #5
    Join Date
    Jun 2021
    Posts
    10

    Post Re: Free is getting called for string object

    You should try this code-
    HTML Code:
    std::string foo("since it's on the stack, it will auto delete out of scope");
    or
    HTML Code:
    std::string* foo = new std::string("allocated on the heap needs explicit destruction")
    delete foo;

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