Access violation al instalar un nuevo componente en Kylix
Hola a todos,
resulta que tengo unos componentes que he creado en C++Builder y me funcionan perfectamente, pero los he pasado a Kylix y me llega a compilar y linkar correctamente, pero al intentar instalarlo me da un error del tipo "Access violation at address xxxxxx, accessing address yyyyy".
He llegado a depurar el error hasta llegar a una funcion que hace uso de una clase creada por mi, pero esa funci贸n no se llama hasta que no se usa el objeto, por lo que no entiendo por que da el error ah铆, la funci贸n en cuesti贸n es "LoadData", si quito las l铆neas que hacen referencia a los objetos del tipo MyResult o MyConnecction, no da ning煤n problema.
Espero que alguien me pueda echar una mano, desde ya muchas gracias.
A continuaci贸n pongo el codigo de dicho componente:
fichero .cpp:
//---------------------------------------------------------------------------
#include <clx.h>
#pragma hdrstop
#include "QMySqlListBox.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
static inline void ValidCtrCheck(TMySqlListBox *)
{
new TMySqlListBox(NULL);
}
//---------------------------------------------------------------------------
__fastcall TMySqlListBox::TMySqlListBox(TComponent* Owner)
: TListBox(Owner)
{
Parent = (TWinControl*)Owner;
MyCon = NULL;
Sorted = true;
Font->Name = "Courier";
_Selected = false;
_SelectedKeyValue = "";
FicheroCargado = false;
}
//---------------------------------------------------------------------------
void __fastcall TMySqlListBox::Loaded(void)
{
TListBox::Loaded();
//if (MyCon!=NULL && FicheroCargado) LoadData();
}
//---------------------------------------------------------------------------
__fastcall TMySqlListBox::~TMySqlListBox(void)
{
// TListBox::~TListBox(void);
}
//---------------------------------------------------------------------------
void TMySqlListBox::CargarFichero(AnsiString NombreFichero)
{
char str[256];
AnsiString Fichero("mlbfiles/" + NombreFichero + ".mlb");
ifstream fin(Fichero.c_str());
if (fin.is_open()){
string Linea;
NumMascaras = 0;
while ( !fin.eof() ){
fin.getline(str,256);
Linea = str;
if (Linea.substr(0,6) == "SELECT"){
SentSQL = Linea.c_str();
}
if (Linea.substr(0,2)=="--"){
SentSQL += AnsiString(Linea.substr(2).c_str());
}
if (Linea.substr(0,8)=="MASCARAS"){
string Mascs = Linea.substr(9);
unsigned p1=0, p2 = Mascs.find('|');
while (p2<Mascs.length()){
Mascaras[NumMascaras++] = Mascs.substr(p1,p2-p1).c_str();
p1 = p2+1;
p2 = Mascs.find('|',p1);
}
p2 = Mascs.length();
Mascaras[NumMascaras++] = Mascs.substr(p1,p2-p1).c_str();
}
if (Linea.substr(0,14)=="CAMPO_DEVOLVER"){
CampoDevolver = Linea.substr(15).c_str();
}
}
FicheroCargado = true;
}else{
ShowMessage("Fichero '"+Fichero+"' No Encontrado.");
FicheroCargado = false;
}
}
//---------------------------------------------------------------------------
void TMySqlListBox::LoadData(void)
{
try
{
TMyResult Res;
/*MyCon->ExecuteQuery(SentSQL, &Res);
if (Res.NumRows>0){
TMyRow MyRow;*/
Items->Clear();
// for ( Res.NextRow(&MyRow); !MyRow.Empty(); Res.NextRow(&MyRow) ) {
AnsiString Cad="";
// for (int col=0; col < NumMascaras; col++)
// Cad += Formatear(MyRow[col].c_str(),Mascaras[col].c_str()).c_str() + AnsiString(" ");
Cad = "|";//+MyRow[CampoDevolver];
Items->Add(Cad);
// }
//}
}
catch (MyException er) // handle any connection or query errors that may come up
{
ShowMessage("Error: " + AnsiString(er.Error) );
}
}
//---------------------------------------------------------------------------
string TMySqlListBox::Formatear(string valor, string Mascara)
{
string val;
unsigned p;
switch (Mascara[0]) {
case '#':
case '0':
p = valor.find(".");
if (valor.length()==0) valor = "0";
if (p<valor.length()) valor.replace(p,1,",");
val = AnsiString(FormatFloat(Mascara.c_str(), StrToFloat(valor.c_str()) ) ).c_str();
if (Mascara[0]=='#')
val = val.insert((unsigned)0,Mascara.length()-val.length(),' ');
break;
case '!':
if (valor.length()==10)
val = valor.substr(8,2)+"-"+valor.substr(5,2)+"-"+valor.substr(2,2);
else
val = valor;
break;
case '?':
if (valor.find(":")>valor.length())
valor = SecToTime(Round(StrToFloat(valor.c_str()),0));
val = valor.insert((unsigned)0,Mascara.length()-valor.length(),' ');
//val = valor;
break;
case '@':
val = valor.substr(0,Mascara.length());
val = val.append(Mascara.length()-val.length(),' ');
break;
case '$':
val = valor.substr(0,Mascara.length());
val = val.insert((unsigned)0,Mascara.length()-val.length(),' ');
break;
}
return val;
}
//---------------------------------------------------------------------------
void __fastcall TMySqlListBox::SetDescFile(AnsiString Value)
{
_DescFile = Value;
if (_DescFile.Length()>0){
CargarFichero(_DescFile);
if (FicheroCargado){
unsigned Ancho = 0;
for (int i =0 ; i<NumMascaras; i++)
Ancho += Mascaras[i].Length()+1;
Width = (Ancho+2)*8;
//if (_MyCon!=NULL) LoadData();
}
}
}
//---------------------------------------------------------------------------
void __fastcall TMySqlListBox::Select(bool selected)
{
_Selected = selected;
if (_Selected){
AnsiString sel = Items->Strings[ItemIndex];
int pos = sel.Pos('|');
_SelectedKeyValue = sel.SubString(pos+1,100);
}
if (FOnSelect != NULL) FOnSelect(this);
}
//---------------------------------------------------------------------------
void __fastcall TMySqlListBox::KeyPress(char &Key)
{
TListBox::KeyPress(Key);
if (Key==13) Select(true);
if (Key==27) Select(false);
}
//---------------------------------------------------------------------------
void __fastcall TMySqlListBox::DblClick()
{
Select(true);
}
//---------------------------------------------------------------------------
void __fastcall TMySqlListBox::DoExit()
{
Select(false);
}
//---------------------------------------------------------------------------
void __fastcall TMySqlListBox::SetMyCon(TMyConnection* Value)
{
_MyCon = Value;
//if (_MyCon!=NULL && FicheroCargado) LoadData();
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
namespace Qmysqllistbox
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TMySqlListBox)};
RegisterComponents("Samples", classes, 0);
}
}
//---------------------------------------------------------------------------
fichero .h
//---------------------------------------------------------------------------
#ifndef QMySqlListBoxH
#define QMySqlListBoxH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Classes.hpp>
#include <QControls.hpp>
#include <QStdCtrls.hpp>
#include <fstream.h>
#include <MyTimes.h>
#include <MyMath.h>
#include <MyConnect.h>
using namespace std;
//---------------------------------------------------------------------------
class PACKAGE TMySqlListBox : public TListBox
{
private:
TNotifyEvent FOnSelect;
DYNAMIC void __fastcall KeyPress(char &Key);
DYNAMIC void __fastcall DblClick(void);
bool _Selected;
AnsiString _SelectedKeyValue;
DYNAMIC void __fastcall DoExit(void);
AnsiString _DescFile;
void __fastcall SetDescFile(AnsiString Value);
TMyConnection* _MyCon;
void __fastcall SetMyCon(TMyConnection* Value);
void __fastcall Loaded(void);
void CargarFichero(AnsiString NombreFichero);
bool FicheroCargado;
void LoadData(void);
string Formatear(string valor, string Mascara);
AnsiString SentSQL;
AnsiString Mascaras[20];
int NumMascaras;
AnsiString CampoDevolver;
protected:
virtual void __fastcall Select(bool selected);
public:
__fastcall virtual TMySqlListBox(TComponent* Owner);
__fastcall virtual ~TMySqlListBox();
__property TMyConnection* MyCon = {read=_MyCon, write=SetMyCon, default=NULL};
__property bool Selected = {read=_Selected};
__property AnsiString SelectedKeyValue = {read=_SelectedKeyValue};
__published:
__property TNotifyEvent OnSelect = {read=FOnSelect, write=FOnSelect};
__property AnsiString DescFile = {read=_DescFile, write=SetDescFile};
};
//---------------------------------------------------------------------------
#endif
resulta que tengo unos componentes que he creado en C++Builder y me funcionan perfectamente, pero los he pasado a Kylix y me llega a compilar y linkar correctamente, pero al intentar instalarlo me da un error del tipo "Access violation at address xxxxxx, accessing address yyyyy".
He llegado a depurar el error hasta llegar a una funcion que hace uso de una clase creada por mi, pero esa funci贸n no se llama hasta que no se usa el objeto, por lo que no entiendo por que da el error ah铆, la funci贸n en cuesti贸n es "LoadData", si quito las l铆neas que hacen referencia a los objetos del tipo MyResult o MyConnecction, no da ning煤n problema.
Espero que alguien me pueda echar una mano, desde ya muchas gracias.
A continuaci贸n pongo el codigo de dicho componente:
fichero .cpp:
//---------------------------------------------------------------------------
#include <clx.h>
#pragma hdrstop
#include "QMySqlListBox.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
static inline void ValidCtrCheck(TMySqlListBox *)
{
new TMySqlListBox(NULL);
}
//---------------------------------------------------------------------------
__fastcall TMySqlListBox::TMySqlListBox(TComponent* Owner)
: TListBox(Owner)
{
Parent = (TWinControl*)Owner;
MyCon = NULL;
Sorted = true;
Font->Name = "Courier";
_Selected = false;
_SelectedKeyValue = "";
FicheroCargado = false;
}
//---------------------------------------------------------------------------
void __fastcall TMySqlListBox::Loaded(void)
{
TListBox::Loaded();
//if (MyCon!=NULL && FicheroCargado) LoadData();
}
//---------------------------------------------------------------------------
__fastcall TMySqlListBox::~TMySqlListBox(void)
{
// TListBox::~TListBox(void);
}
//---------------------------------------------------------------------------
void TMySqlListBox::CargarFichero(AnsiString NombreFichero)
{
char str[256];
AnsiString Fichero("mlbfiles/" + NombreFichero + ".mlb");
ifstream fin(Fichero.c_str());
if (fin.is_open()){
string Linea;
NumMascaras = 0;
while ( !fin.eof() ){
fin.getline(str,256);
Linea = str;
if (Linea.substr(0,6) == "SELECT"){
SentSQL = Linea.c_str();
}
if (Linea.substr(0,2)=="--"){
SentSQL += AnsiString(Linea.substr(2).c_str());
}
if (Linea.substr(0,8)=="MASCARAS"){
string Mascs = Linea.substr(9);
unsigned p1=0, p2 = Mascs.find('|');
while (p2<Mascs.length()){
Mascaras[NumMascaras++] = Mascs.substr(p1,p2-p1).c_str();
p1 = p2+1;
p2 = Mascs.find('|',p1);
}
p2 = Mascs.length();
Mascaras[NumMascaras++] = Mascs.substr(p1,p2-p1).c_str();
}
if (Linea.substr(0,14)=="CAMPO_DEVOLVER"){
CampoDevolver = Linea.substr(15).c_str();
}
}
FicheroCargado = true;
}else{
ShowMessage("Fichero '"+Fichero+"' No Encontrado.");
FicheroCargado = false;
}
}
//---------------------------------------------------------------------------
void TMySqlListBox::LoadData(void)
{
try
{
TMyResult Res;
/*MyCon->ExecuteQuery(SentSQL, &Res);
if (Res.NumRows>0){
TMyRow MyRow;*/
Items->Clear();
// for ( Res.NextRow(&MyRow); !MyRow.Empty(); Res.NextRow(&MyRow) ) {
AnsiString Cad="";
// for (int col=0; col < NumMascaras; col++)
// Cad += Formatear(MyRow[col].c_str(),Mascaras[col].c_str()).c_str() + AnsiString(" ");
Cad = "|";//+MyRow[CampoDevolver];
Items->Add(Cad);
// }
//}
}
catch (MyException er) // handle any connection or query errors that may come up
{
ShowMessage("Error: " + AnsiString(er.Error) );
}
}
//---------------------------------------------------------------------------
string TMySqlListBox::Formatear(string valor, string Mascara)
{
string val;
unsigned p;
switch (Mascara[0]) {
case '#':
case '0':
p = valor.find(".");
if (valor.length()==0) valor = "0";
if (p<valor.length()) valor.replace(p,1,",");
val = AnsiString(FormatFloat(Mascara.c_str(), StrToFloat(valor.c_str()) ) ).c_str();
if (Mascara[0]=='#')
val = val.insert((unsigned)0,Mascara.length()-val.length(),' ');
break;
case '!':
if (valor.length()==10)
val = valor.substr(8,2)+"-"+valor.substr(5,2)+"-"+valor.substr(2,2);
else
val = valor;
break;
case '?':
if (valor.find(":")>valor.length())
valor = SecToTime(Round(StrToFloat(valor.c_str()),0));
val = valor.insert((unsigned)0,Mascara.length()-valor.length(),' ');
//val = valor;
break;
case '@':
val = valor.substr(0,Mascara.length());
val = val.append(Mascara.length()-val.length(),' ');
break;
case '$':
val = valor.substr(0,Mascara.length());
val = val.insert((unsigned)0,Mascara.length()-val.length(),' ');
break;
}
return val;
}
//---------------------------------------------------------------------------
void __fastcall TMySqlListBox::SetDescFile(AnsiString Value)
{
_DescFile = Value;
if (_DescFile.Length()>0){
CargarFichero(_DescFile);
if (FicheroCargado){
unsigned Ancho = 0;
for (int i =0 ; i<NumMascaras; i++)
Ancho += Mascaras[i].Length()+1;
Width = (Ancho+2)*8;
//if (_MyCon!=NULL) LoadData();
}
}
}
//---------------------------------------------------------------------------
void __fastcall TMySqlListBox::Select(bool selected)
{
_Selected = selected;
if (_Selected){
AnsiString sel = Items->Strings[ItemIndex];
int pos = sel.Pos('|');
_SelectedKeyValue = sel.SubString(pos+1,100);
}
if (FOnSelect != NULL) FOnSelect(this);
}
//---------------------------------------------------------------------------
void __fastcall TMySqlListBox::KeyPress(char &Key)
{
TListBox::KeyPress(Key);
if (Key==13) Select(true);
if (Key==27) Select(false);
}
//---------------------------------------------------------------------------
void __fastcall TMySqlListBox::DblClick()
{
Select(true);
}
//---------------------------------------------------------------------------
void __fastcall TMySqlListBox::DoExit()
{
Select(false);
}
//---------------------------------------------------------------------------
void __fastcall TMySqlListBox::SetMyCon(TMyConnection* Value)
{
_MyCon = Value;
//if (_MyCon!=NULL && FicheroCargado) LoadData();
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
namespace Qmysqllistbox
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TMySqlListBox)};
RegisterComponents("Samples", classes, 0);
}
}
//---------------------------------------------------------------------------
fichero .h
//---------------------------------------------------------------------------
#ifndef QMySqlListBoxH
#define QMySqlListBoxH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Classes.hpp>
#include <QControls.hpp>
#include <QStdCtrls.hpp>
#include <fstream.h>
#include <MyTimes.h>
#include <MyMath.h>
#include <MyConnect.h>
using namespace std;
//---------------------------------------------------------------------------
class PACKAGE TMySqlListBox : public TListBox
{
private:
TNotifyEvent FOnSelect;
DYNAMIC void __fastcall KeyPress(char &Key);
DYNAMIC void __fastcall DblClick(void);
bool _Selected;
AnsiString _SelectedKeyValue;
DYNAMIC void __fastcall DoExit(void);
AnsiString _DescFile;
void __fastcall SetDescFile(AnsiString Value);
TMyConnection* _MyCon;
void __fastcall SetMyCon(TMyConnection* Value);
void __fastcall Loaded(void);
void CargarFichero(AnsiString NombreFichero);
bool FicheroCargado;
void LoadData(void);
string Formatear(string valor, string Mascara);
AnsiString SentSQL;
AnsiString Mascaras[20];
int NumMascaras;
AnsiString CampoDevolver;
protected:
virtual void __fastcall Select(bool selected);
public:
__fastcall virtual TMySqlListBox(TComponent* Owner);
__fastcall virtual ~TMySqlListBox();
__property TMyConnection* MyCon = {read=_MyCon, write=SetMyCon, default=NULL};
__property bool Selected = {read=_Selected};
__property AnsiString SelectedKeyValue = {read=_SelectedKeyValue};
__published:
__property TNotifyEvent OnSelect = {read=FOnSelect, write=FOnSelect};
__property AnsiString DescFile = {read=_DescFile, write=SetDescFile};
};
//---------------------------------------------------------------------------
#endif