This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Abort when shared libary (g++) function should catch thrown exception



Compiler: egcs-2.91.66  (invoked as g++)
Loader: GNU ld 2.9.1
CPU: Pentium II
OS: Caldera OpenLinux 2.3 (Linux Kernel 2.2.10 )
  uname -a -> Linux acsifw2.acsi.com 2.2.10 #1 SMP Tue Aug 10 19:01:45 MDT 1999 i586 unknown

Compiler arguments: see Makefile, below

Problem Synopsis:

When a function containing a try/catch construct is compiler with g++ and
loaded into a shared object/library, exceptions thrown by functions called within
the shared object function's try/catch block (and which should be caught in the
catch block) cause an "Abort".

I searched the GNU site and GCC bugs for something that sounded like this problem and
found nothing.  

For what it might be worth, the Sun C++4.0.1 and C++5.0.1 compilers yielded similar
failures but the thrown exception was caught by the inner catch block in MAIN instead
of the catch block in afunc()!  

Problem Demonstration:

The following files provide a simple means to repoduce this problem:

    Makefile
    afunc.C
    main.C
    myclass.C
    myclass.h
    test.script
    test.sample.output

# -------------------------------------------------------------------
# --------    Makefile
CC= g++
LD= ld
MSRCS= main.C myclass.C
MOBJS= $(MSRCS:.C=.o)
LSRCS= afunc.C 
LOBJS= $(LSRCS:.C=.o)
SRCS= $(MSRCS) $(LSRCS)
OBJS= $(MOBJS) $(LOBJS)

.C.o:
	$(CC) -g -c $*.C

all: clean static atest sotest

static: $(OBJS)
	$(CC) -o static $(OBJS)	

alib:	$(LOBJS)
	$(AR) -ru libxx.a $(LOBJS) 

solib:	$(LOBJS)
	$(LD) -G -o libxx.so $(LOBJS) 

atest:	alib $(MOBJS)
	$(CC) -o atest $(MOBJS) -L. -lxx

sotest: solib $(MOBJS)
	$(CC) -o sotest $(MOBJS) -L. -lxx

test:	all
	echo Run the tests illustarted in test.script to see the problem.

clean:
	rm -f $(OBJS) libxx.so static sotest


# -------------------------------------------------------------------
# --------    afunc.C
#include <stdio.h>
#include "myclass.h"

int afunc(int k) {
	try {
		Test_class x;

		printf("afunc: try k:%d\n",k);
		x.Test_func(k);
   	}
	catch( Test_err ) {
		printf("afunc: catch Test_err\n");
		return(1);
	}
	printf("afunc: after catch\n");
	return(0);
}
# -------------------------------------------------------------------
# --------    main.C
#include <stdio.h>
#include <stdlib.h>
#include "myclass.h"

main(int ac, char *av[]){
	int k = 0;
	
	if ( ac > 1 )
		k = atoi(av[1]);

	try {
		printf("main outer try 1\n");
		try {
			printf("main inner try 1\n");
			afunc(k);
		}
		catch( Test_err ) {
			printf("main: inner catch Test_err\n");
		}
		printf("main: after inner catch\n");
	}
	catch( Test_err ) {
		printf("main: outer catch Test_err\n");
	}
	printf("main: after outer catch\n");
}
# -------------------------------------------------------------------
# --------    myclass.C
#include <stdio.h>
#include <stdlib.h>
#include "myclass.h"

Test_err::Test_err(char *amsg) {
			printf("Test_err: message |%s|\n", amsg);
};

Test_class::Test_func(int k) {
	printf("Test_func: try k:%d\n",k);
	if ( k )
		throw Test_err("Test_func: throw");
	return (0);
};
# -------------------------------------------------------------------
# --------    myclass.h
int afunc(int);

class Test_err {
	public:
		Test_err(char*);
};

class Test_class {
	public:
		Test_func(int);
};

# -------------------------------------------------------------------
# --------    test.script
PATH=$PATH:.
export LD_LIBRARY_PATH=$LD_LIBRARAY_PATH:.
date 
echo "static linked objects" >> test.log 2>&1
static 0
static 1 
echo "objects linkjed with libxx.a" >> test.log 2>&1
atest 0 
atest 1 
echo "objects linkjed with libxx.so" >> test.log 2>&1
sotest 0 
sotest 1 
# -------------------------------------------------------------------
# ---------  test sample.output 

Script started on Mon Jan  8 14:48:44 2001
[dmk@acsifw2 sotry]$ PATH=$PATH:.
[dmk@acsifw2 sotry]$ export LD_LIBRARY_PATH=$LD_LIBRARAY_PATH:.
[dmk@acsifw2 sotry]$ date
Mon Jan  8 14:49:09 EST 2001

[dmk@acsifw2 sotry]$ echo "static linked objects"
static linked objects
[dmk@acsifw2 sotry]$ static 0
main outer try 1
main inner try 1
afunc: try k:0
Test_func: try k:0
afunc: after catch
main: after inner catch
main: after outer catch
[dmk@acsifw2 sotry]$ static 1
main outer try 1
main inner try 1
afunc: try k:1
Test_func: try k:1
Test_err: message |Test_func: throw|
afunc: catch Test_err                        <<<---- Expected result - catch in afunc
main: after inner catch
main: after outer catch

[dmk@acsifw2 sotry]$ echo "objects linkjed with libxx.a"
objects linkjed with libxx.a
[dmk@acsifw2 sotry]$ atest 0
main outer try 1
main inner try 1
afunc: try k:0
Test_func: try k:0
afunc: after catch
main: after inner catch
main: after outer catch
[dmk@acsifw2 sotry]$ atest 1
main outer try 1
main inner try 1
afunc: try k:1
Test_func: try k:1
Test_err: message |Test_func: throw|
afunc: catch Test_err                        <<<---- Expected result - catch in afunc
main: after inner catch
main: after outer catch

[dmk@acsifw2 sotry]$ echo "objects linkjed with libxx.so"
objects linkjed with libxx.so
[dmk@acsifw2 sotry]$ sotest 0
main outer try 1
main inner try 1
afunc: try k:0
Test_func: try k:0
afunc: after catch
main: after inner catch
main: after outer catch
[dmk@acsifw2 sotry]$ sotest 1
main outer try 1
main inner try 1
afunc: try k:1
Test_func: try k:1
Test_err: message |Test_func: throw|
Aborted                                      <<<---- BUG: Aborts when afunc in shared library
[dmk@acsifw2 sotry]$ 
Script done on Mon Jan  8 14:50:34 2001


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]