0

I know it's a common question but I didn't find a suitable explanation yet! My project compiles but I keep getting the Thread: signal SIGABRT! - the strcpy function; nothing's written in the debugger window and when I debug step by step the test works perfectly fine and I get success, but when I try to run it again I get that error! How can I fix it?

here's my code :

static char* copyMember(const char* str){
    if(str==NULL){
        return NULL;
    }
    char* newStr=malloc((strlen(str)+1)*sizeof(char));
    if(newStr==NULL){
        return NULL;
    }
    return strcpy(newStr, str);
}
alk
  • 69,737
  • 10
  • 105
  • 255
Raw305
  • 31
  • 5
  • 5
    It should be input parameter `str` related, the logic is no problem in your code – Eric Tsui Jun 28 '15 at 05:46
  • I thought about that but in the debugger window I can see that: str = (const char*) "moshe" *str=m newStr=(char*) "moshe" *newStr=m – Raw305 Jun 28 '15 at 05:55
  • I tried running this code, and it works. Perhaps you aren't importing or linking the proper libraries when you build. What are your `#include`s? Do you get any compiler warnings? – sudo Jun 28 '15 at 05:56
  • No compiler warnings! – Raw305 Jun 28 '15 at 05:59
  • 2
    @RawanKhazen The memory related problem like overflow will probably trigger Thread: signal SIGABRT! even when you can run it under debug mode. The `str` argument or your code exec context could result in it. So, paste the code context would help. – Eric Tsui Jun 28 '15 at 06:00
  • 2
    On which OS, with which compiler, compilation command, standard C library, ...? `SIGABRT` is generally related to `abort()` being called (perhaps by a failing `assert`). Very probably the issue is elsewhere. Try using [valgrind](http://valgrind.org/) if possible. – Basile Starynkevitch Jun 28 '15 at 06:06
  • BTW, many implementations have a [strdup](http://pubs.opengroup.org/onlinepubs/009695399/functions/strdup.html) doing exactly what your `copyMember` (not a very nice name, BTW) does. – Basile Starynkevitch Jun 28 '15 at 06:07
  • 1
    Is it multi-threaded - in which case is another thread doing something with `str` – Ed Heal Jun 28 '15 at 06:14
  • @EricTsui you were right, I finally figured out where the problem was and could fix it! Thanks guys :)) – Raw305 Jun 28 '15 at 07:21
  • @RawanKhazen Glad you resolved it. And you can also update/edit again to share your fix with others if you'd like. – Eric Tsui Jun 28 '15 at 07:25
  • Does the code provide the prototype to `strcpy()` (typically by `#include`ing `` and is compiled on 64bits? – alk Jun 28 '15 at 13:20

0 Answers0