PDA

View Full Version : C/C++ conundrum


sparkfizt
10-24-2008, 12:12 PM
I know we've got atleast a few professional programmers around here and so I've got a wierd bug I've fought for the last few days that I dont fully understand.

The setup is this:
I've got a C++ dll/so that is meant to be called in order to do some scientific calculations.
I've got a C driver application that is meant to flex and test the dll/so.

When I compile in windows it all functions as I expect, runs to completion and gives me the correct answer.

I then compile that same code through GCC for osx/unix. The driver application is a litttle different because unix shared object library syntax.

When I go to execute the application on osx it proceeds to crash(segfault). After digging at the program for a while became obvious it's due to the heap somehow becoming corrupted.

I cant post the source but I'll give you some pseudo-code.

Doing things in this order causes the crash:

load .so
create .so function pointers
call initialize on the .so returning a linked list

malloc a struct to be handed to the .so

while(not correct answer)
keep calling .so

However when I move the malloc to before instantiating the dll the program functions as it does on windows. This seems to indicate that malloc is stepping on the heap being used by the dll.

Anyone familiar with this kind of issue? Unfortunately most my experience lies with java and this bug has me scratching my head since to me it all seems semantically correct(but obviously I'm wrong somewhere).

Schnoogs
10-24-2008, 12:15 PM
this is why I use C# and Java! ;)

sparkfizt
10-24-2008, 12:24 PM
this is why I use C# and Java! ;)

Pretty much :P I wrote it in C++ because the people I'm writing the code for live in C and I was trying to give them as little grief as possible with integration.

DangerousDaze
10-24-2008, 01:14 PM
Do the two architectures have different word lengths? You might be assuming ints/longs have specific lengths when pointing around your malloc'd area that are ok for Windows but wrong for *nix. The solution is to always use sizeof().

sparkfizt
10-24-2008, 02:03 PM
Do the two architectures have different word lengths? You might be assuming ints/longs have specific lengths when pointing around your malloc'd area that are ok for Windows but wrong for *nix. The solution is to always use sizeof().

I use sizeof for the malloc call, it's just a struct with some primitive data types and a char* pointer.

DangerousDaze
10-24-2008, 02:21 PM
I use sizeof for the malloc call, it's just a struct with some primitive data types and a char* pointer.

I mean when you're using the data as opposed to just allocating it. How about attaching your source and I'll give it a quick once over.

AniAko
10-24-2008, 02:26 PM
this is why I use C# and Java! ;)

That's fine, until you want shared memory space (comes up more often than you'd think) then you're back to good old C/C++ :)


Are you using GCC on both platforms?

sparkfizt
10-24-2008, 02:47 PM
I mean when you're using the data as opposed to just allocating it. How about attaching your source and I'll give it a quick once over.

hmmm interesting looking more deeply into the uses of sizeof this definitely seems like it could be related. Unfortunately I really cant link the code. My code is very similar to this though:

main.c

typedef struct myStruct
{
int myInt;
double myDouble;
char* myPtr;
}myStruct;


myStruct* structPtr;
structPtr = malloc(sizeof(myStruct));
structPtr->myDouble = 42.0;

answer = (*functionPtr)(structPtr);



then on the dll side

double functionPtr(myStruct* structPtr)
{
double data = structPtr->myDouble;
return performCalcs(data);
}

of course on the C++ dll side I'm also using new to create some random temporary objects, which is when you noticed memory got mangled.

I'm using GCC/g++ for the unix/osx side and visual studio for windows.

Schnoogs
10-24-2008, 03:08 PM
That's fine, until you want shared memory space (comes up more often than you'd think) then you're back to good old C/C++ :)

I've been programming at the undergraduate, graduate and professional level for roughly 15 years and I've never had to share memory between processes.

I can think of more compelling reasons to deal with the drudgery of using C or C++ than shared memory.

AniAko
10-24-2008, 03:17 PM
I've been programming at the undergraduate, graduate and professional level for roughly 15 years and I've never had to share memory between processes.

It all matters with "what" you've been programming. :) And true, there are more compelling reasons, but that is one of the few incapabilities of .net/java code

For the record I swear by C# and .Net now a days, unless there's PHP to do. Sweet sweet sweet php :)

DangerousDaze
10-24-2008, 04:35 PM
It's very difficult to suggest anything without the actual code. You can stick it in a txt file and attach it if that helps.

Doctor Setebos
10-24-2008, 04:38 PM
VB FTW. :rolleyes:

(it's the only language I'm allowed to code in here at work, otherwise I would be doing everything in either C# or Java)

sparkfizt
10-24-2008, 05:57 PM
It's very difficult to suggest anything without the actual code. You can stick it in a txt file and attach it if that helps.

yeah I understand it's about impossible to diagnose this stuff without seeing the real code. I cant post it because if I were to post my stuff to the internet I'd get fired... fired bad. :P