Question on StringValuePtr, rb_str_dup

B

Berger, Daniel

Hi all,

I'm having a hard time preserving a string. Essentially, what's
happening is that even though I'm dup'ing the original string, after I
pass it a C function that modifies the receiver, both the original
string *and* the dup appear to be affected.

Here's a generic example of what I mean:

static VALUE foo(VALUE klass, VALUE rstring_orig){
VALUE rstring;
char* string;

rstring =3D rb_str_dup(rstring_orig);
string =3D StringValuePtr(rstring);

c_func_that_modifies_receiver(string);

return rb_str_new2(string); =20
}

string =3D "hello"
foo(string)
p string # modified, but shouldn't be.

In other words, even though I've dup'd the original string and have sent
the dup to the C function that modifies the receiver, the original
string is still being modified.

What am I doing wrong here?

Thanks.

Dan
 
N

nobu.nokada

Hi,

At Wed, 14 Sep 2005 06:38:44 +0900,
Berger, Daniel wrote in [ruby-talk:156009]:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top