Returning reference gives Segmentation Fault. How to solve it?

1,317 views
Skip to first unread message

Riasat Abir

unread,
Mar 30, 2011, 4:31:34 AM3/30/11
to andro...@googlegroups.com
While debugging I've experienced that returning reference in the native code is giving segmentation fault.

Such as calling this function tolower from another cpp code generates segmentation fault : sHost  = tolower(sType); //const std::string& sType

string tolower(const string& _str)
{
int iSize = _str.size();
char* pStr = (char*)_str.data();
for (int i = 0; i < iSize; i++)
        pStr[i] = tolower(pStr[i]);

return _str;
}

-------------------------CONSOLE--------------------------
411                     sHost  = tolower(sType);
(gdb) n

Program received signal SIGSEGV, Segmentation fault.
0xafd15ef0 in __libc_android_abort () from G:/afe/obj/local/armeabi/libc.so


Anyone can try returning a reference works or not in the native code?

Hasn't anyone faced this problem?

David Turner

unread,
Mar 30, 2011, 4:45:17 AM3/30/11
to andro...@googlegroups.com, Riasat Abir
You should never mofidy the content return by std::string.data(), especially when you have a "const string&" reference.

"""
The returned array points to an internal location which should not be modified directly in the program. Its contents are guaranteed to remain unchanged only until the next call to a non-constant member function of the string object.
"""

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.

alan

unread,
Mar 30, 2011, 4:48:03 AM3/30/11
to andro...@googlegroups.com
the body of your function is more likely to be the problem, you are modifying the data of a constant string which is bad. you code should be:

string tolower(const string& _str)
{
        string result = _str;
int iSize = result.size();
        for (int i = 0; i < iSize; i++)
           result[i] = tolower(result[i]);

return result;
}

casting the result of data() to a "char *" is also incorrect, it is "const char *" for a reason, it means that you shouldn't modify it and that if you do modify it the behaviour will be undefined. see the documentation at http://www.cplusplus.com/reference/string/string/data/

Riasat Abir

unread,
Mar 30, 2011, 5:05:24 AM3/30/11
to andro...@googlegroups.com
Not only here I am getting segmentation fault everywhere parameter reference has been used.

Like this: the bold line execution gives the segmentation fault. called from st.nextToken(sType); //string& sType

bool StringTokenizer::nextToken(string& _token)
{
string::size_type token_start(pos);
bool within_token(false), within_quote(false);
string::size_type iSize = str.size();

const char* pStr = str.data();
for (; pos < iSize; pos++){
switch (pStr[pos]){
       // whitespace
case ' ':
case '\t':
case '\n':
case '\r':
if (within_quote)
break;
if (within_token)
{
string::size_type token_end(pos-token_start); 
             _token = str.substr(token_start, token_end);  
return true;
}
break;
/* */

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.



--

HimHim

Riasat Abir

unread,
Mar 30, 2011, 5:14:18 AM3/30/11
to andro...@googlegroups.com
string tolower(const string& _str)
{
        string result = _str;
int iSize = result.size();
        for (int i = 0; i < iSize; i++)
           result[i] = tolower(result[i]);

return result;
}

Tried your modified code, it also gives segmentation fault:

411                     sHost  = tolower(sType);
Current language:  auto; currently c++
(gdb) n
warning: (Internal error: pc 0x80cb820c in read in psymtab, but not in symtab.)
warning: (Internal error: pc 0x80cb820c in read in psymtab, but not in symtab.)

Program received signal SIGSEGV, Segmentation fault.
0xafd15ef0 in __libc_android_abort () from G:/afe/obj/local/armeabi/libc.so

On Wed, Mar 30, 2011 at 2:48 PM, alan <al...@birtles.org.uk> wrote:

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.



--

HimHim

Riasat Abir

unread,
Mar 30, 2011, 5:31:05 AM3/30/11
to andro...@googlegroups.com
I've found one more thing:

sHost = sType;

Even this simple assigning gives segmentation fault :S
--

HimHim

alan

unread,
Mar 30, 2011, 5:43:38 AM3/30/11
to andro...@googlegroups.com
is sType declared like this:
string& sType;
it should be:
string sType;
again i would recommend you buy a good c++ book and read up about references

Riasat Abir

unread,
Mar 30, 2011, 6:10:47 AM3/30/11
to andro...@googlegroups.com
yes declared as: string sType

[Actually its giving error on NDK, works on windows & qnx both platform, I am just working on porting to Android]

If I create local string variable sHostTmp and store the sType to it, it doesn't make any problem.

But if I want to use in the reference sHost passed to this function, it gives segmentation fault.

So I assumed its ndk problem with using reference :s

Because anywhere I want store in the reference I get segmentation fault.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.



--

HimHim

mingw android

unread,
Mar 30, 2011, 6:13:16 AM3/30/11
to andro...@googlegroups.com

You really need to read about C++ before attempting to use it.

> HimHim

alan

unread,
Mar 30, 2011, 6:34:09 AM3/30/11
to andro...@googlegroups.com
references definitely work as I use them extensively myself, there must be some other problem in your code which is causing the references to crash

mingw android

unread,
Mar 30, 2011, 6:36:40 AM3/30/11
to andro...@googlegroups.com

Yeah, I've compiled and run qt lighthouse, and that uses more than a few C++ features.

> references definitely work as I use them extensively myself, there must be
> some other problem in your code which is causing the references to crash
>

Riasat Abir

unread,
Mar 30, 2011, 8:27:14 AM3/30/11
to andro...@googlegroups.com
Ok I've found why the problem is happening.
(Not the solution yet!)

I have written a test file named test.cpp. I wrote a function named testStr in it :

std::string& testStr(std::string& a, const std::string& x, std::string& y, std::string& z)
{
a = x;
a +=y;
a +=z;

return a;
}

when I call this function within another function of test.cpp there is no probelm.

But if this testStr function is written in another file suppose in test1.cpp and i call this function from test.cpp (i have included the header file test1.h in it & used extern) then it's giving segmentation fault when I am trying to assign anything to that reference.


--

HimHim

mingw android

unread,
Mar 30, 2011, 9:04:00 AM3/30/11
to andro...@googlegroups.com

Can you also paste the calling code. I do think you're asking us how to code in C++ though still!

One option would be to use c only. It doesn't have references so you'll not be able to write broken referencing code with that language.

> Ok I've found why the problem is happening.
> (Not the solution yet!)
>
> I have written a test file named test.cpp. I wrote a function named testStr
> in it :
>
> std::string& testStr(std::string& a, const std::string& x, std::string& y,
> std::string& z)
> {
> *a = x;*
> HimHim

Riasat Abir

unread,
Mar 30, 2011, 9:41:43 AM3/30/11
to andro...@googlegroups.com
I've tried this : calling a function (with parameter reference) from  another cpp file, I can't assign anything to the parameter passed by reference.

in a function Java_com_example_afeTest_testReference in test.cpp
-----------------------------------------
std::string a ="";
const std::string x = "jo";
std::string y = "yi", z = "ta";
testFunc(a,x,y, z);           ////// call test function which resides in same cpp no error
---------------------------------------------

Now put this function in test.cpp it works
----------------------------------------------------------
std::string& testFunc(std::string& a, const std::string& x, std::string& y, std::string& z)
{
a = x;
a +=y;
a +=z;

return a;
}
---------------------------------------------------------
Now put this testFunc in a different test2.cpp & call it from test.cpp it will produce segmentation fault after
a=x 

** So if testFunc & Java_com_example_afeTest_testReference  is same cpp file it works, if two functions in two cpp files and you want to pass reference, it gives segmentation fault. I've checked with extern also.

HimHim

alan

unread,
Mar 30, 2011, 10:25:58 AM3/30/11
to andro...@googlegroups.com
i have compiled and run the above code and it does not crash

Riasat Abir

unread,
Mar 31, 2011, 2:45:08 AM3/31/11
to andro...@googlegroups.com
Ok I've gone deep and found the problem why yours is working and mine not.

I've tried to pass reference from a function in afeTest library (test.cpp)  to a function of different library (AFWUtil.cpp). (both are shared library)
Then it causes the segmentation fault.

But now I tried to use reference from one cpp file function to another cpp file function within the same library and it works.
(LOCAL_SRC_FILES := test.cpp test2.cpp)

So is there anything to do to link between the libraries?
I've loaded both libraries with

System.loadLibrary("basemulti");
System.loadLibrary("AFETest");

Does it make any sense?

On Wed, Mar 30, 2011 at 8:25 PM, alan <al...@birtles.org.uk> wrote:
i have compiled and run the above code and it does not crash

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.



--

HimHim

alan

unread,
Mar 31, 2011, 4:30:23 AM3/31/11
to andro...@googlegroups.com
passing c++ objects across shared libraries is not well supported unless you have your c++ runtime in a shared library as well. until there is a shared version of gnu stl you should use stlport_shared or link all your code into a single library

Riasat Abir

unread,
Mar 31, 2011, 6:29:50 AM3/31/11
to andro...@googlegroups.com
Thanks.

Now it makes sense!

But as I've to use exception I can't stlport_shared.

So I guess I've to build the libraries as static, isn't it?

To demonstrate the problem I've just modified the two-libs sample, made two libraries shared and passed reference from one to another. Interested users can check that it gives segmentation fault passing reference across shared libraries.

On Thu, Mar 31, 2011 at 2:30 PM, alan <al...@birtles.org.uk> wrote:
passing c++ objects across shared libraries is not well supported unless you have your c++ runtime in a shared library as well. until there is a shared version of gnu stl you should use stlport_shared or link all your code into a single library
--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.


--

HimHim

two-libs.rar
Reply all
Reply to author
Forward
0 new messages