linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* segfault with strdup
@ 2003-05-20 11:20 Martin Buchan
  2003-05-20 12:25 ` Chris Nanakos
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Martin Buchan @ 2003-05-20 11:20 UTC (permalink / raw)
  To: linux-c-programming

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2003-05-21  9:50 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-05-20 11:20 segfault with strdup Martin Buchan
2003-05-20 12:25 ` 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

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).