Suppress memory leak when using Kernel_Utils::decode()

+ some code cleanup
This commit is contained in:
eap 2021-08-05 15:53:09 +03:00
parent f016bdf914
commit 6b5dcfe000
6 changed files with 114 additions and 118 deletions

View File

@ -41,9 +41,6 @@ class LightApp_SelectionMgr;
/*! /*!
* The GeomSelectionTools class gives high level tools to select Geom (and other objects) * The GeomSelectionTools class gives high level tools to select Geom (and other objects)
* A specific attention has been given to analyze selected GEOM objects. * A specific attention has been given to analyze selected GEOM objects.
*
* @param myStudy This class is specific to the study !
*
*/ */
class PLUGINUTILS_EXPORT GeomSelectionTools class PLUGINUTILS_EXPORT GeomSelectionTools

View File

@ -52,7 +52,8 @@ static std::string removeFile(std::string fileName, int& notOk)
return errStr; return errStr;
} }
std::string MG_ADAPT::remove_extension(const std::string& filename) { std::string MG_ADAPT::remove_extension(const std::string& filename)
{
size_t lastdot = filename.find_last_of("."); size_t lastdot = filename.find_last_of(".");
if (lastdot == std::string::npos) return filename; if (lastdot == std::string::npos) return filename;
return filename.substr(0, lastdot); return filename.substr(0, lastdot);
@ -65,30 +66,28 @@ namespace
return SMESH_File( fName ).exists(); return SMESH_File( fName ).exists();
} }
// ======================================================================= // =======================================================================
med_idt openMedFile(const std::string aFile) med_idt openMedFile(const std::string aFile)
// ======================================================================= // =======================================================================
// renvoie le medId associe au fichier Med apres ouverture // renvoie le medId associe au fichier Med apres ouverture
{ {
med_idt medIdt = MEDfileOpen(aFile.c_str(),MED_ACC_RDONLY); med_idt medIdt = MEDfileOpen(aFile.c_str(),MED_ACC_RDONLY);
if (medIdt <0) if (medIdt <0)
{ {
THROW_SALOME_EXCEPTION("\nThe med file " << aFile << " cannot be opened.\n"); THROW_SALOME_EXCEPTION("\nThe med file " << aFile << " cannot be opened.\n");
} }
return medIdt; return medIdt;
} }
// =======================================================================
// ======================================================================= void getTimeStepInfos(std::string aFile, med_int& numdt, med_int& numit, std::string fieldName)
void getTimeStepInfos(std::string aFile, med_int& numdt, med_int& numit, std::string fieldName) // =======================================================================
// ======================================================================= {
{ // Il faut voir si plusieurs maillages
// Il faut voir si plusieurs maillages
herr_t erreur = 0 ; herr_t erreur = 0 ;
med_idt medIdt ; med_idt medIdt ;
// Ouverture du fichier // Ouverture du fichier
//~SCRUTE(aFile.toStdString()); //~SCRUTE(aFile.toStdString());
medIdt = openMedFile(aFile); medIdt = openMedFile(aFile);
@ -105,7 +104,7 @@ void getTimeStepInfos(std::string aFile, med_int& numdt, med_int& numit, std::st
// Lecture du type du champ, des noms des composantes et du nom de l'unite // Lecture du type du champ, des noms des composantes et du nom de l'unite
char nomcha [MED_NAME_SIZE+1]; char nomcha [MED_NAME_SIZE+1];
strcpy(nomcha, fieldName.c_str()); strcpy(nomcha, fieldName.c_str());
// Lecture du nombre de composantes // Lecture du nombre de composantes
med_int ncomp = MEDfieldnComponentByName(medIdt, nomcha); med_int ncomp = MEDfieldnComponentByName(medIdt, nomcha);
char meshname[MED_NAME_SIZE+1]; char meshname[MED_NAME_SIZE+1];
char * comp = (char*) malloc(ncomp*MED_SNAME_SIZE+1); char * comp = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
@ -148,21 +147,22 @@ void getTimeStepInfos(std::string aFile, med_int& numdt, med_int& numit, std::st
// Fermeture du fichier // Fermeture du fichier
if ( medIdt > 0 ) MEDfileClose(medIdt); if ( medIdt > 0 ) MEDfileClose(medIdt);
} }
struct GET_DEFAULT // struct used to get default value from GetOptionValue() struct GET_DEFAULT // struct used to get default value from GetOptionValue()
{ {
bool isDefault; bool isDefault;
operator bool* () { operator bool* () {
return &isDefault; return &isDefault;
} }
}; };
class outFileStream : public std::ofstream{ class outFileStream : public std::ofstream{
public: public:
~outFileStream(){close();} //to close file at dtor ~outFileStream(){close();} //to close file at dtor
}; };
}
} // anonymous namespace
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
MgAdapt::MgAdapt() MgAdapt::MgAdapt()
@ -865,12 +865,13 @@ void MgAdapt::execCmd( const char* cmd, int& err)
#if defined(WIN32) #if defined(WIN32)
#if defined(UNICODE) # if defined(UNICODE)
const wchar_t * aCmd = Kernel_Utils::utf8_decode(cmd); const wchar_t * aCmd = Kernel_Utils::utf8_decode(cmd);
SMESHUtils::ArrayDeleter<const wchar_t> deleter( aCmd );
std::unique_ptr <FILE, decltype(&_pclose)> pipe(_wpopen(aCmd, O_RDONLY), _pclose ); std::unique_ptr <FILE, decltype(&_pclose)> pipe(_wpopen(aCmd, O_RDONLY), _pclose );
#else # else
std::unique_ptr <FILE, decltype(&_pclose)> pipe(_popen(cmd, "r"), _pclose ); std::unique_ptr <FILE, decltype(&_pclose)> pipe(_popen(cmd, "r"), _pclose );
#endif # endif
#else #else
std::unique_ptr <FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose ); std::unique_ptr <FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose );
#endif #endif
@ -966,7 +967,7 @@ std::string MgAdapt::getCommandToRun()
// Check coherence between mesh dimension and option fo adaptation // Check coherence between mesh dimension and option fo adaptation
checkDimensionOptionAdaptation(); checkDimensionOptionAdaptation();
// sizemap file is written only if level is higher than 3 // sizemap file is written only if level is higher than 3
if ( verbosityLevel > 3) if ( verbosityLevel > 3)
{ {
std::string solFileOut = getFileName()+".sol"; std::string solFileOut = getFileName()+".sol";

View File

@ -1077,18 +1077,14 @@ std::vector< std::string > SMESH_Gen::GetPluginXMLPaths()
xmlPath += sep + plugin + ".xml"; xmlPath += sep + plugin + ".xml";
bool fileOK; bool fileOK;
#ifdef WIN32 #ifdef WIN32
#ifdef UNICODE # ifdef UNICODE
const wchar_t* path = Kernel_Utils::decode_s(xmlPath); const wchar_t* path = Kernel_Utils::decode_s(xmlPath);
#else SMESHUtils::ArrayDeleter<const wchar_t> deleter( path );
# else
const char* path = xmlPath.c_str(); const char* path = xmlPath.c_str();
#endif # endif
fileOK = (GetFileAttributes(path) != INVALID_FILE_ATTRIBUTES); fileOK = (GetFileAttributes(path) != INVALID_FILE_ATTRIBUTES);
#ifdef UNICODE
delete path;
#endif
#else #else
fileOK = (access(xmlPath.c_str(), F_OK) == 0); fileOK = (access(xmlPath.c_str(), F_OK) == 0);
#endif #endif

View File

@ -81,12 +81,12 @@ bool SMESH_File::open()
if ( !_map && length > 0 ) if ( !_map && length > 0 )
{ {
#ifdef WIN32 #ifdef WIN32
#ifdef UNICODE # ifdef UNICODE
std::wstring aName = Kernel_Utils::utf8_decode_s(_name); std::wstring aName = Kernel_Utils::utf8_decode_s(_name);
const wchar_t* name = aName.c_str(); const wchar_t* name = aName.c_str();
#else # else
char* name = _name.data(); char* name = _name.data();
#endif # endif
_file = CreateFile(name, GENERIC_READ, FILE_SHARE_READ, _file = CreateFile(name, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
bool ok = ( _file != INVALID_HANDLE_VALUE ); bool ok = ( _file != INVALID_HANDLE_VALUE );

View File

@ -104,6 +104,8 @@ namespace SMESHUtils
TOBJ* _obj; TOBJ* _obj;
ArrayDeleter( TOBJ* obj ): _obj( obj ) {} ArrayDeleter( TOBJ* obj ): _obj( obj ) {}
~ArrayDeleter() { delete [] _obj; _obj = 0; } ~ArrayDeleter() { delete [] _obj; _obj = 0; }
operator TOBJ*() { return _obj; }
TOBJ* get() { return _obj; }
private: private:
ArrayDeleter( const ArrayDeleter& ); ArrayDeleter( const ArrayDeleter& );
}; };

View File

@ -460,18 +460,17 @@ GenericHypothesisCreator_i* SMESH_Gen_i::getHypothesisCreator(const char* theHyp
// load plugin library // load plugin library
if(MYDEBUG) MESSAGE("Loading server meshers plugin library ..."); if(MYDEBUG) MESSAGE("Loading server meshers plugin library ...");
#ifdef WIN32 #ifdef WIN32
#ifdef UNICODE # ifdef UNICODE
const wchar_t* path = Kernel_Utils::decode_s(aPlatformLibName); const wchar_t* path = Kernel_Utils::decode_s(aPlatformLibName);
#else SMESHUtils::ArrayDeleter<const wchar_t> deleter( path );
# else
const char* path = aPlatformLibName.c_str(); const char* path = aPlatformLibName.c_str();
#endif # endif
#else #else
const char* path = aPlatformLibName.c_str(); const char* path = aPlatformLibName.c_str();
#endif #endif
LibHandle libHandle = LoadLib( path ); LibHandle libHandle = LoadLib( path );
#if defined(WIN32) && defined(UNICODE)
delete path;
#endif
if (!libHandle) if (!libHandle)
{ {
// report any error, if occurred // report any error, if occurred
@ -5194,7 +5193,8 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
TCollection_AsciiString aStudyName( "" ); TCollection_AsciiString aStudyName( "" );
if ( isMultiFile ) { if ( isMultiFile ) {
CORBA::WString_var url = aStudy->URL(); CORBA::WString_var url = aStudy->URL();
aStudyName = (char*)SALOMEDS_Tool::GetNameFromPath( Kernel_Utils::encode(url.in()) ).c_str(); SMESHUtils::ArrayDeleter<const char> urlMulibyte( Kernel_Utils::encode( url.in()) );
aStudyName = (char*)SALOMEDS_Tool::GetNameFromPath( urlMulibyte.get() ).c_str();
} }
// Set names of temporary files // Set names of temporary files
TCollection_AsciiString filename = tmpDir + aStudyName + "_SMESH.hdf"; TCollection_AsciiString filename = tmpDir + aStudyName + "_SMESH.hdf";