Hi all,
Not sure if this is OT?
I have a function in a library written in C++ which returns a
strdup(s.c_str( )) to an application written i C. Running Valgrind on my
C-application shows this results in memory leaks due to the c_str is never
freed.
Example:
Foo.h
#ifdef __cplusplus
class Foo {
public:
std::string getString() { return s; }
private:
std::string s;
};
#else
typedef struct Foo Foo;
#endif
#ifdef __cplusplus
extern "C" {
#endif
extern char *cplusplus_getS tring(Foo *); // C++ call-back
extern char *get_string(Foo *); // C function
#ifdef __cplusplus
}
#endif
Foo.cc
char *cplusplus_getS tring(Foo *foo)
{
std::string s;
s = foo->getString();
return strdup(s.c_str( )) // this is coursing memory leaks.
}
main.c
char *get_string(str uct Foo *foo)
{
return cplusplus_getSt ring(foo);
}
int main()
{
struct Foo *foo;
printf("%s\n", get_string());
return 0;
}
--
Hilsen/Regards
Michael Rasmussen http://keyserver.veridis.com:11371/p...rch=0xE3E80917 22 3244
"Michael Rasmussen" <mi*@miras.or g> wrote in message
news:pa******** *************** *****@miras.org ... Hi all,
Not sure if this is OT?
I have a function in a library written in C++ which returns a strdup(s.c_str( )) to an application written i C. Running Valgrind on my C-application shows this results in memory leaks due to the c_str is never freed.
Example:
Foo.h
#ifdef __cplusplus
class Foo { public: std::string getString() { return s; }
private: std::string s; };
#else
typedef struct Foo Foo;
#endif
#ifdef __cplusplus
extern "C" {
#endif
extern char *cplusplus_getS tring(Foo *); // C++ call-back extern char *get_string(Foo *); // C function
#ifdef __cplusplus
}
#endif
Foo.cc
char *cplusplus_getS tring(Foo *foo) { std::string s;
s = foo->getString(); return strdup(s.c_str( )) // this is coursing memory leaks. }
Have you tried a simple
return s.c_str();
This will return a const char* to a buffer inside of s that remains until
changes are made to s. As long as your main.c doesn't try to change the
contents, everything should (read probably) work. main.c
char *get_string(str uct Foo *foo) { return cplusplus_getSt ring(foo); }
int main() { struct Foo *foo;
printf("%s\n", get_string()); return 0; }
-- Hilsen/Regards Michael Rasmussen http://keyserver.veridis.com:11371/p...rch=0xE3E80917
On Thu, 09 Feb 2006 16:11:23 -0800, Jim Langston wrote: Have you tried a simple return s.c_str();
This will return a const char* to a buffer inside of s that remains until changes are made to s. As long as your main.c doesn't try to change the contents, everything should (read probably) work.
Yes. It produces this output from Valgrind:
==7412== Invalid read of size 2
==7412== at 0x40BBA1A: mempcpy (in /lib/tls/i686/cmov/libc-2.3.5.so)
==7412== by 0x408D28B: vfprintf (in /lib/tls/i686/cmov/libc-2.3.5.so)
==7412== by 0x4092B02: printf (in /lib/tls/i686/cmov/libc-2.3.5.so)
==7412== by 0x804881C: main (main.c:56)
==7412== Address 0x429DB4C is 12 bytes inside a block of size 109 free'd
==7412== at 0x401C304: operator delete(void*) (vg_replace_mal loc.c:246)
==7412== by 0x421733C: std::string::_R ep::_M_destroy( std::allocator< char> const&) (in /usr/lib/libstdc++.so.6. 0.7)
==7412== by 0x42184E7: std::string::~s tring() (in
/usr/lib/libstdc++.so.6. 0.7)
return s.c_str() and return strdup(s.c_str( )) is coursing memory leaks.
Any other way to return a string as a C-string? Would returning a char
buffer not give me the same problems?
--
Hilsen/Regards
Michael Rasmussen http://keyserver.veridis.com:11371/p...rch=0xE3E80917
Michael Rasmussen wrote: Hi all,
Not sure if this is OT?
I have a function in a library written in C++ which returns a strdup(s.c_str( )) to an application written i C. Running Valgrind on my C-application shows this results in memory leaks due to the c_str is never freed.
Actually, the leak is due to the memory allocated by strdup() never
being freed.
"red floyd" <no*****@here.d ude> wrote in message
news:Xf******** ***********@new ssvr13.news.pro digy.com... Michael Rasmussen wrote: Hi all,
Not sure if this is OT?
I have a function in a library written in C++ which returns a strdup(s.c_str( )) to an application written i C. Running Valgrind on my C-application shows this results in memory leaks due to the c_str is never freed.
Actually, the leak is due to the memory allocated by strdup() never being freed.
Well, I guess you'll have to do it the ugly C style way then.
Pass a char* from C to the C++ function with preallocated space and strcpy
into that.
Jim Langston wrote: "Michael Rasmussen" <mi*@miras.or g> wrote in message news:pa******** *************** *****@miras.org ... Hi all,
Not sure if this is OT?
I have a function in a library written in C++ which returns a strdup(s.c_str( )) to an application written i C. Running Valgrind on my C-application shows this results in memory leaks due to the c_str is never freed.
Example:
Foo.h
#ifdef __cplusplus
class Foo { public: std::string getString() { return s; }
private: std::string s; };
#else
typedef struct Foo Foo;
#endif
#ifdef __cplusplus
extern "C" {
#endif
extern char *cplusplus_getS tring(Foo *); // C++ call-back extern char *get_string(Foo *); // C function
#ifdef __cplusplus
}
#endif
Foo.cc
char *cplusplus_getS tring(Foo *foo) { std::string s;
s = foo->getString(); return strdup(s.c_str( )) // this is coursing memory leaks. } Have you tried a simple return s.c_str();
How is that going to help? You're returning a pointer to something
that comes out of a local object. You're essentially returning the
local object. This is called "a dangling pointer".
This will return a const char* to a buffer inside of s that remains until changes are made to s. As long as your main.c doesn't try to change the contents, everything should (read probably) work.
What are you talking about? As soon as the closing curly brace is
reached, the 's' is _destroyed_.
AFAIK, 'strdup' is not a standard function, but using 'strdup' is
probably the only way to preserve that string. You can, of course,
do 'malloc' and 'strcpy', which probably what 'strdup' does under
the covers anyway.
[..]
V
--
Please remove capital As from my address when replying by mail
Jim Langston wrote: "red floyd" <no*****@here.d ude> wrote in message news:Xf******** ***********@new ssvr13.news.pro digy.com... Michael Rasmussen wrote: Hi all,
Not sure if this is OT?
I have a function in a library written in C++ which returns a strdup(s.c_str( )) to an application written i C. Running Valgrind on my C-application shows this results in memory leaks due to the c_str is never freed.
Actually, the leak is due to the memory allocated by strdup() never being freed.
Well, I guess you'll have to do it the ugly C style way then.
Pass a char* from C to the C++ function with preallocated space and strcpy into that.
I suppose it might be a simpler solution. Of course, the C program
could always simply call 'free' for that pointer after it's done with
it, no? I guess, "man strdup" is in order...
V
--
Please remove capital As from my address when replying by mail
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:u9******** ************@co mcast.com... Jim Langston wrote: "red floyd" <no*****@here.d ude> wrote in message news:Xf******** ***********@new ssvr13.news.pro digy.com... Michael Rasmussen wrote: Hi all,
Not sure if this is OT?
I have a function in a library written in C++ which returns a strdup(s.c_str( )) to an application written i C. Running Valgrind on my C-application shows this results in memory leaks due to the c_str is never freed.
Actually, the leak is due to the memory allocated by strdup() never being freed.
Well, I guess you'll have to do it the ugly C style way then.
Pass a char* from C to the C++ function with preallocated space and strcpy into that.
I suppose it might be a simpler solution. Of course, the C program could always simply call 'free' for that pointer after it's done with it, no? I guess, "man strdup" is in order...
V -- Please remove capital As from my address when replying by mail
Best I could come up with is this:
const char* f(const char* text)
{
static char returnthis[1000];
std::string tempstring(text );
strcpy( returnthis, tempstring.c_st r() );
return returnthis;
}
Jim Langston wrote: "Victor Bazarov" <v.********@com Acast.net> wrote in message news:u9******** ************@co mcast.com... Jim Langston wrote: "red floyd" <no*****@here.d ude> wrote in message news:Xf******** ***********@new ssvr13.news.pro digy.com... Michael Rasmussen wrote: > Hi all, > > Not sure if this is OT? > > I have a function in a library written in C++ which returns a > strdup(s.c_str( )) to an application written i C. Running Valgrind > on my C-application shows this results in memory leaks due to the > c_str is never > freed. >
Actually, the leak is due to the memory allocated by strdup() never being freed.
Well, I guess you'll have to do it the ugly C style way then.
Pass a char* from C to the C++ function with preallocated space and strcpy into that.
I suppose it might be a simpler solution. Of course, the C program could always simply call 'free' for that pointer after it's done with it, no? I guess, "man strdup" is in order...
V -- Please remove capital As from my address when replying by mail
Best I could come up with is this: const char* f(const char* text) { static char returnthis[1000]; std::string tempstring(text ); strcpy( returnthis, tempstring.c_st r() ); return returnthis; }
Uh... What exactly is the point of all this? If you pass a char const*
in and you expect a char const* out, why do you need all this dancing
around with a static array, with an automatic std::string? It seems that
the intention here is
const char* useless_functio n(const char* text)
{
return text;
}
Of course, the OP had a C++ object with a _member_ of type 'std::string'
and needed to extract the contents somewhere... You must have simplified
the original intention just a tad too much.
V
--
Please remove capital As from my address when replying by mail
On Thu, 09 Feb 2006 18:18:49 -0800, Jim Langston wrote: Best I could come up with is this: const char* f(const char* text) { static char returnthis[1000]; std::string tempstring(text ); strcpy( returnthis, tempstring.c_st r() ); return returnthis; } }
I was given a more "clean" solution on a Linux list. The solution is to
create a string as a member of the class and then use this string as a
temporary storage. From this member you are allowed to return s.c_str()
since this string will be alive as long as the instance of the class. This
is also, in my opinion, a bether solution since it keeps encapsulation -
You will have to call the class if you need any change. More correct OOP
style.
Correct example:
Foo.h
#ifdef __cplusplus
class Foo {
public:
std::string cstring;
std::string getString() { return s; }
private:
std::string s;
};
#else
typedef struct Foo Foo;
#endif
#ifdef __cplusplus
extern "C" {
#endif
extern const char *cplusplus_getS tring(Foo *); // C++ call-back extern
const char *get_string(Foo *); // C function
#ifdef __cplusplus
}
#endif
Foo.cc
const char *cplusplus_getS tring(Foo *foo)
{
cstring = foo->getString();
return cstring.c_str() // no memory leak since it is destroyed from
the class' destructor.
}
main.c
const char *get_string(str uct Foo *foo)
{
return cplusplus_getSt ring(foo);
}
int main()
{
struct Foo *foo;
printf("%s\n", get_string());
return 0;
}
--
Hilsen/Regards
Michael Rasmussen http://keyserver.veridis.com:11371/p...rch=0xE3E80917 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Phil Powell |
last post by:
My first time working with a PHP class, and after 6 hours of working out the
kinks I am unable to return a value from the class, so now I appeal to the
general audience what on earth did I do wrong this time?
This is the code the retrieves the values:
if (($hasRegistered || $hasPreRegistered) && !empty($uplinenumber)) {
// CHECK TO SEE IF UPLINE NUMBER IS A VALID NUMBER
$regNumberGenerator = new RegNumberGenerator($uplinenumber,...
|
by: Jose Fernandez |
last post by:
Hello.
I'm building a web service and I get this error.
NEWS.News.CoverNews(string)': not all code paths return a value
This is the WebMethod
public SqlDataReader CoverNews(string Sport)
{
|
by: Michael Maes |
last post by:
Hello,
I have a BaseClass and many Classes which all inherit (directly) from the BaseClass.
One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk.
1. The Deserialization goes like:
#Region " Load "
|
by: nafri |
last post by:
hello all,
I want to create a function that returns the first element of the Array
that is input to it. However, the Input Array can be an Array of points,
double, or anyother type, which means the return type of the function depends
on the type of the objects the Input array holds.
I can have the return type as Object, but it has problems like late binding
and more importantly the client has to always Cast it. For example.
class...
|
by: Dany |
last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by Microsoft must be installed on their servers.
Now german Umlaute (ä, ü, ö) and quotes are returned incorrectly in SOAP fault responses.
This can be easily verified:
Implement the following in a web service method (just raises a SOAPException with a...
| |
by: MSDNAndi |
last post by:
Hi,
I have a set of simple webservices calls that worked fine using .NET
Framework 1.0.
I am calling a Java/Apache based webservices, the calling side is not able
to supply a proper WSDL.
What it does is to call a webservice with two parameters, one being a
integer, the other one being a "String" which contains XML (not the best
practice, however that is the target interface and we cannot change that).
|
by: Pedro Pinto |
last post by:
Hi there once more........
Instead of showing all the code my problem is simple.
I've tried to create this function:
char temp(char *string){
alterString(string);
return string;
|
by: utab |
last post by:
Dear all,
I tried sth easy(actually this was an exercise) but I tried to use the
standard lib. heavily for this problem(as far as I can). There was one
point I could not figure out. The problem is :
../a.out 1.3+3.2+.1+40/3*8/7-4*5-32
The program will parse the argument and find the result of the above
expression. I have two versions(the 2nd is working , not perfect ;-}),
|
by: KoreyAusTex |
last post by:
If anyone can help me figure out the what the missing return statements are, I think it might be the fact that I need to add a return false in the getValue()?
import java.util.*;
public class Card
{
// instance variables
//suits
private int suit;
private int spades;
|
by: =?Utf-8?B?QmVu?= |
last post by:
Hi all,
I'm trying to understand the concept of returning functions from the
enclosing functions. This idea is new to me and I don't understand when and
why I would need to use it.
Can someone please shed some light on this subject?
Thanks a lot,
Ben
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |