发送字符串为什么会出现malloc报错的问题呢?难道和opencv库有关吗

laixbreth 发布于 2017/07/06 09:28
阅读 384
收藏 0

 

picfasong.cpp发送一张图片给picjieshou.cpp,第三次,或者第二次,就会报错误,请帮我分析下吧,谢谢,我真的找不出毛病了,以下是代码,最为奇怪的是,当我删除picjieshou.cpp中第73行的malloc(143);之后,就不会报错了,或者,把picjieshou.cpp中的1024修改为51以下的数字时,就不再报错了,可以无限的发送图片下去。难道malloc使用有什么禁忌

报错信息:

----------------以下为picjieshou.cpp程序运行输出信息-----------------

 

ready:

here !

 

 

 

---------------1---------------

times:1

len_img:1024

len_img:1024

NOW!!!!!!!!!!!!!!!!!!!

0.5

---------------1---------------

 

 

 

 

ready:

here !

picjieshou: malloc.c:2394: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.

 

----------------以上为picjieshou.cpp程序运行输出信息------------------

------------------以下为picfasong.cpp代码-------------------------
 

#include <sys/types.h>
#include <sys/socket.h>                         // 鍖呭惈濂楁帴瀛楀嚱鏁板簱
#include <stdio.h>
#include <netinet/in.h>                         // 鍖呭惈AF_INET鐩稿叧缁撴瀯
#include <arpa/inet.h>                      // 鍖呭惈AF_INET鐩稿叧鎿嶄綔鐨勫嚱鏁

#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h> #include <sys/shm.h>
#include <pthread.h>
#include <highgui.h>
#include <iostream>

#define MYPORT  6666
#define BUFFER_SIZE 1024
using namespace std;
int len_imgdata;
typedef struct
{
    int ab;
    int num[1000000];
}Node;
typedef struct MyIplImage
{
    int  nSize;             /**< sizeof(IplImage) */
    int  ID;                /**< version (=0)*/
    int  nChannels;         /**< Most of OpenCV functions support 1,2,3 or 4 channels */
    int  alphaChannel;      /**< Ignored by OpenCV */
    int  depth;             /**< Pixel depth in bits: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16S,
                               IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F are supported.  */
    char colorModel[4];     /**< Ignored by OpenCV */
    char channelSeq[4];     /**< ditto */
    int  dataOrder;         /**< 0 - interleaved color channels, 1 - separate color channels.
                               cvCreateImage can only create interleaved images */
    int  origin;            /**< 0 - top-left origin,
                               1 - bottom-left origin (Windows bitmaps style).  */
    int  align;             /**< Alignment of image rows (4 or 8).
                               OpenCV ignores it and uses widthStep instead.    */
    int  width;             /**< Image width in pixels.                           */
    int  height;            /**< Image height in pixels.                          */
    struct _IplROI *roi;    /**< Image ROI. If NULL, the whole image is selected. */
    struct _IplImage *maskROI;      /**< Must be NULL. */
    void  *imageId;                 /**< "           " */
    struct _IplTileInfo *tileInfo;  /**< "           " */
    int  imageSize;         /**< Image data size in bytes
                               (==image->height*image->widthStep
                               in case of interleaved data)*/
    string imageData;        /**< Pointer to aligned image data.         */
    int  widthStep;         /**< Size of aligned image row in bytes.    */
    int  BorderMode[4];     /**< Ignored by OpenCV.                     */
    int  BorderConst[4];    /**< Ditto.                                 */
    string imageDataOrigin;  /**< Pointer to very origin of image data
                               (not necessarily aligned) -
                               needed for correct deallocation */

}
MyIplImage;

int main( int argc, char **argv )
{
    //sockfd
    int sock_cli = socket(AF_INET,SOCK_STREAM, 0);

    struct sockaddr_in servaddr;
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
       servaddr.sin_port = htons(MYPORT);
    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");

    if (connect(sock_cli, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
    {
        perror("connect");
        exit(1);
    }

    Node *myNode=(Node*)malloc(sizeof(Node));
    myNode->ab=123;
    myNode->num[0]=110;
    myNode->num[999999]=99;
    IplImage *img=(IplImage*)malloc(sizeof(IplImage));
    IplImage *img_yuanshi = cvLoadImage( argv[1] );
    img->nSize=img_yuanshi->nSize;
    img->ID=img_yuanshi->ID;
    img->nChannels=img_yuanshi->nChannels;
    img->alphaChannel=img_yuanshi->alphaChannel;
    strcpy(img->colorModel,img_yuanshi->colorModel);
    img->dataOrder=img_yuanshi->dataOrder;
    img->depth=img_yuanshi->depth;
    img->origin=img_yuanshi->origin;
    img->align=img_yuanshi->align;
    img->width=img_yuanshi->width;
    img->height=img_yuanshi->height;
    img->imageSize=img_yuanshi->imageSize;
//    strcpy(img->imageData,img_yuanshi->imageData);
    img->imageData=img_yuanshi->imageData;
    img->widthStep=img_yuanshi->widthStep;


//    img->BorderMode=img_yuanshi->BorderMode;

    img->BorderMode[0]=img_yuanshi->BorderMode[0];
    img->BorderMode[1]=img_yuanshi->BorderMode[1];
    img->BorderMode[2]=img_yuanshi->BorderMode[2];
    img->BorderMode[3]=img_yuanshi->BorderMode[3];

    img->BorderConst[0]=img_yuanshi->BorderConst[0];
    img->BorderConst[1]=img_yuanshi->BorderConst[1];
    img->BorderConst[2]=img_yuanshi->BorderConst[2];
    img->BorderConst[3]=img_yuanshi->BorderConst[3];

//    img->imageDataOrigin=img_yuanshi->imageDataOrigin;

 

 

 

    cout<<"img_yuanshi->nSize:"<<img_yuanshi->nSize<<endl;
    cout<<"img_yuanshi->ID:"<<img_yuanshi->ID<<endl;
    cout<<"img_yuanshi->nChannels:"<<img_yuanshi->nChannels<<endl;
    cout<<"img_yuanshi->depth:"<<img_yuanshi->depth<<endl;
    cout<<"img_yuanshi->colorModel:"<<img_yuanshi->colorModel<<endl;
    cout<<"img_yuanshi->alphaChannel:"<<img_yuanshi->alphaChannel<<endl;
    cout<<"img_yuanshi->dataOrder:"<<img_yuanshi->dataOrder<<endl;
    cout<<"img_yuanshi->origin:"<<img_yuanshi->origin<<endl;
    cout<<"img_yuanshi->width:"<<img_yuanshi->width<<endl;
    cout<<"img_yuanshi->height:"<<img_yuanshi->height<<endl;
    cout<<"img_yuanshi->imageSize:"<<img_yuanshi->imageSize<<endl;
//    cout<<"img_yuanshi->strlen(imageData):"<<strlen(img_yuanshi->imageData)<<endl;
    cout<<"img_yuanshi->strlen(imageData):"<<strlen(img->imageData)<<endl;
    cout<<"img_yuanshi->widthStep:"<<img_yuanshi->widthStep<<endl;
    cout<<"img_yuanshi->BorderMode:"<<img_yuanshi->BorderMode<<endl;
    cout<<"img_yuanshi->BorderConst:"<<img_yuanshi->BorderConst<<endl;
//    cout<<"img_yuanshi->imageDataOrigin:"<<img_yuanshi->imageDataOrigin<<endl;
//    cout<<"img->imageDataOrigin:"<<img->imageDataOrigin<<endl;


    int needSend=sizeof(Node);
    char *buffer=(char*)malloc(needSend);
    memcpy(buffer,myNode,needSend);
    
    int needSend_img=sizeof(IplImage);
    char *buffer_img=(char*)malloc(needSend_img);
    memcpy(buffer_img,img_yuanshi,needSend_img);

    
    IplImage *img_huilai=(IplImage*)malloc(sizeof(IplImage));
    memcpy(img_huilai,buffer_img,needSend_img);
    cout<<"fasongqian_length:"<<strlen(buffer_img)<<endl;
    cout<<"img_huilai->imageData:"<<strlen(img_huilai->imageData)<<endl;

    cout<<"OK"<<endl;
    int pos=0;
    int len=0;

    int pos_img=0;
    int len_img=0;
//    while(pos < needSend)
//    {
//        len=send(sock_cli, buffer+pos, BUFFER_SIZE,0);
//        if(len <= 0)
//        {
//            perror("ERRPR");
//            break;
//        }
//        pos+=len;
//    }
//    free(buffer);
//    free(myNode);
    cout<<"all_length:"<<strlen(buffer_img)<<endl;

    while(pos_img < needSend_img)
    {
        len_img=send(sock_cli, buffer_img+pos_img, BUFFER_SIZE,0);
        if(len_img <= 0)
        {
            perror("ERRPR");
            break;
        }
        pos_img+=len_img;
    }
    free(buffer_img);
    free(img);

    len_imgdata=send(sock_cli, img->imageData, img->imageSize,0);
//    close(sock_cli);
    printf("Send over!!!\n");
    return 0;
}

 

------------------以上为picfasong.cpp代码-------------------------
 

 

------------------以下为picjieshou.cpp代码-------------------------
 

 

picjieshou.cpp代码

#include<netinet/in.h>
#include <unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<highgui.h>
#include<iostream>

#define HELLO_WORLD_SERVER_PORT    6666
#define LENGTH_OF_LISTEN_QUEUE     20
#define BUFFER_SIZE                1024
using namespace std;
int changdu=0,len_imageData=0,needRecv_imageData=284995,pos_imageData=0,times=0;

 

 

int main(int argc, char **argv)
{
    int jishu=0;
    struct sockaddr_in   server_addr;
    bzero(&server_addr, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htons(INADDR_ANY);
    server_addr.sin_port = htons(HELLO_WORLD_SERVER_PORT);
    int server_socket = socket(PF_INET, SOCK_STREAM, 0);
    if (server_socket < 0)
    {
        printf("Create Socket Failed!\n");
        exit(1);
    }

    if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)))
    {
        printf("Server Bind Port: %d Failed!\n", HELLO_WORLD_SERVER_PORT);
        exit(1);
    }

    if (listen(server_socket, LENGTH_OF_LISTEN_QUEUE))
    {
        printf("Server Listen Failed!\n");
        exit(1);
    }

    for(int i=0;i<50;i++)
    {

 


    
        struct sockaddr_in client_addr;
        socklen_t          length = sizeof(client_addr);

        int new_server_socket = accept(server_socket, (struct sockaddr*)&client_addr, &length);
        if (new_server_socket < 0)
        {
            printf("Server Accept Failed!\n");
            break;
        }

    char str1[10], str2[10];
    cout<<"ready:"<<endl;
    cout<<"here !"<<endl;
    

        int needRecv_img=sizeof(IplImage);
        int needRecv_img2=sizeof(IplImage);
        
        malloc(143);
        char *buffer_img=(char*)malloc(144);
    cout<<endl;
    cout<<endl;
    cout<<endl;
    jishu++;
    cout<<"---------------"<<jishu<<"---------------"<<endl;
        int pos_img=0;
        int len_img;
        


    while(pos_img < needRecv_img)
    {
    
    times++;    
    cout<<"times:"<<times<<endl;

        len_img = recv(new_server_socket, buffer_img, 1024, 0);
    cout<<"len_img:"<<len_img<<endl;
        len_img = 1024;
    cout<<"len_img:"<<len_img<<endl;
        if (len_img < 0)
        {
        printf("Server Recieve Data Failed!\n");
        break;
        cout<<"here !-------------1"<<endl;
        }
        pos_img+=len_img;
    
    }
//    times=0;

    cout<<"NOW!!!!!!!!!!!!!!!!!!!"<<endl;
    
    cout<<"0.5"<<endl;
    cout<<"---------------"<<jishu<<"---------------"<<endl;
    cout<<endl;
    cout<<endl;
    cout<<endl;
    cout<<endl;
    

 


        free(buffer_img);

 


    }

}

------------------以上为picjieshou.cpp代码-------------------------
 

 

 

加载中
返回顶部
顶部