memory leaks - C# code calling C++/CLI class method - new instance still has old instance values -


i newbie in terms of working both c# , c++/cli code together. in c# code, creating new instance of class fooclass , running method fooclass:parsecmdargs, takes in system::string, converts std::string, , pushes vector "bars" variable. print out "bars" content @ end of parsecmdargs function. getting unexpected output described below.

c# code - wpf application

private void button_click_1(object sender, routedeventargs e) {     clrclass.fooclass foo = new clrclass.fooclass();      list<string> args = new list<string>();     args.add("1st run - 1");     args.add("1st run - 2");     args.add("1st run - 3");     args.add("1st run - 4");     args.add("1st run - 5");      foo.parsecmdargs(args.count, args.toarray());      args.clear();     args.add("2nd run - 1");     args.add("2nd run - 2");     args.add("2nd run - 3");     args.add("2nd run - 4");     args.add("2nd run - 5");      foo = new clrclass.fooclass();     foo.parsecmdargs(args.count, args.toarray()); } 

clrclass.h

#pragma once #include <string> #include <iostream> #include <msclr\marshal_cppstd.h>  using namespace system;  namespace clrclass {    public ref class fooclass   {   public:     void parsecmdargs(int, array<system::string^>^);   }; } 

clrclass.cpp

#include "stdafx.h"  #include "clrclass.h"  using namespace clrclass; using namespace std;  vector<string> bars;  void fooclass::parsecmdargs(int argc, array<system::string^> ^argvmanaged) {   string *argv;   argv = new string[argc];    (int j = 0; j < argc; j++) {     msclr::interop::marshal_context context;     std::string standardstring = context.marshal_as<std::string>(argvmanaged[j]);      argv[j] = standardstring;      bars.push_back(argv[j]);   }    cout << "print bars" << endl;   cout << "-----------" << endl;    (int = 0; < bars.size(); i++) {     cout << bars[i] << endl;   } } 

the output is:

print bars ----------- 1st run - 1 1st run - 2 1st run - 3 1st run - 4 1st run - 5 print bars ----------- 1st run - 1 1st run - 2 1st run - 3 1st run - 4 1st run - 5 2nd run - 1 2nd run - 2 2nd run - 3 2nd run - 4 2nd run - 5 

i have few queries if assist me here:

1) did not expect first instance of fooclass assigned "foo" still keep content of "bars" variable (in c++/cli code). can see output, after assigning new instance of fooclass "foo", old content "1st run" stored in "new" fooclass instance's "bars" variable. 1a) see why happening? 1b) need setup destructor fooclass?

2) best way me call parsecmdargs() function 1 instance of fooclass? suppose creating new instance of fooclass not ideal (which not working anyways, see 1). need bars.clear() each time parsecmdargs finished??

3) way marshaling / converting system::string std::string correct?

4) e.g. there concern memory leaks in current implementation of c# managed code calling unmanaged c++/cli class , use of marshaling?

thank advance help, , let me know if need further details or clarifications.

best regards

you need make vector<string> bars; local variable of function. or @ least instance field of class.

right it's global variable keep it's value long have dll loaded appdomain (simply put, whole duration of program).

this has nothing clr or managed code. same happen if called c++ code c++ executable.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -