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
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';
}
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.
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.
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;