C++ - Object Heap Marker to Detect Memory Leaks
In this post I have a program which generates core and from the core file we can find out how many objects for a particular class has been created. This helps in isolating memory leaks related issues. Using this method we can analyze release mode core dumps so as to see the values within each objects.The basic idea is to put a heap marker string along with each object so that we can identify the object start and end by going through the core dump. What I am doing here is create an array within the class which will have a predefined string so as to identify the object. Now whenever we create that object it gets persisted into heap and at any time we dump the heap we can see that string in heap thereby identifying the start and end of an object. I have tested all these in redhat linux 8.0.
Here is the Class which has markers
#define MARKERSIZE 5
#define BEGMARKER "<A>"
#define ENDMARKER "</A>"
#include <iostream>
class A
{
char beginA[MARKERSIZE ];
int i;
char b;
char endA[MARKERSIZE];
public:
A() : i(0xabcd), b('X')
{
std::cout << "CTOR" << std::endl;
strcpy(beginA,BEGMARKER);
strcpy(endA,ENDMARKER);
}
~A()
{
std::cout << "DTOR" << std::endl;
}
void displayme()
{
printf("ADDR BEG = %p\n",beginA)
printf("ADDR I = %p\n", &i);
printf("ADDR B = %p\n", &b);
printf("ADDR END = %p\n",endA)
}
}
Now the main function which create 10 such objects and generates a core. After which we will analyze the core file to say how many objects for class A got created
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <A.hpp>
int main()
{
for(int i=0 ; i<10 ; ++i)
{
A *a = new A();
//a->displayme();
//delete a;
}
//send a SIGSEGV signal to generate core
pid_t pid = getpid();
kill(pid,11);
}
Before running the program make sure that you have set the ulimit properly to generate core dumps.
-bash-2.05b# ulimit -c unlimited
-bash-2.05b# ./a.out > /dev/null
Segmentation Fault (core dumped)
-bash-2.05b# od –c core.1233| grep "< / A >"|wc –l
11
Here the count is showing as 11 because core also has the string literal entry for ENDMARKER. So 11 – 1 = 10. Just by going through the core file we are able to tell how many objects of A reside in memory.
Now using od itself you can analyze the objects and figure out the values which got populated into the object. While going through the values within the object make sure you take care of the endianess.
Labels: C++, markers, Memory, memory leak
Posted by - at 9:05 pm | 0 comments read on
C# - Command line Tools n Modularizing IF-ELSE
I have been writing some of the command line tools which handles lots of functionality. I used to end up writing lots of IF-ELSE which are non reusable code. What I think we should do to make such tools really extensible is defining the commands within an XML file.
<command name="displayallnames" class="[class and assembly name]" />
Once a person enters a particular option we traverse through the input XML file to pick the required class and call necessary methods.
An interface ICommand like defines what we expect from such commands.
namespace code.grep.in.commandline
{
enum ExecResult
{
int SUCCESS = 0;
int FAILURE = 1;
int INVALID_USAGE = 2;
}
interface ICommand {
ExecResult execute(Arguments args);
void usage();
}
}
Now what we can do is in the command line tools is to read the XML file and using reflection call the corresponding classes execute method.
I will post the complete implementation soon!
Labels: C#, commandline
Posted by - at 7:48 pm | 3 comments read on
SharePoint - SSP Personalization Services Permissions
In /ssp/admin/manageservicepermissions.aspx datasources are used to map these permissions. So it seems it gets stored inside a table within SQL
<ItemTemplate>
<asp:LinkButton runat="server"
ForeColor='<%#DataBinder.Eval(Container.DataItem, "Color")%>'
OnClientClick='<%#DataBinder.Eval(Container.DataItem, "Script")%>'
Text='<%# DataBinder.Eval(Container.DataItem, "UserName")%>'
OnClick="EditUsers"
ToolTip='<%# DataBinder.Eval(Container.DataItem, "UserName")%>'/>
</ItemTemplate>
SSP service permissions are stored as a text field under dbo.MIPObjects of SSP's database. It uses 6 bits to store Manage Analytics, Manage Audiences, Manage User Profiles, Personal Features, Personal Site, Set Permissions and i can go through enabling each to find the flags.
<object>
<field name="PersistedXml" type="string">
<?xml version="1.0" encoding="utf-16"?>
<Acl>
<Ace IdentityName="NT AUTHORITY\Authenticated Users"
DisplayName="NT AUTHORITY\Authenticated
Users" SID="***"
Rights="3" />
<Ace IdentityName="NT AUTHORITY\NETWORK SERVICE"
DisplayName="NT AUTHORITY\NETWORK SERVICE"
SID="**"
Rights="63" />
<Ace IdentityName="fbamembers:admin"
DisplayName="admin"
SID=""
Rights="63" />
<Ace IdentityName="fbaroles:users"
DisplayName="Users"
SID=""
Rights="3" />
</Acl>
</field>
</object>
Posted by - at 9:58 am | 0 comments read on