c++-封装的libcurl类在使用vector时出错

问题描述

封装的libcurl类在使用vector时出错

环境:
系统:Windows 8专业版(32位)
IDE:VS 2013
curl库版本:libcurl-7.19.3-win32-ssl-msvc
运行时提示

0x61DE35F7 (curllib.dll) (curltest.exe 中)处有未经处理的异常: 0xC0000005: 读取位置 0xFEEEFF02 时发生访问冲突。

#include "stdafx.h"
#include "curl/curl.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <io.h>

#pragma comment(lib, "curl/libcurl_imp.lib")

#define PROGNAME "curltest"
#define RETRY_TIME 5
#define URL_SIZE 100

using namespace std;

size_t print_callback(char* buffer, size_t size, size_t nitems, void *outstream)
{
    return nitems;
}

size_t write_callback(char* buffer, size_t size, size_t nitems, string &text)
{
size_t sizes = size * nitems;
string temp(buffer, sizes);
text += temp;
return sizes;
}

int progress_callback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
{
printf("%.1f%%t", 100 * (dlnow / dltotal));
cout << "r";
cout.flush();
return 0;
}

class Novel
{
private:
CURL *eh;
CURLcode res;
int http_code;
int retry_time;
public:
int id;
double filesize;
bool is_exist;
char *url = new char[URL_SIZE];
string *text = new string;
Novel(int id)
{
    this->id = id;
    retry_time = RETRY_TIME;
    sprintf_s(url, URL_SIZE, "dl.wenku8.cn/down.php?type=txt&id=%d", id);
    eh = curl_easy_init();
    curl_easy_setopt(eh, CURLOPT_URL, url);
    curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, print_callback);
    curl_easy_setopt(eh, CURLOPT_NOPROGRESS, 1);
    curl_easy_setopt(eh, CURLOPT_PROGRESSFUNCTION, progress_callback);
    curl_easy_setopt(eh, CURLOPT_FOLLOWLOCATION, 1);
    curl_easy_setopt(eh, CURLOPT_HEADER, 1);
    curl_easy_setopt(eh, CURLOPT_NOBODY, 1);
    res = curl_easy_perform(eh);
    curl_easy_getinfo(eh, CURLINFO_HTTP_CODE, &http_code);
    if (http_code == 404)
    {
        is_exist = false;
        filesize = -1;
        return;
    }
    else
        is_exist = true;
    curl_easy_getinfo(eh, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &filesize);
}
int start()
{
    if (is_exist == false)
    {
        cerr << "文件不存在" << endl;
        return 1;
    }
    else
    {
        curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, write_callback);
        curl_easy_setopt(eh, CURLOPT_NOBODY, 0);
        curl_easy_setopt(eh, CURLOPT_HEADER, 0);
        curl_easy_setopt(eh, CURLOPT_NOPROGRESS, 0);
        curl_easy_setopt(eh, CURLOPT_WRITEDATA, text);
        cout << "正在下载 " << id << ".txt" << endl;
        res = curl_easy_perform(eh);
        if (res != CURLE_OK)
        {
            cerr << curl_easy_strerror(res) << endl;
            return -1;
        }
        cout << "下载完成" << endl;
    }
    return 0;
}
~Novel()
{
    curl_easy_cleanup(eh);
    delete[] url;
    delete text;
}
};

int main(int argc, char *argv[])
{
/*
    int retry_time = RETRY_TIME;
    int id = 1;
    vector<Novel> vNovel;
    for (id = 1; retry_time!=0; id++)
    {
        vNovel.push_back(id);
        if (vNovel[id - 1].start())
        {
            retry_time--;
        }
        else
        {
            retry_time = RETRY_TIME;
        }
    }
    getchar();
    vNovel.clear();    // 这句出错
*/
// 上面是出错的地方,下面无错
    Novel test(1);
    test.start();
    getchar();
    return 0;
}
时间: 2024-08-29 14:38:47

c++-封装的libcurl类在使用vector时出错的相关文章

vector定义-定义元素为int数组的vector时出错: vector&amp;amp;lt;int [10]&amp;amp;gt; vec;

问题描述 定义元素为int数组的vector时出错: vector<int [10]> vec; 为什么不能这样定义?请问错在哪里? 正常的定义方式是vector> vec; 但是vector为什么不能直接存放数组呢?非常疑惑,求大神解答- 解决方案 vector<int *> vector<vector<int>> 这两个试试看 解决方案二: 像老曹那样,定义指针 解决方案三: int数组不是模版类型. 你这个需要双层vector vector>

ziparchive-PHP 采用zipArchive类压缩文件文件时出错?

问题描述 PHP 采用zipArchive类压缩文件文件时出错? <?php header("Content-type:text/html;charset=utf-8"); /** Created by PhpStorm. User: Administrator Date: 2015/9/1 Time: 10:24 */ $zip = new ZipArchive(); //首先实例化这个类 if ($zip->open('F:test.zip',ZIPARCHIVE::O

PHP封装的HttpClient类用法实例

         本文实例讲述了PHP封装的HttpClient类.分享给大家供大家参考.具体分析如下:          这是一段php封装的HttpClient类,可实现GET POST Cookie Session等简单的功能.原来做过,这两天重新修改了一下. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

如何使用php实现封装数据库的类文件

 <?php class dbname{ private $localhost; private $root; private $pass; private $db_name; public function __construct($localhost,$root,$pass,$db_name){ $this->localhost=$localhost; $this->root=$root; $this->pass=$pass; $this->db_name=$db_nam

使用libzplay库封装一个音频类

装载请说明原地址,谢谢~~      前两天我已经封装好一个duilib中使用的webkit内核的浏览器控件和一个基于vlc的用于播放视频的视频控件,这两个控件可以分别用在放酷狗播放器的乐库功能和MV功能上,也可以用于其他duilib项目,说起来做仿酷狗程序,但是至今我虽然把仿酷狗的主界面做好了,但是还没有播放音乐的功能,所以今天就再封装一个音频类.    我以前并不怎么使用音频和视频的功能,所以对常用的视频库和视频库不太了解,而我肯定不会使用系统的win32控件或者MFC里面的类,因为多数系统

分页封装实用工具类及其使用方法

分页封装实用工具类及其使用方法 作者: javaboy2012 Email:yanek@163.com qq:    1046011462     package com.yanek.util; import java.util.List; public class PageBean { /** * @param args */ public static void main(String[] args) { } private int currentpage; // 当前页数 private

MySQL数据库学习笔记(十一)----DAO设计模式实现数据库的增删改查(进一步封装JDBC工具类)

[正文] 一.DAO模式简介 DAO即Data Access Object,数据访问接口.数据访问:故名思义就是与数据库打交道.夹在业务逻辑与数据库资源中间. DAO模式实际上是两个模式的组合,即Data Accessor (数据访问者)模式和 Active Domain Object(领域对象)模式.Data Accessor 模式实现了数据访问和业务逻辑的分离:Active Domain Object 模式实现了业务数据的对象化封装. 需要注意的是,DAO设计模式是Java EE中的设计模式

java-如何为已封装的构造类中的数组赋值并打印

问题描述 如何为已封装的构造类中的数组赋值并打印 初学java 望大神赐教 class Leisss{ private String name; private Leisss(String a){ this.name=a; } public void setname(String ss){ this.name=ss; } public String getname(){ return this.name; } static Leisss shuzu[]=new Leisss[3]; public

MySQL数据库学习笔记(十)----JDBC事务处理、封装JDBC工具类

[正文] 首先需要回顾一下上一篇文章中的内容:MySQL数据库学习笔记(九)----JDBC的PreparedStatement接口重构增删改查 一.JDBC事务处理: 我们已经知道,事务的概念即:所有的操作要么同时成功,要么同时失败.在MySQL中提供了Commit.Rollback命令进行事务的提交与回滚.实际上在JDBC中也存在事务处理,如果要想进行事务处理的话,则必须按照以下的步骤完成. JDBC中事务处理的步骤: 1.要取消掉JDBC的自动提交:void setAutoCommit(b