linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Martin Buchan <M.J.Buchan@gre.ac.uk>
To: linux-c-programming@vger.kernel.org
Subject: segfault with strdup
Date: Tue, 20 May 2003 12:20:34 +0100	[thread overview]
Message-ID: <20030520112034.GZ674@gre.ac.uk> (raw)

Hi,

I have built a C program which parses an XML file containing
application names and the command to run those applications. Once i
parse the file, i then build a GTK menu - like the GNOME start
button thingy with all the apps put into their relevant sections. I.e.

Internet    -> Web Browsers -> Mozilla Web Browser
                            -> Netscape Navigator 
                            -> Lynx Text Only Browser

            -> Email        -> Mozilla Mail
                            -> Mutt
		  
Programming -> Anjuta
            -> Sun Forte



Etc etc.

How i build the menu is by parsing the XML file and sticking the
results in an array of structs and then looping thru the array and
building the buttons, submenu and menu entries.

This is all working now, however, it only works if i run it in the
directory that i have done all my coding in. I.e. If i compile and
install to a different location, i get a seg fault when parsing the
XML file. Its not that it cant find the file, it goes thru and
parses the first entry in the file and then croaks on the 2nd
while parsing the name of the app - which is "Netscape Navigator"
If i shorten the name to just "Netscape", it then croaks on the 3rd
entry which is "Lynx Text Only Browser", if i then shorten this to
just "Lynx Browser", it then croaks on the 4th and so on and so on.

I have some #define 's which state the location of some of my files
like so:

#define APPLIST "/home/bm98/prg/gtk-menu/current/applist.xml"
#define XPMDIR "/home/bm98/prg/gtk-menu/current/xpms/"
#define HELPDIR "/home/bm98/prg/gtk-menu/current/help/"

If i use these locations, it works fine but if i change it to 

#define APPLIST "/home/bm98/menu/applist.xml"
#define XPMDIR "/home/bm98/menu/xpms/"
#define HELPDIR "/home/bm98/menu/help/"

It doesnt work. (Ultimately i want to get these values from a
./configure script with a --prefix=XXX so that i dont need to 
hard code it etc)

Here is my struct

struct menuentry
{
  gchar *appsection;
  gchar *sectionxpm;
  gchar *appsubmenu;
  gchar *appname;
  gchar *appcommand;
};
			 
typedef struct menuentry menuentry;
			 
And here is how i allocate space for it.

menuentry *mePtr = malloc( n * sizeof (menuentry) );


As i say, this works fine for the first set of locations, but not
for the second.

The place where it is seg faulting is

uri = xmlGetProp(cur, "name");
mePtr[i].appname = strdup(uri);  /* HERE */

Here is some output from DDD:

#3  0x123fc in parseApp (doc=0x3d450, cur=0x39c98, mePtr=0x29708, i=1) at menu-0.9.1.c:207
#2  0xfeccf2a4 in strdup () from /usr/lib/libc.so.1
#1  0xfecc1c90 in malloc () from /usr/lib/libc.so.1
#0  0xfecc1c48 in _smalloc () from /usr/lib/libc.so.1


Can anyone spot anything from this info where I am going wrong. I
will gladly post more info from ddd or more of my source code if
this will help.


Thanks
Martin

             reply	other threads:[~2003-05-20 11:20 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-05-20 11:20 Martin Buchan [this message]
2003-05-20 12:25 ` segfault with strdup Chris Nanakos
2003-05-20 12:46   ` Martin Buchan
2003-05-20 13:12   ` Martin Buchan
     [not found] ` <200305201316.h4KDGUhG007842@grinch.txt.com>
2003-05-20 13:52   ` Martin Buchan
2003-05-20 16:32 ` Glynn Clements
2003-05-21  9:50   ` Martin Buchan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20030520112034.GZ674@gre.ac.uk \
    --to=m.j.buchan@gre.ac.uk \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).