Search code examples
c++pointersdlldelete-operator

external dll, static function and char* relation in C++


I'm trying to figure out, how to code properly this scenario:

Inside a C++ application, I'm calling external DLL via LoadLibrary, then:

char * _input = new {... is  created here with some data}
char *aallocchrar =new char[10000];
fnHello(_input,aallocchrar);

fnHellois static function in dll. It returns void, but fills aallocchrar pointer with some data.

After this, I do

delete[] _aallocchrar;

and it always throws memory error. Inside static function, at the very end, I've tried using:

return 0;
exit(1);

exit(1) crashes even on function execution.

How to do it flawlessly? Pass pointers, fill with data, exit dll function. Why that evil dll changes my aallochar function, that I can't later delete[] it? How should I end static dll function properly - with exit? - so it should release my pointers away?

Inside dll function, all I do with _aallocchrar* is:

strcat (str, ";\n\n");

I'm not re-assigning it or somethin g like that. Function difinition is:

void __declspec(dllexport) __cdecl runMainRoutine(char* _inputString, char* finalResult)

EDIT1: This function works with passed char array:

char* eqn_output(pPLA PLA, char* str)
{

//
register pcube p, last;
register int i, var, col, len;
int x;
bool firstand, firstor;

if (cube.output == -1)
fatal("Cannot have no-output function for EQNTOTT output mode");
if (cube.num_mv_vars != 1)
fatal("Must have binary-valued function for EQNTOTT output mode");
makeup_labels(PLA);

/* Write a single equation for each output */
for(i = 0; i < cube.part_size[cube.output]; i++) 
{
strcat (str, OUTLABEL(i));
strcat (str, " = ");
//printf("%s = ", OUTLABEL(i));
col = strlen(OUTLABEL(i)) + 3;
firstor = TRUE;

/* Write product terms for each cube in this output */
foreach_set(PLA->F, last, p)
    if (is_in_set(p, i + cube.first_part[cube.output])) {
    if (firstor)
    {
        strcat (str, "(");
        col += 1;           
       // printf("("), col += 1;
    }
    else
    {
    strcat (str, " | (");   
    col += 4;
    //printf(" | ("), col += 4;

    }
    firstor = FALSE;
    firstand = TRUE;

    /* print out a product term */
    for(var = 0; var < cube.num_binary_vars; var++)
        if ((x=GETINPUT(p, var)) != DASH) {
        len = strlen(INLABEL(var));
        if (col+len > 72)
        {
            strcat (str, "\n    "); 
            col = 4;
            //printf("\n    "), col = 4;
        }
        if (! firstand)
        {
            strcat (str, "&");
            col += 1;
            //printf("&"), col += 1;
        }
        firstand = FALSE;
        if (x == ZERO)
        {
            strcat (str, "!");
            col += 1;
            //printf("!"), col += 1;
        }
        //printf("%s", INLABEL(var)), col += len;
        strcat (str, INLABEL(var));
        col += len;
        }
    //printf(")"), col += 1;

    strcat (str, ")");
    col += 1;
    }
strcat (str, ";\n\n");
//printf(";\n\n");
}

Solution

  • It sounds like you're doing an "strcat()" on an uninitialized string.

    Methinks that might cause a segmentation violation, eh?

    Q: What happens if you substitute "strcpy()"? Do you still experience the problem?