Thread: Best method to alloc memory for string and desalloc.

  1. #61
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    last code: http://pastebin.com/m796dc78a

    I continue execute a program by a loop,
    when next loop is call, the program crash.

    I have eliminated various global variables strings, and tried your tips, but some not function perfectly with my equipment.

  2. #62
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Any chance you could post a few samples of input?

    I would rewrite the serial comms to read from a file.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #63
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    This is a Code of Serial packets, with value weights captureds:

    http://www.4shared.com/file/80304658...edoSample.html

    This is a Log of my Socket Client, with all packets receiveds and send, this log is a loop complete of my program.

    http://www.4shared.com/file/80304999...172161520.html

  4. #64
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    I Suspect a problem with function EscreveDisplay,

    because I make a test,

    I StartProgram, and next connected with my client socket app,
    the my program call a function PortUserStart
    then, I send to pc the number iUserSocket through EnviaPedidoPC
    And my client socket has received only ...........

    The function EscreveDisplay complete a text with ... to c omplete 40 chars.

    Code:
    void PortUserStart(int skt)
    {
    	iUserSocket = skt;
    }
    
    void EnviaPedidoPC (unsigned char *pChar, int pTamanho){
    	
    	unsigned char *cMsgFinal;
    	int iLen;
    	
    	Delay (200);
    	
    	pTamanho += 4;
    	cMsgFinal = malloc (pTamanho);
    	
    	if (cMsgFinal == NULL)
    		VcomSendSocket(iUserSocket, "1-ERRO DE ALOCAÇÃO DE MEMÓRIA", 29);	
    	
    	if (iUserSocket){
    		sprintf(cMsgFinal, "%c%s%c%c", cSTX, pChar, cCR, cLF);	// Formata Pacote
    	
    		iLen = strlen (cMsgFinal);
    		VcomSendSocket(iUserSocket, cMsgFinal, iLen);	
    	}
    	
    	free (cMsgFinal);
    }

  5. #65
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I know I often write a lot in my posts. And some of it may be rubbish. However:
    [quote="Me"]
    You are going to the 52nd element of a 51 element array - not good.

    Quote Originally Posted by You
    Then I Have to declare with unsigned char cDispPacote [52]; ?
    Then Me:
    Yes.

    You appear to not have made this change... It will definitely not help.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #66
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    sorry, i has change this code, but then the display dont show the message,
    i was reported to, at some post back...

    now i has changed again,
    without display
    but send, .......... continues

    Code:
    unsigned char *pMensagem
    
    if (iLenMsg < 40){
    		for(iLenCont=iLenMsg; iLenCont<40; iLenCont++)
    			strcat (pMensagem, ".");
    	}
    I suspect this part, is several to program.
    Last edited by sergioms; 01-12-2009 at 03:36 PM.

  7. #67
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    Changed this:

    Code:
    unsigned char *cFuncao;
    unsigned char *cSubFuncao;
    to this:

    Code:
    unsigned char cFuncao[4];
    unsigned char cSubFuncao[32];
    The program Crash here:

    Code:
    else if (strcmp (cFuncao, "OK") == 0){							// Tratamento de Confirmações
    				substring(&cPacote[2], cSubFuncao, 2);
    				if (strcmp (cSubFuncao, "OP") == 0)	{						// Recebe Confirmação de Ordem de Pesagem
    					EnviaPedidoPC ("OISL", 4);		
    				}
    in some part of this code :S

  8. #68
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't see anything wrong in that code. However, it is possible that it crashes because of something else that went on BEFORE that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed