C#呼び出しC++パッケージ(続き)

4327 ワード

こちらは引き続きそのtxtファイルを読む頻度が増加する問題について、こちらのC++パッケージは引き続き改善した後にシステムが安定したことに気づいて、johnのたゆまぬ努力と改善の精神に感謝して、自分がすでに生活に多くの専門の精神を磨かれたことに気づいて、こちらは先にC言語のパッケージコードに行きます:
</pre><pre name="code" class="cpp">
// OptionsPlay.SharedFile.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include <stdio.h>
#include <sys\timeb.h> 
#include <stdlib.h>
#include<windows.h>
//#define LINENUM 174
#define LINESIZE 428
#define TESTTIME 100
#define _CRT_SECURE_NO_DEPRECATE

extern "C"_declspec(dllexport) int ReadSharedFolder(char *p, const char*targetFile, int lineNum);

void resetLoc(int *loc)
{
	*loc = 0;
}

int ReadSharedFolder(char *p, const char*targetFile, int lineNum){
	FILE *fp = fopen(targetFile, "r");
	if (fp == NULL)
	{
		perror("Error opeing file");
		return -1;
	}

	// manual initial one-dimension array p
	for (int i = 0; i < lineNum * (LINESIZE - 1); i++){
		p[i] = '\0';
	}

	// initialize two-dimension array quotation
	char **quotation;
	quotation = new char*[lineNum];
	for (int j = 0; j < lineNum; j++){
		quotation[j] = new char[LINESIZE];
	}
	int loc = 0;

	while (fgets(quotation[loc], LINESIZE, fp) != NULL)
	{
		loc++;
	}
	for (int i = 1; i < loc - 1; i++){
		for (int j = 0; j < (LINESIZE - 1); j++){
			p[(i - 1) * (LINESIZE - 1) + j] = quotation[i][j];
		}
	}
	fclose(fp);
	//resetLoc(&loc);
	// free array quotation
	for (int i = 0; i < lineNum; i++){
		delete[] quotation[i];
		quotation[i] = NULL;
	}
	delete[] quotation;

	return 0;
}




まず、このコードは/0を満たすのに重要です.そうしないと、/r/nなどのバッファの解放されない乱符号が存在します.
for (int i = 0; i < lineNum * (LINESIZE - 1); i++){
		p[i] = '\0';
	}

次に、quotationのdeleteもメモリの漏洩を発生させないとともに、野ポインタを回避します.
for (int i = 0; i < lineNum; i++){
		delete[] quotation[i];
		quotation[i] = NULL;
	}
	delete[] quotation;

同時に呼び出す部分もいくつか調整しました.主にMarshalです.FreeHGlobalはこのスタックを解析し、throwを使用して万一このcのパッケージに異常があった場合、サービスを異常にしないようにします.
[DllImport("OptionsPlay.SharedFile.dll", EntryPoint = "ReadSharedFolder", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        public static extern int ReadSharedFolder(IntPtr p, [MarshalAs(UnmanagedType.LPStr)] string targetFile, int lineNum);
        public static byte[] GetData(string folder, string fileName, int lineNum, int filetypeFlag)
        {
            IntPtr p = System.IntPtr.Zero;
            try
            {
                if (filetypeFlag == 1)
                {
                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();
                    p = Marshal.AllocHGlobal(sizeof(char) * lineNum * lineSize);
                    if (ReadSharedFolder(p, fileName, lineNum) == -1)
                    {
                        Logger.Debug(string.Format("Error opening file ", fileName, stopwatch.ElapsedMilliseconds));
                        throw new System.IO.IOException("error opening txt file");
                    }
                    byte[] b = new byte[lineNum * lineSize];
                    Marshal.Copy(p, b, 0, lineNum * lineSize);
                    stopwatch.Stop();
                    Logger.Debug(string.Format("File {0} Copied in {1}ms", fileName, stopwatch.ElapsedMilliseconds));
                    return b;
                }
                else
                {
                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();


                    string path = Path.Combine(folder, fileName);
                    byte[] result = File.ReadAllBytes(path);
                    stopwatch.Stop();


                    Logger.Debug(string.Format("File {0} Copied in {1}ms", fileName, stopwatch.ElapsedMilliseconds));
                    return result;
                }
            }
            catch (Exception ex)
            {
                Logger.Error("FromLocalDrive GetData Error", ex);
                throw;
            }
            finally
            {
                Marshal.FreeHGlobal(p);
            }
        }